custom/plugins/TTAlternateProduct/src/Subscriber/AlternateProductSubscriber.php line 32

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace TTAlternateProduct\Subscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  7. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  8. class AlternateProductSubscriber implements EventSubscriberInterface
  9. {
  10.     protected SalesChannelRepositoryInterface $salesChannelProductRepository;
  11.     public function __construct(
  12.         SalesChannelRepositoryInterface $salesChannelProductRepository
  13.     ){
  14.         $this->salesChannelProductRepository $salesChannelProductRepository;
  15.     }
  16.     public static function getSubscribedEvents()
  17.     {
  18.         return [
  19.             ProductPageLoadedEvent::class => 'onProductPageLoaded'
  20.         ];
  21.     }
  22.     /**
  23.      * @throws Exception
  24.      */
  25.     public function onProductPageLoaded(ProductPageLoadedEvent $event): void
  26.     {
  27.         $page $event->getPage();
  28.         $product $page->getProduct();
  29.        
  30.         $criteria = new Criteria();
  31.         $criteria->addAssociation('crossSellings.assignedProducts.product');
  32.         $criteria->addFilter(new EqualsFilter('productNumber'$product->getProductNumber()));
  33.         $criteria->addFilter(new EqualsFilter('crossSellings.name''sinnvolle Alternative'));
  34.         $crossSellings $this->salesChannelProductRepository->search($criteria$event->getSalesChannelContext())->first();
  35.         if($crossSellings != null){
  36.             $crossSellings $crossSellings->getCrossSellings();
  37.         }
  38.         $productAlternate null;
  39.         if($crossSellings){
  40.            foreach($crossSellings as $crossSelling){
  41.             if($crossSelling->getName() == 'sinnvolle Alternative'){
  42.                 $productAlternate $crossSelling->getAssignedProducts()->getElements();
  43.                 break;
  44.             }
  45.            }
  46.         }
  47.         if($productAlternate){
  48.             $productAlternate end($productAlternate)->getProductId();
  49.         }
  50.         
  51.         if(!isset($productAlternate) || $productAlternate == null) {
  52.             return;
  53.         }
  54.         $criteria = new Criteria();
  55.         $criteria->addAssociation('cover');
  56.         $criteria->addFilter(new EqualsFilter('id'$productAlternate));
  57.         $productAlternate $this->salesChannelProductRepository->search($criteria$event->getSalesChannelContext())->first();
  58.         
  59.         if($productAlternate && $productAlternate->getActive() && $productAlternate->getAvailable()) {
  60.             //OR-519 Sinnvolle Alternative Bug
  61.             //OR-559 get correct  price final of product
  62.             $price $product->getCalculatedPrices()->first()->getUnitPrice();
  63.             $priceAlternate $productAlternate->getCalculatedPrices()->first()->getUnitPrice();
  64.             //OR-559 calculate percentage
  65.             $percentageBase   100;
  66.             $percentageLess   = ($priceAlternate 100) / $price;
  67.             $percantageResult number_format(ceil($percentageBase $percentageLess),0,'','');
  68.             //Show if price alternate < price origin
  69.             if((float)$price > (float)$priceAlternate && $percantageResult 1) {
  70.                 $page->assign(['productAlternate' => $productAlternate,'percantageResult' => $percantageResult]);
  71.             }
  72.         }
  73.     }
  74. }