custom/static-plugins/MadBodymedBundle/src/Subscriber/CartSubscriber.php line 72

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace MadBodymedBundle\Subscriber;
  4. use Shopware\Core\Checkout\Cart\Order\CartConvertedEvent;
  5. use Shopware\Core\Checkout\Order\OrderEvents;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Shopware\Core\Checkout\Cart\Event\BeforeLineItemAddedEvent;
  9. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  12. class CartSubscriber implements EventSubscriberInterface
  13. {
  14.     private SalesChannelRepositoryInterface $salesChannelProductRepository;
  15.     public function __construct(SalesChannelRepositoryInterface $salesChannelProductRepository)
  16.     {
  17.         $this->salesChannelProductRepository $salesChannelProductRepository;
  18.     }
  19.     public static function getSubscribedEvents(): array
  20.     {
  21.         return [
  22.             #AfterLineItemAddedEvent::class => 'onAfterLineItemAdded',
  23.             CartConvertedEvent::class => 'onCartConverted',
  24.             #BeforeLineItemAddedEvent::class => 'onLineItemAdded',
  25.             OrderEvents::ORDER_LINE_ITEM_LOADED_EVENT => 'lineItemLoaded',
  26.         ];
  27.     }
  28.     public function onLineItemAdded(BeforeLineItemAddedEvent $event//Zum Prüfen ob der Bundle Artikel vollständig konfiguriert wurde, bevor er in den Warenkorb gelegt wurde.
  29.     {
  30.         $lineItems $event->getCart()->getLineItems();
  31.         foreach($lineItems->getElements() as $key => $lineItem){
  32.             //Prüfen ob es ein Bundle ist
  33.             if($lineItem->getPayload()['isBundle'] === true){
  34.                 foreach($lineItem->getChildren() as $lineItemChild){
  35.                     dump($lineItemChild->getReferencedId());
  36.                     $criteria = (new Criteria())
  37.                         ->addAssociation('product')
  38.                         ->addFilter(new EqualsFilter('id'$lineItemChild->getReferencedId()))
  39.                     ;
  40.                     $article $this->salesChannelProductRepository->search($criteria$event->getSalesChannelContext())->first();
  41.                     //Prüfen ob es ein Variantenartikel ist, der keinen Parent hat.
  42.                     if($article->getChildCount() > and $article->getParentId() == null){
  43.                        return;
  44.                     }
  45.                 }
  46.             }
  47.         }
  48.     }
  49.     public function lineItemLoaded(EntityLoadedEvent $event){
  50.         $lineitems $event->getEntities();
  51.         usort($lineitems, function($a$b) {
  52.                 return $a->position <=> $b->position;
  53.         });
  54.     }
  55.     public function onCartConverted(CartConvertedEvent $event)
  56.     {
  57.         $convertedCart $event->getConvertedCart();
  58.         $newLineItems = [];
  59.         foreach ($convertedCart['lineItems'] as $key => $lineItem) {
  60.             if (isset($lineItem['payload']['isBundle']) && \boolval($lineItem['payload']['isBundle']) === true) {
  61.                 $lineItem['bundle'] = 1;
  62.                 /*if (isset($lineItem['payload']['bundlePriceType'])) {
  63.                     $lineItem['bundlePriceType'] = $lineItem['payload']['bundlePriceType'];
  64.                 }
  65.                 if (isset($lineItem['payload']['nomsBundleType'])) {
  66.                     $lineItem['nomsBundleType'] = $lineItem['payload']['nomsBundleType'];
  67.                 }*/
  68.             }
  69.             $newLineItems[] = $lineItem;
  70.         }
  71.         $convertedCart['lineItems'] = $newLineItems;
  72.         $event->setConvertedCart($convertedCart);
  73.     }
  74. }