custom/plugins/Itptheme/src/Subscriber/ItpthemeSubscriber.php line 108

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Itptheme\Subscriber;
  3. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\OrFilter;
  8. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
  9. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  10. use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
  11. use Shopware\Storefront\Page\GenericPageLoadedEvent;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  14. class ItpthemeSubscriber implements EventSubscriberInterface
  15. {
  16.     protected EntityRepositoryInterface $shippingMethodRepository;
  17.     public function __construct(EntityRepositoryInterface $shippingMethodRepository)
  18.     {
  19.         $this->shippingMethodRepository = $shippingMethodRepository;
  20.     }
  21.     public static function getSubscribedEvents(): array
  22.     {
  23.         return [
  24.             ProductPageLoadedEvent::class => 'onProductsLoaded',
  25.             'product.loaded' => 'onEntityLoadedEvent',
  26.             CheckoutConfirmPageLoadedEvent::class => 'onCheckoutConfirmPageLoaded',
  27.             OffcanvasCartPageLoadedEvent::class => 'onOffcanvasCartLoaded',
  28.             CheckoutCartPageLoadedEvent::class => 'onCheckoutCartPageLoadedEvent',
  29.             GenericPageLoadedEvent::class => 'onGenericPageLoaded'
  30.         ];
  31.     }
  32.     public function onEntityLoadedEvent(EntityLoadedEvent $event)
  33.     {
  34.         $products = $event->getEntities();
  35.         foreach ($products as $product) {
  36.             $p = $product->{"customFields"};
  37.             $tmp = array();
  38.             $tmp = $this->checkZeiss($p, $tmp);
  39.             $tmpStr = join(', ', array_unique($tmp));
  40.             $tmpStr = trim($tmpStr);
  41.             $tmpStr = trim($tmpStr, ',');
  42.             $tmpStr = trim($tmpStr);
  43.             if (count($tmp) > 0) {
  44.                 $p['show_extended_itp_skus'] = '1';
  45.                 $p['extended_itp_skus'] = $tmpStr;
  46.             } else {
  47.                 $p['show_extended_itp_skus'] = '0';
  48.             }
  49.             $product->{"customFields"} = $p;
  50.         }
  51.     }
  52.     public function onProductsLoaded(ProductPageLoadedEvent $event)
  53.     {
  54.         $productData = $event->getPage()->getProduct()->getCustomFields();
  55.         $tmp = array();
  56.         $tmp = $this->checkZeiss($productData, $tmp);
  57.         if (count($tmp) > 0) {
  58.             $tmpStr = join(', ', array_unique($tmp));
  59.             $tmpStr = trim($tmpStr);
  60.             $tmpStr = trim($tmpStr, ',');
  61.             $tmpStr = trim($tmpStr);
  62.             $event->getPage()->assign(['show_extended_itp_skus' => '1']);
  63.             $event->getPage()->assign(['extended_itp_skus' => $tmpStr]);
  64.         } else {
  65.             $event->getPage()->assign(['show_extended_itp_skus' => '0']);
  66.         }
  67.     }
  68.     public function checkZeiss($element, $tmp)
  69.     {
  70.         if (@$element['itp_product_attributes_zeiss_no'] != '') {
  71.             $tmp[] = $element['itp_product_attributes_zeiss_no'];
  72.         }
  73.         if (@$element['itp_product_attributes_zeiss_no_2'] != '') {
  74.             $tmp[] = $element['itp_product_attributes_zeiss_no_2'];
  75.         }
  76.         return $tmp;
  77.     }
  78.     public function onCheckoutConfirmPageLoaded(CheckoutConfirmPageLoadedEvent $event)
  79.     {
  80.         $this->selectAndSortShippingMethods($event);
  81.     }
  82.     public function onOffcanvasCartLoaded(OffcanvasCartPageLoadedEvent $event)
  83.     {
  84.         $this->selectAndSortShippingMethods($event);
  85.     }
  86.     public function onCheckoutCartPageLoadedEvent(CheckoutCartPageLoadedEvent $event)
  87.     {
  88.         $this->selectAndSortShippingMethods($event);
  89.     }
  90.     private function selectAndSortShippingMethods($event)
  91.     {
  92.         $shippingMethodIds = $event->getPage()->getShippingMethods()->getIds();
  93.         $context = $event->getContext();
  94.         $context->setConsiderInheritance(true);
  95.         $orFilters = [];
  96.         foreach ($shippingMethodIds as $shippingMethodId) {
  97.             $orFilters[] = new EqualsFilter('id', $shippingMethodId);
  98.         }
  99.         $criteria = new Criteria();
  100.         $criteria->addAssociation('prices');
  101.         $criteria->addAssociation('deliveryTime');
  102.         $criteria->addAssociation('tax');
  103.         $criteria->addFilter(new OrFilter($orFilters));
  104.         $shippingMethods = $this->shippingMethodRepository->search($criteria, $context);
  105.         $sortArray = $this->sortArray($shippingMethods->getElements(), $this->getShippingNetPrices($shippingMethods));
  106.         $sortedShippingMethods = new \Shopware\Core\Checkout\Shipping\ShippingMethodCollection($sortArray);
  107.         $event->getPage()->setShippingMethods($sortedShippingMethods);
  108.     }
  109.     private function getShippingNetPrices($shippingMethods)
  110.     {
  111.         $shippingMethodsWithNetPrices = [];
  112.         foreach ($shippingMethods as $shippingMethod) {
  113.             $netPrice = null;
  114.             $priceCollection = $shippingMethod->getPrices()->getElements();
  115.             if (is_array($priceCollection) && !empty($priceCollection)) {
  116.                 $firstPrice = reset($priceCollection);
  117.                 if ($firstPrice instanceof \Shopware\Core\Checkout\Shipping\Aggregate\ShippingMethodPrice\ShippingMethodPriceEntity) {
  118.                     $netPrice = $firstPrice->getCurrencyPrice()->first()->getNet();
  119.                 }
  120.             }
  121.             $shippingMethodsWithNetPrices[$shippingMethod->getId()] = $netPrice;
  122.         }
  123.         return $shippingMethodsWithNetPrices;
  124.     }
  125.     private function sortArray($sortArray, $shippingMethodsWithNetPrices)
  126.     {
  127.         uasort($sortArray, function ($a, $b) use ($shippingMethodsWithNetPrices) {
  128.             $netPriceA = $shippingMethodsWithNetPrices[$a->getId()] ?? 0;
  129.             $netPriceB = $shippingMethodsWithNetPrices[$b->getId()] ?? 0;
  130.             if ($netPriceA === $netPriceB) {
  131.                 return 0;
  132.             }
  133.             return ($netPriceA < $netPriceB) ? -1 : 1;
  134.         });
  135.         return $sortArray;
  136.     }
  137.     public function onGenericPageLoaded(GenericPageLoadedEvent $event)
  138.     {
  139.         $customerLoggedIn = false;
  140.         if ($event->getSalesChannelContext()->getCustomer()) {
  141.             $customerLoggedIn = true;
  142.         }
  143.         $event->getPage()->setExtensions(['customer_logged_in' => $customerLoggedIn]);
  144.     }
  145. }