custom/plugins/WbfkExtensions/src/Subscriber/AddShippingAndDeliveryInformationToProduct.php line 57

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace WbfkExtensions\Subscriber;
  4. use Exception;
  5. use Shopware\Core\Content\Product\Events\ProductListingResolvePreviewEvent;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  7. use Shopware\Core\System\SalesChannel\Entity\SalesChannelEntityLoadedEvent;
  8. use Shopware\Storefront\Page\Product\ProductPageCriteriaEvent;
  9. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use WbfkExtensions\Core\Checkout\Cart\Delivery\PreCalculatedShippingAndDeliveryInformationService;
  12. use WbfkExtensions\Core\Checkout\Cart\Delivery\ShippingAndDeliveryInformationService;
  13. class AddShippingAndDeliveryInformationToProduct implements EventSubscriberInterface
  14. {
  15.     public function __construct(
  16.         protected readonly PreCalculatedShippingAndDeliveryInformationService $preCalculatedShippingAndDeliveryInformationService,
  17.         protected readonly ShippingAndDeliveryInformationService $shippingAndDeliveryInformationService
  18.     ) {
  19.     }
  20.     public static function getSubscribedEvents(): array
  21.     {
  22.         return [
  23.             'sales_channel.product.loaded' => [['findPreCalculatedSupplierDeliveryDateRange']],
  24.             ProductListingResolvePreviewEvent::class => [['addSupplierDeliveryTimesToQuery']],
  25.             ProductPageCriteriaEvent::class => [['addSupplierToQuery']],
  26.             ProductPageLoadedEvent::class => [['findCurrentSupplierDeliveryDateRange']]
  27.         ];
  28.     }
  29.     /**
  30.      * @throws Exception
  31.      */
  32.     public function findPreCalculatedSupplierDeliveryDateRange(SalesChannelEntityLoadedEvent $event): void
  33.     {
  34.         $this->preCalculatedShippingAndDeliveryInformationService->addSupplierDeliveryDateRangeToProducts(
  35.             $event->getEntities(),
  36.             $event->getSalesChannelContext()->getShippingLocation()->getCountry(),
  37.             $event->getContext()
  38.         );
  39.     }
  40.     public function addSupplierDeliveryTimesToQuery(ProductPageCriteriaEvent|ProductListingResolvePreviewEvent $event): void
  41.     {
  42.         $criteria $event->getCriteria();
  43.         $criteria->addAssociation('supplierDeliveryTimes');
  44.         $criteria->getAssociation('supplierDeliveryTimes')->addFilter(
  45.             new EqualsFilter('countryId'$event->getSalesChannelContext()->getShippingLocation()->getCountry()->getId())
  46.         );
  47.     }
  48.     public function addSupplierToQuery(ProductPageCriteriaEvent $event): void
  49.     {
  50.         $criteria $event->getCriteria();
  51.         $criteria->addAssociation('productSuppliers.wbfkSupplier.deliveryTimes');
  52.         $criteria->getAssociation('productSuppliers')->addFilter(
  53.             new EqualsFilter('wbfkSupplier.isActive'true),
  54.             new EqualsFilter('active'true)
  55.         );
  56.     }
  57.     /**
  58.      * @throws Exception
  59.      */
  60.     public function findCurrentSupplierDeliveryDateRange(ProductPageLoadedEvent $event): void
  61.     {
  62.         /*
  63.          * NOTE : Shipping and delivery informations are also added from
  64.          * 1. \WbfkExtensions\Core\Content\Product\Cms\BuyBoxCmsElementResolverDecorator::enrich
  65.          * 2. \WbfkExtensions\Twig\ShippingInformation::loadProductWithDeliveryInformation
  66.          */
  67.         $product $event->getPage()->getProduct();
  68.         if ($product->getExtension('wbfk_bundle_product')->count()) {
  69.             // For bundle product we shipping and delivery informations are added from \Wbfk\Bundles\Subscriber\ProductSubscriber::onProductPageLoaded
  70.             return;
  71.         }
  72.         /** @noinspection PhpDeprecationInspection */
  73.         $product->addExtension(
  74.             'shippingAndDeliveryInformations',
  75.             $this->shippingAndDeliveryInformationService->getShippingAndDeliveryInformationSortedBySupplierPriority($product$event->getSalesChannelContext())
  76.         );
  77.     }
  78. }