custom/plugins/FourtwosixShippingCostsCalculator/src/Decorator/Core/System/SalesChannel/Context/BaseContextFactoryDecorator.php line 86

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace FourtwosixShippingCostsCalculator\Decorator\Core\System\SalesChannel\Context;
  3. use Doctrine\DBAL\Connection;
  4. use Exception;
  5. use Shopware\Core\Checkout\Cart\Delivery\Struct\ShippingLocation;
  6. use Shopware\Core\Checkout\Cart\Price\Struct\CartPrice;
  7. use Shopware\Core\Checkout\Customer\Aggregate\CustomerAddress\CustomerAddressEntity;
  8. use Shopware\Core\Checkout\Payment\Exception\UnknownPaymentMethodException;
  9. use Shopware\Core\Checkout\Payment\PaymentMethodEntity;
  10. use Shopware\Core\Checkout\Shipping\ShippingMethodEntity;
  11. use Shopware\Core\Defaults;
  12. use Shopware\Core\Framework\Api\Context\AdminSalesChannelApiSource;
  13. use Shopware\Core\Framework\Api\Context\SalesChannelApiSource;
  14. use Shopware\Core\Framework\Context;
  15. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Pricing\CashRoundingConfig;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  18. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  19. use Shopware\Core\Framework\Feature;
  20. use Shopware\Core\Framework\Uuid\Uuid;
  21. use Shopware\Core\System\Country\Aggregate\CountryState\CountryStateEntity;
  22. use Shopware\Core\System\Country\CountryEntity;
  23. use Shopware\Core\System\Currency\Aggregate\CurrencyCountryRounding\CurrencyCountryRoundingEntity;
  24. use Shopware\Core\System\Currency\CurrencyEntity;
  25. use Shopware\Core\System\SalesChannel\BaseContext;
  26. use Shopware\Core\System\SalesChannel\Context\BaseContextFactory;
  27. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextService;
  28. use Shopware\Core\System\SalesChannel\SalesChannelEntity;
  29. use Shopware\Core\System\Tax\TaxCollection;
  30. class BaseContextFactoryDecorator extends BaseContextFactory
  31. {
  32.     public function __construct(private EntityRepository $salesChannelRepository,
  33.         private EntityRepository $currencyRepository,
  34.         private EntityRepository $customerGroupRepository,
  35.         private EntityRepository $countryRepository,
  36.         private EntityRepository $taxRepository,
  37.         private $paymentMethodRepository,
  38.         private EntityRepository $shippingMethodRepository,
  39.         private Connection       $connection,
  40.         private EntityRepository $countryStateRepository,
  41.         private EntityRepository $currencyCountryRepository)
  42.     {
  43.         parent::__construct($salesChannelRepository$currencyRepository$customerGroupRepository$countryRepository$taxRepository$paymentMethodRepository$shippingMethodRepository$connection$countryStateRepository$currencyCountryRepository);
  44.     }
  45.     public function create(string $salesChannelId, array $options = []): BaseContext
  46.     {
  47.         $context $this->getContext($salesChannelId$options);
  48.         $criteria = new Criteria([$salesChannelId]);
  49.         $criteria->setTitle('base-context-factory::sales-channel');
  50.         $criteria->addAssociation('currency');
  51.         $criteria->addAssociation('domains');
  52.         /** @var SalesChannelEntity|null $salesChannel */
  53.         $salesChannel $this->salesChannelRepository->search($criteria$context)
  54.             ->get($salesChannelId);
  55.         if (!$salesChannel) {
  56.             throw new \RuntimeException(sprintf('Sales channel with id %s not found or not valid!'$salesChannelId));
  57.         }
  58.         if (!Feature::isActive('FEATURE_NEXT_17276')) {
  59.             /*
  60.              * @deprecated tag:v6.5.0 - Overriding the languageId of the SalesChannel is deprecated and will be removed in v6.5.0
  61.              * use `$salesChannelContext->getLanguageId()` instead
  62.              */
  63.             if (\array_key_exists(SalesChannelContextService::LANGUAGE_ID$options)) {
  64.                 $salesChannel->setLanguageId($options[SalesChannelContextService::LANGUAGE_ID]);
  65.             }
  66.         }
  67.         //load active currency, fallback to shop currency
  68.         $currency $salesChannel->getCurrency();
  69.         if (\array_key_exists(SalesChannelContextService::CURRENCY_ID$options)) {
  70.             $currencyId $options[SalesChannelContextService::CURRENCY_ID];
  71.             $criteria = new Criteria([$currencyId]);
  72.             $criteria->setTitle('base-context-factory::currency');
  73.             $currency $this->currencyRepository->search($criteria$context)->get($currencyId);
  74.         }
  75.         //load not logged in customer with default shop configuration or with provided checkout scopes
  76.         $shippingLocation $this->loadShippingLocation($options$context$salesChannel);
  77.         $groupId $salesChannel->getCustomerGroupId();
  78.         /** @deprecated tag:v6.5.0 - Fallback customer group is deprecated and will be removed */
  79.         $groupIds array_unique([$salesChannel->getCustomerGroupId(), Defaults::FALLBACK_CUSTOMER_GROUP]);
  80.         $criteria = new Criteria($groupIds);
  81.         $criteria->setTitle('base-context-factory::customer-group');
  82.         $customerGroups $this->customerGroupRepository->search($criteria$context);
  83.         /** @deprecated tag:v6.5.0 - Fallback customer group is deprecated and will be removed */
  84.         $fallbackGroup $customerGroups->has(Defaults::FALLBACK_CUSTOMER_GROUP) ? $customerGroups->get(Defaults::FALLBACK_CUSTOMER_GROUP) : $customerGroups->get($salesChannel->getCustomerGroupId());
  85.         $customerGroup $customerGroups->get($groupId);
  86.         //loads tax rules based on active customer and delivery address
  87.         $taxRules $this->getTaxRules($context);
  88.         //detect active payment method, first check if checkout defined other payment method, otherwise validate if customer logged in, at least use shop default
  89.         $payment $this->getPaymentMethod($options$context$salesChannel);
  90.         //detect active delivery method, at first checkout scope, at least shop default method
  91.         $shippingMethod $this->getShippingMethod($options$context$salesChannel);
  92.         [$itemRounding$totalRounding] = $this->getCashRounding($currency$shippingLocation$context);
  93.         $context = new Context(
  94.             $context->getSource(),
  95.             [],
  96.             $currency->getId(),
  97.             $context->getLanguageIdChain(),
  98.             $context->getVersionId(),
  99.             $currency->getFactor(),
  100.             true,
  101.             CartPrice::TAX_STATE_GROSS,
  102.             $itemRounding
  103.         );
  104.         return new BaseContext(
  105.             $context,
  106.             $salesChannel,
  107.             $currency,
  108.             $customerGroup,
  109.             $fallbackGroup,
  110.             $taxRules,
  111.             $payment,
  112.             $shippingMethod,
  113.             $shippingLocation,
  114.             $itemRounding,
  115.             $totalRounding
  116.         );
  117.     }
  118.     /**
  119.      * @param array<string, mixed> $options
  120.      * @fourtwosix-edit
  121.      */
  122.     private function loadShippingLocation(array $optionsContext $contextSalesChannelEntity $salesChannel): ShippingLocation
  123.     {
  124.         $customerAddress null;
  125.         if (isset($options["zipcode"])) {
  126.             $customerAddress = new CustomerAddressEntity();
  127.             $customerAddress->setZipcode($options["zipcode"]);
  128.         }
  129.         // allows previewing cart calculation for a specify state for not logged in customers
  130.         if (isset($options[SalesChannelContextService::COUNTRY_STATE_ID])) {
  131.             $countryStateId $options[SalesChannelContextService::COUNTRY_STATE_ID];
  132.             \assert(\is_string($countryStateId) && Uuid::isValid($countryStateId));
  133.             $criteria = new Criteria([$countryStateId]);
  134.             $criteria->addAssociation('country');
  135.             $criteria->setTitle('base-context-factory::country');
  136.             $state $this->countryStateRepository->search($criteria$context)->get($countryStateId);
  137.             if (!$state instanceof CountryStateEntity) {
  138.                 throw new Exception('countryStateNotFound' $countryStateId);
  139.             }
  140.             /** @var CountryEntity $country */
  141.             $country $state->getCountry();
  142.             if (isset($options["zipcode"])) {
  143.                 $customerAddress->setCountry($country);
  144.             }
  145.             return new ShippingLocation($country$state$customerAddress);
  146.         }
  147.         $countryId $options[SalesChannelContextService::COUNTRY_ID] ?? $salesChannel->getCountryId();
  148.         \assert(\is_string($countryId) && Uuid::isValid($countryId));
  149.         $criteria = new Criteria([$countryId]);
  150.         $criteria->setTitle('base-context-factory::country');
  151.         $country $this->countryRepository->search($criteria$context)->get($countryId);
  152.         if (!$country instanceof CountryEntity) {
  153.             throw new Exception('countryNotFound ' $countryId);
  154.         }
  155.         if (isset($options["zipcode"])) {
  156.             $customerAddress->setCountry($country);
  157.         }
  158.         return new ShippingLocation($countrynull$customerAddress);
  159.     }
  160.     private function getTaxRules(Context $context): TaxCollection
  161.     {
  162.         $criteria = new Criteria();
  163.         $criteria->setTitle('base-context-factory::taxes');
  164.         $criteria->addAssociation('rules.type');
  165.         /** @var TaxCollection $taxes */
  166.         $taxes $this->taxRepository->search($criteria$context)->getEntities();
  167.         return $taxes;
  168.     }
  169.     /**
  170.      * @param array<string, mixed> $options
  171.      */
  172.     private function getPaymentMethod(array $optionsContext $contextSalesChannelEntity $salesChannel): PaymentMethodEntity
  173.     {
  174.         $id $options[SalesChannelContextService::PAYMENT_METHOD_ID] ?? $salesChannel->getPaymentMethodId();
  175.         $criteria = (new Criteria([$id]))->addAssociation('media');
  176.         $criteria->setTitle('base-context-factory::payment-method');
  177.         $paymentMethod $this->paymentMethodRepository
  178.             ->search($criteria$context)
  179.             ->get($id);
  180.         if (!$paymentMethod instanceof PaymentMethodEntity) {
  181.             throw new UnknownPaymentMethodException($id);
  182.         }
  183.         return $paymentMethod;
  184.     }
  185.     /**
  186.      * @param array<string, mixed> $options
  187.      */
  188.     private function getShippingMethod(array $optionsContext $contextSalesChannelEntity $salesChannel): ShippingMethodEntity
  189.     {
  190.         $id $options[SalesChannelContextService::SHIPPING_METHOD_ID] ?? $salesChannel->getShippingMethodId();
  191.         $ids \array_unique(array_filter([$id$salesChannel->getShippingMethodId()]));
  192.         $criteria = new Criteria($ids);
  193.         $criteria->addAssociation('media');
  194.         $criteria->setTitle('base-context-factory::shipping-method');
  195.         $shippingMethods $this->shippingMethodRepository->search($criteria$context);
  196.         /** @var ShippingMethodEntity $shippingMethod */
  197.         $shippingMethod $shippingMethods->get($id) ?? $shippingMethods->get($salesChannel->getShippingMethodId());
  198.         return $shippingMethod;
  199.     }
  200.     /**
  201.      * @return CashRoundingConfig[]
  202.      */
  203.     private function getCashRounding(CurrencyEntity $currencyShippingLocation $shippingLocationContext $context): array
  204.     {
  205.         $criteria = new Criteria();
  206.         $criteria->setTitle('base-context-factory::cash-rounding');
  207.         $criteria->setLimit(1);
  208.         $criteria->addFilter(new EqualsFilter('currencyId'$currency->getId()));
  209.         $criteria->addFilter(new EqualsFilter('countryId'$shippingLocation->getCountry()->getId()));
  210.         $countryConfig $this->currencyCountryRepository->search($criteria$context)->first();
  211.         if ($countryConfig instanceof CurrencyCountryRoundingEntity) {
  212.             return [$countryConfig->getItemRounding(), $countryConfig->getTotalRounding()];
  213.         }
  214.         return [$currency->getItemRounding(), $currency->getTotalRounding()];
  215.     }
  216.     /**
  217.      * @param array<string, mixed> $session
  218.      */
  219.     private function getContext(string $salesChannelId, array $session): Context
  220.     {
  221.         $sql '
  222.         # context-factory::base-context
  223.         SELECT
  224.           sales_channel.id as sales_channel_id,
  225.           sales_channel.language_id as sales_channel_default_language_id,
  226.           sales_channel.currency_id as sales_channel_currency_id,
  227.           currency.factor as sales_channel_currency_factor,
  228.           GROUP_CONCAT(LOWER(HEX(sales_channel_language.language_id))) as sales_channel_language_ids
  229.         FROM sales_channel
  230.             INNER JOIN currency
  231.                 ON sales_channel.currency_id = currency.id
  232.             LEFT JOIN sales_channel_language
  233.                 ON sales_channel_language.sales_channel_id = sales_channel.id
  234.         WHERE sales_channel.id = :id
  235.         GROUP BY sales_channel.id, sales_channel.language_id, sales_channel.currency_id, currency.factor';
  236.         $data $this->connection->fetchAssociative($sql, [
  237.             'id' => Uuid::fromHexToBytes($salesChannelId),
  238.         ]);
  239.         if ($data === false) {
  240.             throw new Exception('noContextData '$salesChannelId);
  241.         }
  242.         if (isset($session[SalesChannelContextService::ORIGINAL_CONTEXT])) {
  243.             $origin = new AdminSalesChannelApiSource($salesChannelId$session[SalesChannelContextService::ORIGINAL_CONTEXT]);
  244.         } else {
  245.             $origin = new SalesChannelApiSource($salesChannelId);
  246.         }
  247.         // explode all available languages for the provided sales channel
  248.         $languageIds $data['sales_channel_language_ids'] ? explode(',', (string)$data['sales_channel_language_ids']) : [];
  249.         $languageIds array_keys(array_flip($languageIds));
  250.         // check which language should be used in the current request (request header set, or context already contains a language - stored in `sales_channel_api_context`)
  251.         $defaultLanguageId Uuid::fromBytesToHex($data['sales_channel_default_language_id']);
  252.         $languageChain $this->buildLanguageChain($session$defaultLanguageId$languageIds);
  253.         $versionId Defaults::LIVE_VERSION;
  254.         if (isset($session[SalesChannelContextService::VERSION_ID])) {
  255.             $versionId $session[SalesChannelContextService::VERSION_ID];
  256.         }
  257.         return new Context(
  258.             $origin,
  259.             [],
  260.             Uuid::fromBytesToHex($data['sales_channel_currency_id']),
  261.             $languageChain,
  262.             $versionId,
  263.             (float)$data['sales_channel_currency_factor'],
  264.             true
  265.         );
  266.     }
  267.     /**
  268.      * @param array<string, mixed> $sessionOptions
  269.      * @param array<string> $availableLanguageIds
  270.      *
  271.      * @return non-empty-array<string>
  272.      */
  273.     private function buildLanguageChain(array $sessionOptionsstring $defaultLanguageId, array $availableLanguageIds): array
  274.     {
  275.         $current $sessionOptions[SalesChannelContextService::LANGUAGE_ID] ?? $defaultLanguageId;
  276.         if (!\is_string($current) || !Uuid::isValid($current)) {
  277.             throw new Exception('invalidLanguageId');
  278.         }
  279.         // check provided language is part of the available languages
  280.         if (!\in_array($current$availableLanguageIdstrue)) {
  281.             throw new Exception('providedLanguageNotAvailable current:' $current ' available: '.  implode(', '$availableLanguageIds));
  282.         }
  283.         if ($current === Defaults::LANGUAGE_SYSTEM) {
  284.             return [Defaults::LANGUAGE_SYSTEM];
  285.         }
  286.         // provided language can be a child language
  287.         return array_filter([$current$this->getParentLanguageId($current), Defaults::LANGUAGE_SYSTEM]);
  288.     }
  289.     private function getParentLanguageId(string $languageId): ?string
  290.     {
  291.         $data $this->connection->createQueryBuilder()
  292.             ->select(['LOWER(HEX(language.parent_id))'])
  293.             ->from('language')
  294.             ->where('language.id = :id')
  295.             ->setParameter('id'Uuid::fromHexToBytes($languageId))
  296.             ->executeQuery()
  297.             ->fetchOne();
  298.         if ($data === false) {
  299.             throw new Exception('languageNotFound '$languageId);
  300.         }
  301.         return $data;
  302.     }
  303. }