custom/plugins/NimbitsPriceOnRequestNext/src/Subscriber/StorefrontSubscriber.php line 71

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Nimbits\NimbitsPriceOnRequestNext\Subscriber;
  3. use Nimbits\NimbitsPriceOnRequestNext\Service\PriceOnRequestService;
  4. use Nimbits\NimbitsPriceOnRequestNext\Setting\Service\SettingService;
  5. use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
  6. use Shopware\Core\Content\Product\SalesChannel\Detail\AbstractProductDetailRoute;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  11. use Shopware\Core\System\SalesChannel\Entity\SalesChannelEntityLoadedEvent;
  12. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
  13. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  14. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
  15. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  16. use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
  17. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  18. use Shopware\Storefront\Pagelet\Footer\FooterPageletLoadedEvent;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. use Symfony\Component\HttpFoundation\RedirectResponse;
  21. use Symfony\Component\HttpFoundation\Request;
  22. use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
  23. use Symfony\Component\HttpKernel\KernelEvents;
  24. //use Symfony\Component\DependencyInjection\ContainerBuilder;
  25. class StorefrontSubscriber implements EventSubscriberInterface
  26. {
  27.     private SettingService $settingsService;
  28.     private CartService $cartService;
  29.     private $productRepository;
  30.     private $productDetailRoute;
  31.     private PriceOnRequestService $priceOnRequestService;
  32.     private SalesChannelRepositoryInterface $salesChannelProductRepository;
  33.     public function __construct(
  34.         SettingService                  $settingsService,
  35.         CartService                     $cartService,
  36.         EntityRepositoryInterface       $productRepository,
  37.         AbstractProductDetailRoute      $productDetailRoute,
  38.         PriceOnRequestService           $priceOnRequestService,
  39.         SalesChannelRepositoryInterface $salesChannelProductRepository
  40.     )
  41.     {
  42.         $this->settingsService $settingsService;
  43.         $this->cartService $cartService;
  44.         $this->productRepository $productRepository;
  45.         $this->productDetailRoute $productDetailRoute;
  46.         $this->priceOnRequestService $priceOnRequestService;
  47.         $this->salesChannelProductRepository $salesChannelProductRepository;
  48.     }
  49.     public static function getSubscribedEvents(): array
  50.     {
  51.         // Return the events to listen to as array like this:  <event to listen to> => <method to execute>
  52.         return [
  53.             KernelEvents::CONTROLLER_ARGUMENTS => 'beforeController',
  54.             ProductPageLoadedEvent::class => 'onProductPageLoaded',
  55.             'sales_channel.product.loaded' => 'productsLoaded',
  56.             OffcanvasCartPageLoadedEvent::class => 'onOffcanvasCartPageLoaded',
  57.             CheckoutCartPageLoadedEvent::class => 'onCheckoutCartPageLoaded',
  58.             CheckoutConfirmPageLoadedEvent::class => 'onCheckoutPageLoaded',
  59.             FooterPageletLoadedEvent::class => 'onFooterLoaded'
  60.         ];
  61.     }
  62.     public function beforeController(ControllerArgumentsEvent $event)
  63.     {
  64.         if ($event->getRequest()->getPathInfo() == '/checkout/product/add-by-number') {
  65.             /** @var $salesChannelContext SalesChannelContext */
  66.             $salesChannelContext $event->getArguments()[1];
  67.             $product $this->salesChannelProductRepository->search(
  68.                 (new Criteria())
  69.                     ->addFilter(new EqualsFilter('productNumber'$event->getRequest()->get('number'))),
  70.                 $salesChannelContext
  71.             );
  72.             if ($product->count() > 0) {
  73.                 if ($this->priceOnRequestService->checkForProductCheckoutDisabled($product->first(), $salesChannelContext->getCurrentCustomerGroup()->getName())) {
  74.                     $event->setController(function (Request $requestSalesChannelContext $salesChannelContext) {
  75.                         return new RedirectResponse($request->getUriForPath('/checkout/cart?nb_por_nbpn=1'));
  76.                     });
  77.                 }
  78.             }
  79.         }
  80.     }
  81.     public function onProductPageLoaded(ProductPageLoadedEvent $event)
  82.     {
  83.         $config $this->settingsService->getSettingsAsStruct($event->getSalesChannelContext()->getSalesChannel()->getId());
  84.         $product $event->getPage()->getProduct();
  85.         $productId $event->getPage()->getProduct()->getId();
  86.         $event->getPage()->getProduct()->addExtension('nimbitsPriceOnRequestSettings'$config);
  87.         $salesChannelContext $event->getSalesChannelContext();
  88.         $salesChannelId $event->getSalesChannelContext()->getSalesChannel()->getId();
  89.         $cg $event->getSalesChannelContext()->getCurrentCustomerGroup()->getName();
  90.         $priceOnRequestService $this->priceOnRequestService;
  91.         $priceOnRequestService->setSalesChannelId($salesChannelId);
  92.         $porRuleset $priceOnRequestService->getPriceOnRequestRules($product$cg);
  93.         $event->getPage()->getProduct()->addExtension('nimbits_por'$porRuleset);
  94.         $customer $salesChannelContext->getCustomer();
  95.         if ($customer != null) {
  96.             $event->getPage()->addExtension('nimbits_por_customer'$customer);
  97.         }
  98.     }
  99.     public function productsLoaded(SalesChannelEntityLoadedEvent $event): void
  100.     {
  101.         $config $this->settingsService->getSettingsAsStruct($event->getSalesChannelContext()->getSalesChannel()->getId());
  102.         $salesChannelContext $event->getSalesChannelContext();
  103.         $salesChannelId $event->getSalesChannelContext()->getSalesChannel()->getId();
  104.         $priceOnRequestService $this->priceOnRequestService;
  105.         $priceOnRequestService->setSalesChannelId($salesChannelId);
  106.         $products = [];
  107.         foreach ($event->getEntities() as $product) {
  108.             $products[] = $product;
  109.             $cg $event->getSalesChannelContext()->getCurrentCustomerGroup()->getName();
  110.             $porRuleset $priceOnRequestService->getPriceOnRequestRules($product$cg);
  111.             $product->addExtension('nimbits_por'$porRuleset);
  112.         }
  113.     }
  114.     public function onOffcanvasCartPageLoaded(OffcanvasCartPageLoadedEvent $event)
  115.     {
  116.         $salesChannelContext $event->getSalesChannelContext();
  117.         $salesChannelId $event->getSalesChannelContext()->getSalesChannel()->getId();
  118.         $cg $event->getSalesChannelContext()->getCurrentCustomerGroup()->getName();
  119.         $config $this->settingsService->getSettingsAsStruct($event->getSalesChannelContext()->getSalesChannel()->getId());
  120.         $priceOnRequestService $this->priceOnRequestService;
  121.         $priceOnRequestService->setSalesChannelId($salesChannelId);
  122.         $event->getPage()->addExtension('nimbitsPriceOnRequestSettings'$config);
  123.         $currentCart $this->cartService->getCart($salesChannelContext->getToken(), $salesChannelContext);
  124.         $lineitems $currentCart->getLineItems();
  125.         //iterate cart and apply price on request rules to each item and the cart
  126.         $products = [];
  127.         $productNumbers = [];
  128.         foreach ($lineitems as $item) {
  129.             $payload $item->getPayload();
  130.             if (!isset($payload["productNumber"])) {
  131.                 continue;
  132.             }
  133.             //xtract children's productNumbers if any
  134.             foreach ($item->getChildren() as $child) {
  135.                 $payloadChild $child->getPayload();
  136.                 if (!isset($payloadChild["productNumber"])) {
  137.                     continue;
  138.                 }
  139.                 $productNumbers[] = $payloadChild["productNumber"];
  140.             }
  141.             $productNumbers[] = $payload["productNumber"];
  142.         }
  143.         $prods $this->salesChannelProductRepository->search(
  144.             (new Criteria())
  145.                 ->addFilter(new EqualsAnyFilter('productNumber'$productNumbers)),
  146.             $salesChannelContext
  147.         );
  148.         $prods $prods->getElements();
  149.         //$product = $this->productDetailRoute->load($prods[$akeys[0]]->getId(), $event->getRequest(),$salesChannelContext, $criteria)->getProduct();
  150.         foreach ($prods as $p) {
  151.             $products[$p->getProductNumber()] = $p;
  152.         }
  153.         foreach ($lineitems as $item) {
  154.             $payload $item->getPayload();
  155.             $rulesets = [];
  156.             if (!isset($payload["productNumber"])) {
  157.                 continue;
  158.             }
  159.             foreach ($item->getChildren() as $child) {
  160.                 $payloadChild $child->getPayload();
  161.                 if (!isset($payloadChild["productNumber"])) {
  162.                     continue;
  163.                 }
  164.                 if (isset($products[$payloadChild["productNumber"]])) {
  165.                     $product $products[$payloadChild["productNumber"]];
  166.                     $porRuleset $priceOnRequestService->getPriceOnRequestRules($product$cg);
  167.                     $rulesets[] = $porRuleset;
  168.                 }
  169.             }
  170.             if (isset($products[$payload["productNumber"]])) {
  171.                 $product $products[$payload["productNumber"]];
  172.                 $porRuleset $priceOnRequestService->getPriceOnRequestRules($product$cg);
  173.                 $rulesets[] = $porRuleset;
  174.             }
  175.             $porRuleset $priceOnRequestService->sortRulesets($rulesets);
  176.             $item->addExtension('nimbits_por'$porRuleset);
  177.         }
  178.         $checkoutdisabled $priceOnRequestService->checkForCheckoutDisabled($products$cg);
  179.         $event->getPage()->addExtension('nimbits_por'$checkoutdisabled);
  180.     }
  181.     public function onCheckoutPageLoaded(CheckoutConfirmPageLoadedEvent $event)
  182.     {
  183.         $salesChannelContext $event->getSalesChannelContext();
  184.         $customer $salesChannelContext->getCustomer();
  185.         if ($customer != null) {
  186.             $event->getPage()->addExtension('nimbits_por_customer'$customer);
  187.         }
  188.     }
  189.     public function onCheckoutCartPageLoaded(CheckoutCartPageLoadedEvent $event)
  190.     {
  191.         $salesChannelContext $event->getSalesChannelContext();
  192.         $salesChannelId $event->getSalesChannelContext()->getSalesChannel()->getId();
  193.         $cg $event->getSalesChannelContext()->getCurrentCustomerGroup()->getName();
  194.         $config $this->settingsService->getSettingsAsStruct($event->getSalesChannelContext()->getSalesChannel()->getId());
  195.         $priceOnRequestService $this->priceOnRequestService;
  196.         $priceOnRequestService->setSalesChannelId($salesChannelId);
  197.         $currentCart $this->cartService->getCart($salesChannelContext->getToken(), $salesChannelContext);
  198.         $lineitems $currentCart->getLineItems();
  199.         //iterate cart and apply price on request rules to each item and the cart
  200.         $products = [];
  201.         $productNumbers = [];
  202.         foreach ($lineitems as $item) {
  203.             $payload $item->getPayload();
  204.             if (!isset($payload["productNumber"])) {
  205.                 continue;
  206.             }
  207.             //xtract children's productNumbers if any
  208.             foreach ($item->getChildren() as $child) {
  209.                 $payloadChild $child->getPayload();
  210.                 if (!isset($payloadChild["productNumber"])) {
  211.                     continue;
  212.                 }
  213.                 $productNumbers[] = $payloadChild["productNumber"];
  214.             }
  215.             $productNumbers[] = $payload["productNumber"];
  216.         }
  217.         $prods $this->salesChannelProductRepository->search(
  218.             (new Criteria())
  219.                 ->addFilter(new EqualsAnyFilter('productNumber'$productNumbers)),
  220.             $salesChannelContext
  221.         );
  222.         $prods $prods->getElements();
  223.         //$product = $this->productDetailRoute->load($prods[$akeys[0]]->getId(), $event->getRequest(),$salesChannelContext, $criteria)->getProduct();
  224.         foreach ($prods as $p) {
  225.             $products[$p->getProductNumber()] = $p;
  226.         }
  227.         //iterate cart and apply price on request ruleset to each item and the cart
  228.         foreach ($lineitems as $item) {
  229.             $payload $item->getPayload();
  230.             $rulesets = [];
  231.             if (!isset($payload["productNumber"])) {
  232.                 continue;
  233.             }
  234.             foreach ($item->getChildren() as $child) {
  235.                 $payloadChild $child->getPayload();
  236.                 if (!isset($payloadChild["productNumber"])) {
  237.                     continue;
  238.                 }
  239.                 if (isset($products[$payloadChild["productNumber"]])) {
  240.                     $product $products[$payloadChild["productNumber"]];
  241.                     $porRuleset $priceOnRequestService->getPriceOnRequestRules($product$cg);
  242.                     $rulesets[] = $porRuleset;
  243.                 }
  244.             }
  245.             if (isset($products[$payload["productNumber"]])) {
  246.                 $product $products[$payload["productNumber"]];
  247.                 $porRuleset $priceOnRequestService->getPriceOnRequestRules($product$cg);
  248.                 $rulesets[] = $porRuleset;
  249.             }
  250.             $porRuleset $priceOnRequestService->sortRulesets($rulesets);
  251.             $item->addExtension('nimbits_por'$porRuleset);
  252.         }
  253.         $checkoutdisabled $priceOnRequestService->checkForCheckoutDisabled($products$cg);
  254.         $checkoutdisabled->dontAddByProductNumber = ($event->getRequest()->get('nb_por_nbpn'0) == 1);
  255.         $event->getPage()->addExtension('nimbits_por'$checkoutdisabled);
  256.     }
  257.     public function onFooterLoaded(FooterPageletLoadedEvent $event)
  258.     {
  259.         $event->getPagelet()->addExtension('nimbitsPriceOnRequestSettings',
  260.             $this->settingsService->getSettingsAsStruct($event->getSalesChannelContext()->getSalesChannel()->getId())
  261.         );
  262.     }
  263. }