custom/plugins/TwigelSportPlugins/src/Subscriber/BeforeLineItemAddedSubscriber.php line 92

Open in your IDE?
  1. <?php
  2. namespace Twigel\SportPlugin\Subscriber;
  3. use Shopware\Core\Checkout\Cart\Event\BeforeLineItemAddedEvent;
  4. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  5. use Shopware\Core\Checkout\Cart\LineItem\LineItemCollection;
  6. use Shopware\Core\Checkout\Cart\LineItemFactoryRegistry;
  7. use Shopware\Core\Checkout\Cart\Price\Struct\CalculatedPrice;
  8. use Shopware\Core\Checkout\Cart\Tax\Struct\CalculatedTaxCollection;
  9. use Shopware\Core\Checkout\Cart\Tax\Struct\TaxRuleCollection;
  10. use Shopware\Core\Checkout\Promotion\Cart\PromotionItemBuilder;
  11. use Shopware\Core\Checkout\Promotion\PromotionEntity;
  12. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  14. use Shopware\Core\Framework\Uuid\Uuid;
  15. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
  16. use Shopware\Core\System\SystemConfig\SystemConfigService;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. use Symfony\Component\HttpFoundation\RequestStack;
  19. class BeforeLineItemAddedSubscriber implements EventSubscriberInterface
  20. {
  21.     /**
  22.      * @var RequestStack
  23.      */
  24.     private RequestStack $requestStack;
  25.     /**
  26.      * @var LineItemFactoryRegistry
  27.      */
  28.     private LineItemFactoryRegistry $factory;
  29.     /**
  30.      * @var PromotionItemBuilder
  31.      */
  32.     private PromotionItemBuilder $promotionItemBuilder;
  33.     /**
  34.      * @var SystemConfigService
  35.      */
  36.     private SystemConfigService $systemConfigService;
  37.     /**
  38.      * @var SalesChannelRepositoryInterface $productRepository
  39.      */
  40.     private SalesChannelRepositoryInterface $productRepository;
  41.     /**
  42.      * @var EntityRepositoryInterface $promotionRepository
  43.      */
  44.     private EntityRepositoryInterface $promotionRepository;
  45.     /**
  46.      * @param RequestStack $requestStack
  47.      * @param LineItemFactoryRegistry $factory
  48.      * @param PromotionItemBuilder $promotionItemBuilder
  49.      * @param SystemConfigService $systemConfigService
  50.      * @param SalesChannelRepositoryInterface $productRepository
  51.      * @param EntityRepositoryInterface $promotionRepository
  52.      */
  53.     public function __construct(
  54.         RequestStack                    $requestStack,
  55.         LineItemFactoryRegistry         $factory,
  56.         PromotionItemBuilder            $promotionItemBuilder,
  57.         SystemConfigService             $systemConfigService,
  58.         SalesChannelRepositoryInterface $productRepository,
  59.         EntityRepositoryInterface       $promotionRepository
  60.     )
  61.     {
  62.         $this->requestStack $requestStack;
  63.         $this->factory $factory;
  64.         $this->promotionItemBuilder $promotionItemBuilder;
  65.         $this->systemConfigService $systemConfigService;
  66.         $this->productRepository $productRepository;
  67.         $this->promotionRepository $promotionRepository;
  68.     }
  69.     public static function getSubscribedEvents(): array
  70.     {
  71.         return [
  72.             BeforeLineItemAddedEvent::class => 'onLineItemAdded',
  73.         ];
  74.     }
  75.     /**
  76.      * @param BeforeLineItemAddedEvent $event
  77.      */
  78.     public function onLineItemAdded(BeforeLineItemAddedEvent $event)
  79.     {
  80.         $productSet $this->requestStack->getCurrentRequest()->get('productSet');
  81.         $productSet json_decode($productSet);
  82.         if (is_null($productSet) || empty($productSet)) {
  83.             return;
  84.         }
  85.         $pluginConfig $this->systemConfigService->get('TwigelSportPlugin.config');
  86.         if (is_null($pluginConfig['setPromotionFull']) && is_null($pluginConfig['setPromotionPartial'])) {
  87.             return;
  88.         }
  89.         $full $this->requestStack->getCurrentRequest()->get('productSetFull');
  90.         if ($full == 'true'){
  91.             $setPromotion $pluginConfig['setPromotionFull'];
  92.         }else{
  93.             $setPromotion $pluginConfig['setPromotionPartial'];
  94.         }
  95.         $lineItems = [];
  96.         $total 0;
  97.         foreach ($productSet as $set) {
  98.             $product $this->productRepository->search(new Criteria([$set]), $event->getSalesChannelContext())->get($set);
  99.             if (is_null($product)) {
  100.                 return;
  101.             }
  102.             $params = [
  103.                 'type' => LineItem::PRODUCT_LINE_ITEM_TYPE,
  104.                 'referencedId' => $set,
  105.                 'quantity' => 1
  106.             ];
  107.             $calcPrice = new CalculatedPrice(
  108.                 $product->getPrice()->get($event->getSalesChannelContext()->getCurrencyId())->getGross(),
  109.                 $product->getPrice()->get($event->getSalesChannelContext()->getCurrencyId())->getGross() * (1),
  110.                 $product->calculatedPrice->getCalculatedTaxes(),
  111.                 $product->calculatedPrice->getTaxRules(),
  112.                 1
  113.             );
  114.             $total += $calcPrice->getTotalPrice();
  115.             $lineItem $this->factory->create($params$event->getSalesChannelContext());
  116.             $lineItem->setRemovable(true);
  117.             $lineItem->setStackable(true);
  118.             $lineItem->setPayloadValue('productSet''true');
  119.             $lineItem->setPrice($calcPrice);
  120.             $lineItems[] = $lineItem;
  121.         }
  122.         $criteria = new Criteria([$setPromotion]);
  123.         $criteria->addAssociation('discounts');
  124.         /** @var PromotionEntity $promotion */
  125.         $promotion $this->promotionRepository->search($criteria$event->getContext())->get($setPromotion);
  126.         if (!empty($promotion)) {
  127.             $discount $promotion->getDiscounts()->first();
  128.             if (!empty($discount) && $promotion->isActive()) {
  129.                 $promotionLineItem $this->promotionItemBuilder->buildDiscountLineItem(
  130.                     $promotion->getCode(),
  131.                     $promotion,
  132.                     $discount,
  133.                     $event->getSalesChannelContext()->getCurrencyId()
  134.                 );
  135.                 switch ($promotionLineItem->getPayloadValue('discountType')) {
  136.                     case 'fixed_unit':
  137.                         $promotionValue $promotionLineItem->getPayloadValue('value');
  138.                         break;
  139.                     default:
  140.                         $promotionValue $total 100 $promotionLineItem->getPayloadValue('value');
  141.                 }
  142.                 $promotionValue = -$promotionValue;
  143.                 $promotionLineItem->setPrice(
  144.                     new CalculatedPrice(
  145.                         $promotionValue,
  146.                         $promotionValue,
  147.                         new CalculatedTaxCollection(),
  148.                         new TaxRuleCollection()
  149.                     )
  150.                 );
  151.                 $promotionLineItem->setId(Uuid::randomHex());
  152.                 $promotionLineItem->setRemovable(true);
  153.                 $promotionLineItem->setStackable(false);
  154.                 $lineItems[] = $promotionLineItem;
  155.             }
  156.         }
  157.         foreach ($event->getCart()->getLineItems() as $lineItem){
  158.             $lineItems[] = $lineItem;
  159.         }
  160.         $lineItems = new LineItemCollection($lineItems);
  161.         $event->getCart()->setLineItems($lineItems);
  162.         $event->getCart()->markModified();
  163.     }
  164. }