custom/plugins/CogiFreeProducts/src/Subscriber/FreeProductsSubscriber.php line 135

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Cogi\FreeProducts\Subscriber;
  3. use Cogi\FreeProducts\Core\FreeProducts\FreeProductsCollection;
  4. use Cogi\FreeProducts\Core\FreeProducts\FreeProductsEntity;
  5. use Cogi\FreeProducts\Helper\CheckFreeProduct;
  6. use Shopware\Core\Checkout\Cart\Event\BeforeLineItemRemovedEvent;
  7. use Shopware\Core\Checkout\Cart\Order\OrderConvertedEvent;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\RangeFilter;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  13. use Shopware\Core\Framework\Struct\ArrayEntity;
  14. use Shopware\Core\Framework\Struct\ArrayStruct;
  15. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  16. use Shopware\Core\System\SystemConfig\SystemConfigService;
  17. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
  18. use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. class FreeProductsSubscriber implements EventSubscriberInterface
  21. {
  22.     /**
  23.      * @var EntityRepositoryInterface $freeProductsRepository
  24.      */
  25.     private $freeProductsRepository;
  26.     /**
  27.      * @var EntityRepositoryInterface $customerRepository
  28.      */
  29.     private $customerRepository;
  30.     /**
  31.      * @var SystemConfigService
  32.      */
  33.     private $systemConfigService;
  34.     /** @var CheckFreeProduct $helperCheckFreeProduct */
  35.     private $helperCheckFreeProduct;
  36.     private $config;
  37.     /**
  38.      * @param EntityRepositoryInterface $freeProductsRepository
  39.      * @param EntityRepositoryInterface $customerRepository
  40.      * @param SystemConfigService $systemConfigService
  41.      * @param CheckFreeProduct $helperCheckFreeProduct
  42.      */
  43.     public function __construct(
  44.         EntityRepositoryInterface $freeProductsRepository,
  45.         EntityRepositoryInterface $customerRepository,
  46.         SystemConfigService $systemConfigService,
  47.         CheckFreeProduct $helperCheckFreeProduct
  48.     )
  49.     {
  50.         $this->freeProductsRepository $freeProductsRepository;
  51.         $this->customerRepository $customerRepository;
  52.         $this->systemConfigService $systemConfigService;
  53.         $this->helperCheckFreeProduct $helperCheckFreeProduct;
  54.     }
  55.     public static function getSubscribedEvents(): array
  56.     {
  57.         return [
  58.             CheckoutCartPageLoadedEvent::class => 'onCheckoutCartPageLoadedEvent',
  59.             OffcanvasCartPageLoadedEvent::class => 'onOffcanvasCartPageLoadedEvent',
  60.             OrderConvertedEvent::class => 'OnOrderConvertedEvent',
  61.             BeforeLineItemRemovedEvent::class => 'OnBeforeLineItemRemoved'
  62.         ];
  63.     }
  64.     public function OnOrderConvertedEvent(OrderConvertedEvent $event)
  65.     {
  66.         $event->getConvertedCart()->addExtension("FreeProductConverted", new ArrayStruct([]));
  67.     }
  68.     public function OnBeforeLineItemRemoved(BeforeLineItemRemovedEvent $event){
  69.         $config $this->systemConfigService->get('CogiFreeProducts.config'$event->getSalesChannelContext()->getSalesChannelId());
  70.         if(!$config['cogiFreeProductsAutoToCart']){
  71.             return;
  72.         }
  73.         if(!empty($event->getLineItem()->getPayloadValue('FreeProduct'))){
  74.             $arrayStruct null;
  75.             if(!empty($event->getCart()->getExtension("FreeProductsRemoved"))){
  76.                 $extension $event->getCart()->getExtension("FreeProductsRemoved")->getVars();
  77.                 array_push($extension$event->getLineItem()->getReferencedId());
  78.                 $extension array_unique($extension);
  79.                 $arrayStruct = new ArrayStruct([...$extension]);
  80.             }else {
  81.                 $arrayStruct = new ArrayStruct([$event->getLineItem()->getReferencedId()]);
  82.             }
  83.             $event->getCart()->addExtension("FreeProductsRemoved"$arrayStruct);
  84.         }
  85.     }
  86.     public function onCheckoutCartPageLoadedEvent(CheckoutCartPageLoadedEvent $event)
  87.     {
  88.         $config $this->systemConfigService->get('CogiFreeProducts.config'$event->getSalesChannelContext()->getSalesChannelId());
  89.         $isCustomerGroup $this->checkCustomerGroup($event->getSalesChannelContext(), $config'cogiFreeProductsCustomerGroupIds');
  90.         if($isCustomerGroup) {
  91.             $criteria = new Criteria();
  92.             $criteria->addFilter(new EqualsFilter("isActive"1));
  93.             $criteria->addFilter(new EqualsFilter("product.active"1));
  94.             $criteria->addAssociation('product');
  95.             $criteria->addAssociation('productManufacturer');
  96.             $criteria->addAssociation('product.options');
  97.             if($config['cogiFreeProductsClearanceSale']){
  98.                 $criteria->addFilter(new RangeFilter('product.stock', ['gt' => 0]),);
  99.             }
  100.             $criteria->addSorting(new FieldSorting("cartPrice"FieldSorting::ASCENDING));
  101.             $freeProducts $this->freeProductsRepository->search($criteria$event->getContext())->getEntities();
  102.             $freeProductCollection = new FreeProductsCollection();
  103.             /** @var FreeProductsEntity $freeProduct */
  104.             foreach($freeProducts as $freeProduct) {
  105.                 if ($this->helperCheckFreeProduct->checkFreeProductIsTimed($event->getContext(), $freeProduct->getId())) {
  106.                     $freeProductCollection->add($freeProduct);
  107.                 }
  108.             }
  109.             $event->getPage()->addExtension("CogiFreeProducts", new ArrayEntity([
  110.                 'FreeProducts' => $freeProductCollection
  111.             ]));
  112.         }
  113.     }
  114.     public function onOffcanvasCartPageLoadedEvent(OffcanvasCartPageLoadedEvent $event)
  115.     {
  116.         $config $this->systemConfigService->get('CogiFreeProducts.config'$event->getSalesChannelContext()->getSalesChannelId());
  117.         $isCustomerGroup $this->checkCustomerGroup($event->getSalesChannelContext(), $config'cogiFreeProductsCustomerGroupIds');
  118.         if($isCustomerGroup){
  119.             $criteria = new Criteria();
  120.             $criteria->addFilter(new EqualsFilter("isActive"1));
  121.             $criteria->addFilter(new EqualsFilter("product.active"1));
  122.             $criteria->addAssociation('product');
  123.             $criteria->addAssociation('productManufacturer');
  124.             $criteria->addAssociation('product.options');
  125.             if($config['cogiFreeProductsClearanceSale']){
  126.                 $criteria->addFilter(new RangeFilter('product.stock', ['gt' => 0]),);
  127.             }
  128.             $criteria->addSorting(new FieldSorting("cartPrice"FieldSorting::ASCENDING));
  129.             $freeProducts $this->freeProductsRepository->search($criteria$event->getContext())->getEntities();
  130.             $freeProductCollection = new FreeProductsCollection();
  131.             /** @var FreeProductsEntity $freeProduct */
  132.             foreach($freeProducts as $freeProduct) {
  133.                 if ($this->helperCheckFreeProduct->checkFreeProductIsTimed($event->getContext(), $freeProduct->getId())) {
  134.                     $freeProductCollection->add($freeProduct);
  135.                 }
  136.             }
  137.             $event->getPage()->addExtension("CogiFreeProducts", new ArrayEntity([
  138.                 'FreeProducts' => $freeProductCollection
  139.             ]));
  140.         }
  141.     }
  142.     private function checkCustomerGroup(SalesChannelContext $context, Array $configString $configKey){
  143.         if(array_key_exists($configKey$config)){
  144.             $configCustomerGroupIds $config[$configKey];
  145.         } else {
  146.             $configCustomerGroupIds = [];
  147.         }
  148.         $isCustomerGroup false;
  149.         if(empty($configCustomerGroupIds)){
  150.             $isCustomerGroup true;
  151.         }
  152.         if(!$isCustomerGroup && $context->getCustomer()){
  153.             foreach($configCustomerGroupIds as $groupId){
  154.                 if($groupId === $context->getCustomer()->getGroupId()){
  155.                     $isCustomerGroup true;
  156.                 }
  157.             }
  158.         }
  159.         return $isCustomerGroup;
  160.     }
  161. }