custom/plugins/shopware6-sapoci/src/Subscriber/CartChangedSubscriber.php line 58

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