custom/plugins/shopware6-sapoci/src/Subscriber/SalesChannelContextSwitchSubscriber.php line 54

Open in your IDE?
  1. <?php
  2. namespace Orcamultimedia\OciPunchout\Subscriber;
  3. use Orcamultimedia\OciPunchout\Checkout\Cart\PunchoutLineItemFactory;
  4. use Orcamultimedia\OciPunchout\Service\OciService;
  5. use Orcamultimedia\OciPunchout\Struct\OciCartData;
  6. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  7. use Shopware\Core\Checkout\Cart\Price\Struct\CalculatedPrice;
  8. use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
  9. use Shopware\Core\Checkout\Cart\Tax\Struct\CalculatedTaxCollection;
  10. use Shopware\Core\Checkout\Cart\Tax\Struct\TaxRuleCollection;
  11. use Shopware\Core\System\SalesChannel\Context\AbstractSalesChannelContextFactory;
  12. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextPersister;
  13. use Shopware\Core\System\SalesChannel\Event\SalesChannelContextSwitchEvent;
  14. use Shopware\Core\System\SystemConfig\SystemConfigService;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Contracts\Translation\TranslatorInterface;
  17. class SalesChannelContextSwitchSubscriber implements EventSubscriberInterface
  18. {
  19.     private CartService $cartService;
  20.     private SalesChannelContextPersister $contextPersister;
  21.     private AbstractSalesChannelContextFactory $factory;
  22.     private OciService $ociService;
  23.     private SystemConfigService $systemConfigService;
  24.     private TranslatorInterface $translator;
  25.     public function __construct(
  26.         CartService $cartService,
  27.         SalesChannelContextPersister $contextPersister,
  28.         AbstractSalesChannelContextFactory $factory,
  29.         OciService $ociService,
  30.         SystemConfigService $systemConfigService,
  31.         TranslatorInterface $translator
  32.     ) {
  33.         $this->cartService $cartService;
  34.         $this->contextPersister $contextPersister;
  35.         $this->factory $factory;
  36.         $this->ociService $ociService;
  37.         $this->systemConfigService $systemConfigService;
  38.         $this->translator $translator;
  39.     }
  40.     public static function getSubscribedEvents(): array
  41.     {
  42.         return [
  43.             SalesChannelContextSwitchEvent::class => 'onSalesChannelContextSwitch'
  44.         ];
  45.     }
  46.     public function onSalesChannelContextSwitch(SalesChannelContextSwitchEvent $event): void
  47.     {
  48.         $context $event->getSalesChannelContext();
  49.         $cart $this->cartService->getCart($context->getToken(), $context);
  50.         if (
  51.             !$this->ociService->isPunchout()
  52.             || !$this->systemConfigService->get(
  53.                 'OciPunchout.config.addShippingCostsAtPunchout',
  54.                 $context->getSalesChannel()->getId()
  55.             )
  56.             || $cart->getLineItems()->count() === 0
  57.             || empty($event->getRequestDataBag()->get('shippingMethodId'))
  58.         ) {
  59.             return;
  60.         }
  61.         try {
  62.             $session $this->contextPersister->load($context->getToken(), $context->getSalesChannel()->getId());
  63.             $newContext $this->factory->create($context->getToken(), $context->getSalesChannel()->getId(), $session);
  64.         } catch (\Exception $e) {
  65.             return;
  66.         }
  67.         $newCart $this->cartService->getCart($newContext->getToken(), $newContext);
  68.         $newCart $this->cartService->recalculate($newCart$newContext);
  69.         if ($cart->has('shipping-costs')){
  70.             $cart->remove('shipping-costs');
  71.         }
  72.         $extensionData = new OciCartData();
  73.         $extensionData->assign([
  74.             'matgroup' => $this->systemConfigService->get(
  75.                 'OciPunchout.config.shippingCostsMatgroup',
  76.                 $context->getSalesChannel()->getId()
  77.             ),
  78.             'unit' => 'PCE'
  79.         ]);
  80.         // Adding leadtime
  81.         if ($this->systemConfigService->get('OciPunchout.config.isSubmitMaximumLeadTimeForShippingCosts',
  82.                                         $context->getSalesChannel()->getId())) {
  83.             $extensionData->assign([
  84.                 'leadtime' => $this->systemConfigService->get(
  85.                     'OciPunchout.config.maximumLeadTime',
  86.                     $context->getSalesChannel()->getId()
  87.                 )
  88.             ]);
  89.         }
  90.         // Adding extschematype
  91.         if ($this->systemConfigService->get('OciPunchout.config.isSubmitShippingCostsExtschematype',
  92.             $context->getSalesChannel()->getId())) {
  93.             $extensionData->assign([
  94.                 'extschematype' => $this->systemConfigService->get(
  95.                     'OciPunchout.config.extschematype',
  96.                     $context->getSalesChannel()->getId()
  97.                 )
  98.             ]);
  99.         }
  100.         // Adding extcategoryid
  101.         if ($this->systemConfigService->get('OciPunchout.config.shippingCostsExtcategoryid',
  102.             $context->getSalesChannel()->getId())) {
  103.             $extensionData->assign([
  104.                 'extcategoryid' => $this->systemConfigService->get(
  105.                     'OciPunchout.config.extcategoryid',
  106.                     $context->getSalesChannel()->getId()
  107.                 )
  108.             ]);
  109.         }
  110.         // Adding Customfield
  111.         if (strcasecmp($this->systemConfigService->get('OciPunchout.config.isSubmitShippingCostsCustomfield',
  112.             $context->getSalesChannel()->getId()), 'default') !== 0) {
  113.             $extensionData->assign([
  114.                 'extcategoryid' => $this->systemConfigService->get(
  115.                     'OciPunchout.config.' $this->systemConfigService->get('OciPunchout.config.isSubmitShippingCostsCustomfield',
  116.                 $context->getSalesChannel()->getId()),
  117.                     $context->getSalesChannel()->getId()
  118.                 )
  119.             ]);
  120.         }
  121.         $lineItem = (new LineItem('shipping-costs'PunchoutLineItemFactory::TYPE'shipping-costs'1))
  122.             ->setLabel($this->translator->trans('orca-ocipunchout.shippingCosts', ['%type%' =>
  123.                 $newContext->getShippingMethod()->getName()]))
  124.             ->setRemovable(true)
  125.             ->setPayloadValue('productNumber''shipping-costs')
  126.             ->setPrice(
  127.                 new CalculatedPrice(
  128.                     $newCart->getDeliveries()->getShippingCosts()->sum()->getUnitPrice(),
  129.                     $newCart->getDeliveries()->getShippingCosts()->sum()->getTotalPrice(),
  130.                     new CalculatedTaxCollection(),
  131.                     new TaxRuleCollection()
  132.                 )
  133.             );
  134.         $alternativeCaptionShippingCosts $this->systemConfigService->get(
  135.             'OciPunchout.config.alternativeCaptionOfShippingCosts',
  136.             $context->getSalesChannel()->getId()
  137.         );
  138.         if (strlen($alternativeCaptionShippingCosts) > 0) {
  139.             $alternativeCaptionShippingCosts trim($alternativeCaptionShippingCosts);
  140.             $lineItem->setLabel($alternativeCaptionShippingCosts);
  141.         }
  142.         $lineItem->addExtension(OciCartData::EXTENSION_NAME$extensionData);
  143.         $cart->add($lineItem);
  144.         $cart->markModified();
  145.         $this->cartService->recalculate($cart$context);
  146.     }
  147. }