Skip to content

Understanding the Core Class

Webkul\Core\Core, in packages/Webkul/Core/src/Core.php, holds the store-wide lookups most Bagisto code needs: the current channel, locale and currency, price conversion and formatting, the saved configuration, countries and states, and a few utilities. You reach it through the global core() helper, which returns the instance behind the Webkul\Core\Facades\Core facade, so every call in one request or one queued job talks to the same object, and that object remembers what it looks up.

php
$channelCode = core()->getCurrentChannelCode();

$total = core()->formatPrice($amount, 'EUR');

$maxAttempts = core()->getConfigData('sales.order_settings.order_creation.max_retry_attempts');

How the Current Context Is Resolved

Each of these values is resolved once, then the remembered model is returned:

ValueResolved fromFallback
Current channelThe channel whose hostname matches the request host, with or without http:// or https://The first channel
Default channelThe channel whose code is config('app.channel')The first channel
Current localeThe locale whose code is app()->getLocale()The locale for config('app.fallback_locale')
Base currencyThe currency whose code is config('app.currency')The first currency
Current currencyThe currency last passed to setCurrentCurrency()The current channel's base currency

The "requested" methods read the admin's channel and locale switchers instead, the channel and locale query parameters; they're in the tables below.

Method Reference

Signatures are as declared in Core.php and its Webkul\Core\Concerns\CurrencyFormatter trait.

Channels

MethodReturns
getAllChannels()Every channel
getCurrentChannel(?string $hostname = null)The current channel. $hostname replaces the request host, but only on the first call
setCurrentChannel(Channel $channel): voidMakes $channel the current channel
getCurrentChannelCode(): ?stringThe current channel's code
getDefaultChannel(): ?ChannelThe default channel
setDefaultChannel(Channel $channel): voidMakes $channel the default channel
getDefaultChannelCode(): ?stringThe default channel's code
getDefaultLocaleCodeFromDefaultChannel(): stringThe default locale code of the default channel
getRequestedChannel()The channel named by the channel query parameter, null when no channel has that code, or the current channel when the parameter is absent
getRequestedChannelCode($fallback = true)The channel request input; with $fallback, the current and then the default channel code when it is empty
getChannelName($channel): stringThe channel's name, falling back to its translation in the app locale and then the fallback locale

Locales

MethodReturns
getAllLocales()Every locale, sorted by name
getCurrentLocale()The current locale
getRequestedLocale()The locale named by the locale query parameter, null when no locale has that code, or the current locale when the parameter is absent
getRequestedLocaleCode($localeKey = 'locale', $fallback = true)The $localeKey request input; with $fallback, app()->getLocale() when it is empty
getRequestedLocaleCodes($localeKey = 'locale'): arrayEvery locale code when the input is all, otherwise a one-item array of the requested code
getRequestedLocaleCodeInRequestedChannel()The requested locale code when the requested channel has that locale, otherwise the channel's default locale code

Currencies and Prices

MethodReturns
getAllCurrencies()Every currency
getBaseCurrency()The base currency
getBaseCurrencyCode()The base currency code
getChannelBaseCurrency()The current channel's base currency
getChannelBaseCurrencyCode()The current channel's base currency code
setCurrentCurrency($currencyCode)Sets the current currency; an unknown code selects the channel's base currency
getCurrentCurrency()The current currency
getCurrentCurrencyCode()The current currency code
getExchangeRate($targetCurrencyId)The exchange rate row for a currency id, or null
convertPrice($amount, $targetCurrencyCode = null)$amount multiplied by the target currency's rate (the current currency when no code is given); unchanged when there is no rate
convertToBasePrice($amount, $targetCurrencyCode = null)$amount divided by the target currency's rate; unchanged when there is no rate
currency($amount = 0)$amount converted to the current currency and formatted
formatPrice(?float $price, ?string $currencyCode = null): string$price formatted in the given or current currency, without converting it
formatBasePrice(?float $price): string$price formatted in the base currency
formatCurrency(?float $price, Currency $currency): string$price formatted for the app locale: with the currency's position, decimals and separators when its currency_position is set, otherwise in the locale's own format with the currency's symbol
useDefaultCurrencyFormatter(?float $price, Currency $currency): stringThe locale's currency format, using the currency's own symbol when it differs from the locale's
useCustomCurrencyFormatter(?float $price, Currency $currency): stringThe number with the currency's decimals and separators, and its symbol, or its code when it has none, placed by currency_position
currencySymbol($currency): stringThe locale's symbol for a currency code or Currency model

Dates

MethodReturns
isChannelDateInInterval($dateFrom = null, $dateTo = null)Whether today, in the current channel's timezone, is between the two dates inclusive; an empty date leaves that side open
channelTimeStamp($channel)The current Unix timestamp; the channel's timezone doesn't change it
is_empty_date($date)Whether a SQL date such as 0000-00-00 00:00:00 is empty
formatDate($date = null, $format = 'd-m-Y H:i:s')The date (now when null) in the current channel's timezone, formatted with Carbon's translatedFormat()
xWeekRange($date, $day)As Y-m-d, the Sunday on or before $date when $day is 0, otherwise the Saturday on or after it

Configuration

MethodReturns
getConfigData(string $field, ?string $currentChannelCode = null, ?string $currentLocaleCode = null): mixedThe saved configuration value. The codes default to getRequestedChannelCode() and getRequestedLocaleCode(): the request's channel and locale input, then the current channel and the app locale. Delegates to system_config()->getConfigData()
getConfigField($fieldName)The field definition from the core configuration tree

A value that has never been saved falls back to config() at the same key without its first segment (sales.payment_methods.<code>.<field> reads config('payment_methods.<code>.<field>')), then to the field's default in system.php; see System Configuration.

Countries and Addresses

MethodReturns
countries()Every row of the countries table
country_name($code)The country name for an ISO 3166-1 alpha-2 code, or an empty string
states($countryCode)The states of a country
groupedStatesByCountries()Every state, grouped in an array keyed by country code
findStateByCountryCode($countryCode = null, $stateCode = null)The matching state, or false
isCountryRequired(), isStateRequired(), isPostCodeRequired()Whether the address field is required, from customer.address.requirements.*

Customers, Taxes and Email

MethodReturns
getGuestCustomerGroup()The customer group with the code guest
getTaxCategoryById($id)The tax category, or null for an empty id
Core::taxRateAsIdentifier(float $taxRate): stringStatic. The rate with dots replaced by underscores, for use in element ids
getSenderEmailDetails()['name' => …, 'email' => …] from emails.configure.email_settings.sender_*, falling back to config('mail.from')
getAdminEmailDetails()The same shape from admin_name and admin_email, falling back to config('mail.admin') and then config('mail.from')
getContactEmailDetails()The same shape from contact_name and contact_email, falling back to config('mail.contact') and then config('mail.from')

Utilities

MethodReturns
version()The value of Core::BAGISTO_VERSION
getMaxUploadSize()PHP's upload_max_filesize setting
convertEmptyStringsToNull($array)The array with empty strings and the string 'null' replaced by null
getSingletonInstance($className)One instance of $className per Core instance, resolved from the container
getSpeculationRules()The browser speculation rules (prerender and prefetch) built from general.content.speculation_rules.*

Other Global Helpers

Each of these is defined in its package's src/Http/helpers.php and available everywhere without an import.

HelperPackageReturns
core()CoreWebkul\Core\Core
menu()CoreWebkul\Core\Menu; menu()->getItems(Menu::ADMIN)
acl()CoreWebkul\Core\Acl
system_config()CoreWebkul\Core\SystemConfig
db_grammar()CoreWebkul\Core\Contracts\DatabaseGrammar for the current database. See Database Compatibility
clean_path(string $path)CoreThe path with empty segments removed
clean_content(string $content)CoreHTML purified and stripped of Blade directives
array_permutation($input)CoreEvery combination of an array of arrays
themes()ThemeWebkul\Theme\Themes; themes()->current(), themes()->set($themeName)
bagisto_asset(string $path, ?string $namespace = null)ThemeThe Vite URL of a theme asset
bagisto_theme_storage()ThemeWebkul\Theme\ThemeStorage for section media URLs
view_render_event(string $eventName, mixed $params = null)ThemeRendered listener output. See View Render Events
product_image()ProductWebkul\Product\ProductImage
product_video()ProductWebkul\Product\ProductVideo
product_toolbar()ProductWebkul\Product\Helpers\Toolbar (sort orders and page limits)
image_manager()ImageCacheIlluminate\Image\ImageManager
image_urls(string $path, ?string $key = null)ImageCacheThe *_image_url set for a stored path. See Image Cache
datagrid(string $datagridClass)DataGridA resolved DataGrid instance
cart()CheckoutWebkul\Checkout\Cart
payment()PaymentWebkul\Payment\Payment
shipping()ShippingWebkul\Shipping\Shipping
magic_ai()MagicAIWebkul\MagicAI\MagicAI
bouncer()UserWebkul\User\Bouncer; hasPermission(), and the static allow()
two_factor_authentication()UserWebkul\User\TwoFactorAuthentication

db_grammar(), bagisto_theme_storage() and image_urls() were added in Bagisto 2.5, and on Bagisto 2.4 image_manager() returns Intervention\Image\ImageManager. There is no theme(), visitor() or money_format() helper.

Things to Watch

  • core() remembers for one request or one job, then starts fresh. The current and default channel, the current and base currency, the current locale, the guest customer group, exchange rates and tax categories are looked up once and kept on the instance. Webkul\Core\Core has no container binding: the instance is the one the facade caches, and php artisan queue:work clears that cache before every job, as Laravel Octane does for every request. Code that works through several channels in one job must call setCurrentChannel(), and setCurrentCurrency(), which is remembered separately, for each one.
  • A command or queued job has no browser request. Laravel builds the console request from APP_URL, so getCurrentChannel() returns the channel whose hostname matches APP_URL, or the first channel.
  • formatPrice() doesn't convert; currency() does. currency() converts from the base currency before formatting, so an amount already in the order's currency is converted twice.
  • getConfigData() isn't config(). It reads the configuration saved from the admin, per channel and locale, from the database.
  • countries() and groupedStatesByCountries() query the database on every call. Call them once per request and reuse the result.

Released under the MIT License.