custom/plugins/SwpPriceOnRequestSix/src/Subscriber/Frontend.php line 72

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /**
  3.  * Shopware
  4.  * Copyright © 2020
  5.  *
  6.  * @category   Shopware
  7.  * @package    SwpPriceOnRequestSix
  8.  * @subpackage Frontend.php
  9.  *
  10.  * @copyright  2020 Iguana-Labs GmbH
  11.  * @author     Module Factory <info at module-factory.com>
  12.  * @license    https://www.module-factory.com/eula
  13.  */
  14. namespace Swp\PriceOnRequestSix\Subscriber;
  15. use Shopware\Core\Checkout\Cart\Price\QuantityPriceCalculator;
  16. use Shopware\Core\Checkout\Cart\Price\Struct\CalculatedPrice;
  17. use Shopware\Core\Checkout\Cart\Price\Struct\QuantityPriceDefinition;
  18. use Shopware\Core\Checkout\Cart\Price\Struct\ReferencePriceDefinition;
  19. use Shopware\Core\Content\Cms\Events\CmsPageLoadedEvent;
  20. use Shopware\Core\Content\Product\SalesChannel\Price\AbstractProductPriceCalculator;
  21. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  22. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  23. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  24. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepository;
  25. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  26. use Shopware\Core\System\SystemConfig\SystemConfigService;
  27. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  28. class Frontend implements EventSubscriberInterface
  29. {
  30.     /**
  31.      * @var SalesChannelRepository
  32.      */
  33.     private $productRepository;
  34.     /**
  35.      * @var SystemConfigService
  36.      */
  37.     private $configService;
  38.     private QuantityPriceCalculator $calculator;
  39.     private AbstractProductPriceCalculator $priceCalculator;
  40.     public function __construct(
  41.         SalesChannelRepository $productRepository,
  42.         SystemConfigService $configService,
  43.         QuantityPriceCalculator $calculator,
  44.         AbstractProductPriceCalculator $priceCalculator
  45.     )
  46.     {
  47.         $this->productRepository $productRepository;
  48.         $this->configService $configService;
  49.         $this->calculator $calculator;
  50.         $this->priceCalculator $priceCalculator;
  51.     }
  52.     public static function getSubscribedEvents()
  53.     {
  54.         // Return the events to listen to as array like this:  <event to listen to> => <method to execute>
  55.         return [
  56.             CmsPageLoadedEvent::class => 'onCmsPageLoaded'
  57.         ];
  58.     }
  59.     /**
  60.      * @param CmsPageLoadedEvent $event
  61.      * @return mixed
  62.      */
  63.     public function onCmsPageLoaded(CmsPageLoadedEvent $event)
  64.     {
  65.         $page $event->getResult()->getVars();
  66.         $request $event->getRequest();
  67.         if (empty($request->get('priceOnRequest'))) {
  68.             return;
  69.         }
  70.         $criteria = new Criteria([]);
  71.         $criteria->addFilter(new EqualsFilter('productNumber'$request->get('priceOnRequest')));
  72.         $results $this->productRepository->search($criteria$event->getSalesChannelContext());
  73.         if ($results->getTotal() == 0) {
  74.             return;
  75.         }
  76.         /** @var SalesChannelProductEntity $product */
  77.         foreach ($results->getElements() as $product) {
  78.             if ($product->getProductNumber() == $request->get('priceOnRequest')) {
  79.                 $contactPageId $this->configService->get('core.basicInformation.contactPage');
  80.                 if (!empty($product->getTranslated()) && !empty($product->getTranslated()['name'])) {
  81.                     $productName $product->getTranslated()['name'];
  82.                 } else {
  83.                     $productName $product->getName();
  84.                 }
  85.                 $quantity intval($request->get('quantity'));
  86.                 $definition $this->getPriceDefinition($product$event->getSalesChannelContext(), $quantity);
  87.                 $price $this->calculator->calculate($definition$event->getSalesChannelContext());
  88.                 $extensions $page['elements'][$contactPageId]->getExtensions();
  89.                 $extensions['priceOnRequestSubject'] = $productName " (" $request->get('priceOnRequest') . ")";
  90.                 $extensions['priceOnRequest'] = \json_encode([
  91.                     'name' => $productName,
  92.                     'id' => $product->getId(),
  93.                     'price' => $price->getUnitPrice(),
  94.                     'brand' => $request->get('manufacturer'),
  95.                     'category' => $request->get('category'),
  96.                     'variant' => $request->get('variation'),
  97.                     'quantity' => $quantity,
  98.                 ]);
  99.                 $page['elements'][$contactPageId]->setExtensions($extensions);
  100.             }
  101.         }
  102.     }
  103.     private function getPriceDefinition(SalesChannelProductEntity $productSalesChannelContext $contextint $quantity): QuantityPriceDefinition
  104.     {
  105.         $this->priceCalculator->calculate([$product], $context);
  106.         if ($product->getCalculatedPrices()->count() === 0) {
  107.             return $this->buildPriceDefinition($product->getCalculatedPrice(), $quantity);
  108.         }
  109.         // keep loop reference to $price variable to get last quantity price in case of "null"
  110.         $price $product->getCalculatedPrice();
  111.         foreach ($product->getCalculatedPrices() as $price) {
  112.             if ($quantity <= $price->getQuantity()) {
  113.                 break;
  114.             }
  115.         }
  116.         return $this->buildPriceDefinition($price$quantity);
  117.     }
  118.     private function buildPriceDefinition(CalculatedPrice $priceint $quantity): QuantityPriceDefinition
  119.     {
  120.         $definition = new QuantityPriceDefinition($price->getUnitPrice(), $price->getTaxRules(), $quantity);
  121.         if ($price->getListPrice() !== null) {
  122.             $definition->setListPrice($price->getListPrice()->getPrice());
  123.         }
  124.         if ($price->getReferencePrice() !== null) {
  125.             $definition->setReferencePriceDefinition(
  126.                 new ReferencePriceDefinition(
  127.                     $price->getReferencePrice()->getPurchaseUnit(),
  128.                     $price->getReferencePrice()->getReferenceUnit(),
  129.                     $price->getReferencePrice()->getUnitName()
  130.                 )
  131.             );
  132.         }
  133.         return $definition;
  134.     }
  135. }