custom/plugins/VioB2BLogin/src/Core/System/SalesChannel/Context/SalesChannelContextFactory.php line 40

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace VioB2BLogin\Core\System\SalesChannel\Context;
  4. use Shopware\Core\Checkout\Cart\Delivery\Struct\ShippingLocation;
  5. use Shopware\Core\Checkout\Customer\Aggregate\CustomerAddress\CustomerAddressEntity;
  6. use Shopware\Core\System\SalesChannel\Context\AbstractSalesChannelContextFactory;
  7. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextPersister;
  8. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  9. use VioB2BLogin\Core\Services\EmployeeService;
  10. use VioB2BLogin\Entity\Employee\EmployeeEntity;
  11. use VioB2BLogin\Entity\EmployeeAddress\EmployeeAddressEntity;
  12. class SalesChannelContextFactory extends AbstractSalesChannelContextFactory
  13. {
  14.     public const EMPLOYEE_ID 'employeeId';
  15.     public const EMPLOYEE_KEY 'employee';
  16.     private AbstractSalesChannelContextFactory $coreFactory;
  17.     private SalesChannelContextPersister $contextPersister;
  18.     private EmployeeService $employeeService;
  19.     public function __construct(
  20.         AbstractSalesChannelContextFactory $coreFactory,
  21.         EmployeeService                    $employeeService,
  22.         SalesChannelContextPersister       $contextPersister
  23.     )
  24.     {
  25.         $this->coreFactory $coreFactory;
  26.         $this->contextPersister $contextPersister;
  27.         $this->employeeService $employeeService;
  28.     }
  29.     public function getDecorated(): AbstractSalesChannelContextFactory
  30.     {
  31.         return $this->coreFactory;
  32.     }
  33.     public function create(string $tokenstring $salesChannelId, array $options = []): SalesChannelContext
  34.     {
  35.         $saleChannelContext $this->coreFactory->create($token$salesChannelId$options);
  36.         $session $this->contextPersister->load($token$salesChannelId);
  37.         $employee $this->loadEmployee($session$saleChannelContext);
  38.         $saleChannelContext->addExtension(self::EMPLOYEE_KEY$employee);
  39.         if ($employee instanceof EmployeeEntity
  40.         ) {
  41.             $newDefaultBillingAddressId null;
  42.             $newDefaultShippingAddressId null;
  43.             $possibleBillingIds = [];
  44.             // reset default billing address in Context
  45.             if ($employee->getAddresses()
  46.                 && $employee->getAddresses()->count() > 0
  47.             ) {
  48.                 $possibleBillingIds $employee->getAddresses()->filter(
  49.                     function (CustomerAddressEntity $address) {
  50.                         /** @var EmployeeAddressEntity $relation */
  51.                         $relation $address->getExtensionOfType('relationData'EmployeeAddressEntity::class);
  52.                         return $relation->isAsBillingAddress();
  53.                     })->getIds();
  54.                 $newDefaultBillingAddressId array_shift($possibleBillingIds);
  55.                 if (!empty($newDefaultBillingAddressId)
  56.                 ) {
  57.                     if (!in_array($saleChannelContext->getCustomer()->getDefaultBillingAddressId(), $possibleBillingIdstrue)) {
  58.                         $saleChannelContext->getCustomer()->setDefaultBillingAddressId($newDefaultBillingAddressId);
  59.                         $saleChannelContext->getCustomer()->setDefaultBillingAddress($employee->getAddresses()->get($newDefaultBillingAddressId));
  60.                     }
  61.                     if ($saleChannelContext->getCustomer()->getActiveBillingAddress() === null
  62.                         || !in_array($saleChannelContext->getCustomer()->getActiveBillingAddress()->getId(), $possibleBillingIdstrue)) {
  63.                         $saleChannelContext->getCustomer()->setActiveBillingAddress($employee->getAddresses()->get($newDefaultBillingAddressId));
  64.                     }
  65.                 }
  66.             }
  67.             // reset default billing address in Context
  68.             if ($employee->getAddresses()
  69.                 && $employee->getAddresses()->count() > 0
  70.             ) {
  71.                 $possibleShippingIds $employee->getAddresses()->filter(
  72.                     function (CustomerAddressEntity $address) {
  73.                         /** @var EmployeeAddressEntity $relation */
  74.                         $relation $address->getExtensionOfType('relationData'EmployeeAddressEntity::class);
  75.                         return $relation->isAsShippingAddress();
  76.                     })->getIds();
  77.                 if (!empty($possibleShippingIds)) {
  78.                     $newDefaultShippingAddressId array_shift($possibleShippingIds);
  79.                 } elseif (!empty($newDefaultBillingAddressId)) {
  80.                     $newDefaultShippingAddressId $newDefaultBillingAddressId;
  81.                 }
  82.                 if (!empty($newDefaultShippingAddressId)) {
  83.                     if (!in_array($saleChannelContext->getCustomer()->getDefaultShippingAddressId(), $possibleShippingIdstrue)) {
  84.                         $saleChannelContext->getCustomer()->setDefaultShippingAddressId($newDefaultShippingAddressId);
  85.                         $saleChannelContext->getCustomer()->setDefaultShippingAddress($employee->getAddresses()->get($newDefaultShippingAddressId));
  86.                     }
  87.                     if ($saleChannelContext->getCustomer()->getActiveShippingAddress() === null
  88.                         || !in_array($saleChannelContext->getCustomer()->getActiveShippingAddress()->getId(), $possibleShippingIdstrue)) {
  89.                         $saleChannelContext->getCustomer()->setActiveShippingAddress($employee->getAddresses()->get($newDefaultShippingAddressId));
  90.                     }
  91.                     // validate shipping Location
  92.                     $shippingLocation $saleChannelContext->getShippingLocation();
  93.                     if (!in_array($shippingLocation->getAddress()->getId(), $possibleShippingIdstrue)
  94.                     ) {
  95.                         $newShippingLocation ShippingLocation::createFromAddress($employee->getAddresses()->get($newDefaultShippingAddressId));
  96.                         // create new SalesChannelContext with new shipping location
  97.                         $newSaleChannelContext = new SalesChannelContext(
  98.                             $saleChannelContext->getContext(),
  99.                             $saleChannelContext->getToken(),
  100.                             $saleChannelContext->getDomainId(),
  101.                             $saleChannelContext->getSalesChannel(),
  102.                             $saleChannelContext->getCurrency(),
  103.                             $saleChannelContext->getCurrentCustomerGroup(),
  104.                             $saleChannelContext->getFallbackCustomerGroup(),
  105.                             $saleChannelContext->getTaxRules(),
  106.                             $saleChannelContext->getPaymentMethod(),
  107.                             $saleChannelContext->getShippingMethod(),
  108.                             $newShippingLocation,
  109.                             $saleChannelContext->getCustomer(),
  110.                             $saleChannelContext->getItemRounding(),
  111.                             $saleChannelContext->getTotalRounding(),
  112.                             $saleChannelContext->getRuleIds()
  113.                         );
  114.                         $newSaleChannelContext->addExtensions($saleChannelContext->getExtensions());
  115.                         $saleChannelContext $newSaleChannelContext;
  116.                     }
  117.                 }
  118.             }
  119.         }
  120.         return $saleChannelContext;
  121.     }
  122.     /**
  123.      * @param array $options
  124.      * @param SalesChannelContext $context
  125.      * @return EmployeeEntity|null
  126.      */
  127.     protected function loadEmployee(array $optionsSalesChannelContext $context): ?EmployeeEntity
  128.     {
  129.         if (!array_key_exists(self::EMPLOYEE_ID$options)) {
  130.             return null;
  131.         }
  132.         $employeeId $options[self::EMPLOYEE_ID];
  133.         return $this->employeeService->getById($employeeId$context);
  134.     }
  135. }