custom/plugins/TwigelSportLabel/src/Subscriber/BeforeLineItemSubscriber.php line 51

Open in your IDE?
  1. <?php
  2. namespace Twigel\SportLabel\Subscriber;
  3. use Shopware\Core\Checkout\Cart\Event\BeforeLineItemAddedEvent;
  4. use Shopware\Core\Checkout\Cart\LineItem\LineItemCollection;
  5. use Shopware\Core\Checkout\Cart\Price\Struct\CalculatedPrice;
  6. use Shopware\Core\Checkout\Cart\Tax\Struct\CalculatedTaxCollection;
  7. use Shopware\Core\Checkout\Cart\Tax\Struct\TaxRuleCollection;
  8. use Shopware\Core\Checkout\Promotion\Cart\PromotionItemBuilder;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class BeforeLineItemSubscriber implements EventSubscriberInterface
  13. {
  14.     /**
  15.      * @var EntityRepositoryInterface
  16.      */
  17.     private EntityRepositoryInterface $groupRepository;
  18.     /**
  19.      * @var EntityRepositoryInterface
  20.      */
  21.     private EntityRepositoryInterface $promotionRepository;
  22.     /**
  23.      * @var PromotionItemBuilder
  24.      */
  25.     private PromotionItemBuilder $promotionItemBuilder;
  26.     public function __construct(
  27.         EntityRepositoryInterface $groupRepository,
  28.         EntityRepositoryInterface $promotionRepository,
  29.         PromotionItemBuilder  $promotionItemBuilder
  30.     )
  31.     {
  32.         $this->groupRepository $groupRepository;
  33.         $this->promotionRepository $promotionRepository;
  34.         $this->promotionItemBuilder $promotionItemBuilder;
  35.     }
  36.     public static function getSubscribedEvents(): array
  37.     {
  38.         return [
  39.             BeforeLineItemAddedEvent::class => 'lineItem'
  40.         ];
  41.     }
  42.     public function lineItem(BeforeLineItemAddedEvent $event)
  43.     {
  44.         if ($event->getSalesChannelContext()->getCustomer()) {
  45.             $groupId $event->getSalesChannelContext()->getCustomer()->getGroupId();
  46.             $criteria = new Criteria([$groupId]);
  47.             $group $this->groupRepository->search($criteria$event->getContext())->first();
  48.             if ($group->getCustomFields() && array_key_exists('twigel_customer_group_discount'$group->getCustomFields()) && $group->getCustomFields()['twigel_customer_group_discount']) {
  49.                 $promotionId $group->getCustomFields()['twigel_customer_group_discount'];
  50.                 $criteria = new Criteria([$promotionId]);
  51.                 $criteria->addAssociations(['discounts']);
  52.                 $promotion $this->promotionRepository->search($criteria$event->getContext())->first();
  53.                 if (!$promotion) {
  54.                     return;
  55.                 }
  56.                 $total $event->getCart()->getPrice()->getTotalPrice();
  57.                 $discount $promotion->getDiscounts()->first();
  58.                 if (!empty($discount) && $promotion->isActive()) {
  59.                     $promotionLineItem $this->promotionItemBuilder->buildDiscountLineItem(
  60.                         $promotion->getCode(),
  61.                         $promotion,
  62.                         $discount,
  63.                         $event->getSalesChannelContext()->getCurrencyId()
  64.                     );
  65.                     switch ($promotionLineItem->getPayloadValue('discountType')) {
  66.                         case 'fixed_unit':
  67.                             $promotionValue $promotionLineItem->getPayloadValue('value');
  68.                             break;
  69.                         default:
  70.                             $promotionValue $total 100 $promotionLineItem->getPayloadValue('value');
  71.                     }
  72.                     $promotionValue = -$promotionValue;
  73.                     $promotionLineItem->setPrice(
  74.                         new CalculatedPrice(
  75.                             $promotionValue,
  76.                             $promotionValue,
  77.                             new CalculatedTaxCollection(),
  78.                             new TaxRuleCollection()
  79.                         )
  80.                     );
  81.                     $promotionLineItem->setStackable(true);
  82.                     $cartPromotionId '';
  83.                     foreach ($event->getCart()->getLineItems()->getElements() as $lineItem){
  84.                         if ($lineItem->getType() === 'promotion'){
  85.                             $cartPromotionId $lineItem->getId();
  86.                         }
  87.                     }
  88.                     if ($cartPromotionId !== $promotionLineItem->getId()){
  89.                         $event->getCart()->addLineItems(new LineItemCollection([$promotionLineItem]));
  90.                     }
  91.                 }
  92.             }
  93.         }
  94.     }
  95. }