custom/static-plugins/MadBodymedBundle/src/Subscriber/ProductSubscriber.php line 43

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace MadBodymedBundle\Subscriber;
  4. use MadBodymedBundle\Core\Content\Product\ProductExtension;
  5. use MadBodymedBundle\Service\BundleChecker;
  6. use MadBodymedBundle\Service\BundleConfigurationLoader;
  7. use Shopware\Core\Checkout\Cart\Price\Struct\CalculatedPrice;
  8. use Shopware\Core\Checkout\Cart\Tax\Struct\CalculatedTax;
  9. use Shopware\Core\Checkout\Cart\Tax\Struct\CalculatedTaxCollection;
  10. use Shopware\Core\Checkout\Cart\Tax\Struct\TaxRule;
  11. use Shopware\Core\Checkout\Cart\Tax\Struct\TaxRuleCollection;
  12. use Shopware\Core\Content\Product\ProductEntity;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  14. use Shopware\Storefront\Page\Product\ProductPageCriteriaEvent;
  15. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. class ProductSubscriber implements EventSubscriberInterface
  18. {
  19.     private BundleChecker $bundleChecker;
  20.     private BundleConfigurationLoader $bundleConfigurationLoader;
  21.     public function __construct(BundleChecker $bundleCheckerBundleConfigurationLoader $bundleConfigurationLoader)
  22.     {
  23.         $this->bundleChecker $bundleChecker;
  24.         $this->bundleConfigurationLoader $bundleConfigurationLoader;
  25.     }
  26.     public static function getSubscribedEvents(): array
  27.     {
  28.         // Return the events to listen to as array like this:  <event to listen to> => <method to execute>
  29.         return [
  30.             ProductPageLoadedEvent::class => 'onProductPageLoaded',
  31.             'product.loaded' => 'onProductLoaded',
  32.             #ProductPageCriteriaEvent::class => 'onProductPageCriteria',
  33.         ];
  34.     }
  35.     public function onProductLoaded(EntityLoadedEvent $event)
  36.     {
  37.         /* @var $product ProductEntity */
  38.         foreach ($event->getEntities() as $product) {
  39.             if ($this->isProductBundle($product)) {
  40.                 // @todo Alternative suchen
  41.                 #$product->setGrouped(true);
  42.             }
  43.         }
  44.     }
  45.     public function onProductPageCriteria(ProductPageCriteriaEvent $event)
  46.     {
  47.         #$event->getCriteria()->addAssociation('bundle');
  48.         #$event->getCriteria()->addAssociation('bundle_product.bundle_id');
  49.     }
  50.     public function onProductPageLoaded(ProductPageLoadedEvent $event): void
  51.     {
  52.         $mainProduct $event->getPage()->getProduct();
  53.         $salesChannelContext $event->getSalesChannelContext();
  54.         if ($this->isProductBundle($mainProduct)) {
  55.             $bundle $this->bundleConfigurationLoader->loadBundleConfigForMainProduct(
  56.                 $mainProduct,
  57.                 $salesChannelContext
  58.             );
  59.             if ($bundle) {
  60.                 $mainProduct->addExtension(ProductExtension::PRODUCT_EXTENSION_BUNDLE$bundle);
  61.                 #$mainProduct->setGrouped(true);
  62.             }
  63.         }
  64.     }
  65.     private function isProductBundle(ProductEntity $productEntity): bool
  66.     {
  67.         return $this->bundleChecker->isProductBundle($productEntity);
  68.     }
  69. }