Country Context
CoreShop uses a system of contexts and resolvers to determine the current country of a visitor or customer.
Context
Different contexts are used for determining the appropriate country in various scenarios:
Name | Priority | Tag | Description |
---|---|---|---|
FixedCountryContext | default | coreshop.context.country | For testing purposes. |
CountryContext | default | coreshop.context.country | Checks for a country within the country request resolver. |
StoreAwareCountryContext | default | coreshop.context.country | Considers the current store context to determine the country. |
Resolver
Resolvers are used to find the correct country based on different criteria:
Name | Priority | Tag | Description |
---|---|---|---|
GeoLiteBasedRequestResolver | 10 | coreshop.context.country.request_based.resolver | Uses the Geo Lite Database to determine user location. |
Create a Custom Resolver
To create a custom Country Context:
-
Implement the Interface: Your context should implement
CoreShop\Component\Address\Context\CountryContextInterface
. -
Define the Method: This interface has a method
getCountry
returningCoreShop\Component\Address\Model\CountryInterface
or throwingCoreShop\Component\Address\Context\CountryNotFoundException
. -
Register the Context: Use the tag
coreshop.context.country
with an optionalpriority
attribute for registration.
Create a Request-Based Resolver
CoreShop supports custom request-based country context resolvers:
-
Implement the Interface: Create a resolver that implements
CoreShop\Component\Address\Context\RequestBased\RequestResolverInterface
. -
Define the Method: Implement the method
findCountry
, which returns a country or null based on the request. -
Register the Resolver: Use the tag
coreshop.context.country.request_based.resolver
with an optionalpriority
attribute.
Example
Creating a DocumentBasedRequestRequestResolver
based on Pimcore Document:
// PHP code for creating DocumentBasedRequestRequestResolver
And configure the service:
services:
App\CoreShop\Address\Context\DocumentBasedRequestRequestResolver:
arguments:
- '@Pimcore\Http\Request\Resolver\DocumentResolver'
- '@coreshop.repository.country'
tags:
- { name: coreshop.context.country.request_based.resolver }
CoreShop will now resolve the current country based on the Pimcore site being accessed.