custom/plugins/SwpRefundSystemSix/src/Storefront/Subscriber/Frontend.php line 76

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /**
  3.  * Shopware
  4.  * Copyright © 2020
  5.  *
  6.  * @category   Shopware
  7.  * @package    SwpProductOptionsSix
  8.  * @subpackage ProductPageCriteriaSubscriber.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\RefundSystemSix\Storefront\Subscriber;
  15. use Shopware\Core\Content\Cms\Aggregate\CmsSlot\CmsSlotEntity;
  16. use Shopware\Core\Content\Cms\CmsPageCollection;
  17. use Shopware\Core\Content\Cms\CmsPageEntity;
  18. use Shopware\Core\Content\Cms\SalesChannel\Struct\ProductBoxStruct;
  19. use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
  20. use Shopware\Core\Content\Product\Events\ProductSuggestResultEvent;
  21. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  22. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  23. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  24. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  25. use Shopware\Core\System\SystemConfig\SystemConfigService;
  26. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  27. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  28. use Shopware\Core\Content\Cms\Events\CmsPageLoadedEvent;
  29. use Shopware\Core\Content\Product\ProductCollection;
  30. class Frontend implements EventSubscriberInterface
  31. {
  32.     /** @var SystemConfigService */
  33.     private $systemConfigService;
  34.     /** @var EntityRepositoryInterface */
  35.     protected $RefundSystemRepository;
  36.     /**
  37.      * Frontend constructor.
  38.      * @param SystemConfigService $systemConfigService
  39.      */
  40.     public function __construct(
  41.         SystemConfigService $systemConfigService,
  42.         EntityRepositoryInterface $RefundSystemRepository
  43.     ) {
  44.         $this->systemConfigService $systemConfigService;
  45.         $this->RefundSystemRepository $RefundSystemRepository;
  46.     }
  47.     /**
  48.      * @return array
  49.      */
  50.     public static function getSubscribedEvents(): array
  51.     {
  52.         return [
  53.             ProductListingResultEvent::class => 'onProductListingResult',
  54.             ProductPageLoadedEvent::class => 'onProductPageLoaded',
  55.             ProductSuggestResultEvent::class => 'onSuggestLoaded',
  56.             CmsPageLoadedEvent::class => 'onCmsPageLoaded'
  57.         ];
  58.     }
  59.   
  60.       /**
  61.      * @param CmsPageLoadedEvent $event
  62.      * @return void
  63.      */
  64.     public function onCmsPageLoaded(CmsPageLoadedEvent $event): void
  65.     {
  66.         
  67.         if ($this->isCannelActive($event->getSalesChannelContext()) !== true) {
  68.             return;
  69.         }
  70.         
  71.         /** @var CmsPageCollection $cms */
  72.         $cms $event->getResult();
  73.         /** @var CmsPageEntity $cmsEntity */
  74.         foreach ($cms->getIterator() as $cmsEntity) {
  75.             
  76.             //Product
  77.             /** @var CmsSlotEntity $productBoxes */
  78.             $productBoxes $cmsEntity->getElementsOfType('product-box');
  79.             /** @var CmsSlotEntity $productBox */
  80.             foreach ($productBoxes as $productBox) {
  81.                 /** @var ProductBoxStruct $box */
  82.                 $box=$productBox->getData();
  83.                 /** @var SalesChannelProductEntity $product */
  84.                 if (!method_exists($box'getProduct')) continue;
  85.                 $product $box->getProduct();
  86.                 $refund $this->productHasRefund($product->getID(), $product$event);
  87.                 if(count($refund)>0) {
  88.                     
  89.                     $product->hasRefund $refund;
  90.                 }
  91.             }
  92.             
  93.             //Slider
  94.             $productBoxes $cmsEntity->getElementsOfType('product-slider');
  95.             /** @var CmsSlotEntity $productBox */
  96.             foreach ($productBoxes as $productBox) {
  97.                 $productCollection = new ProductCollection();
  98.                 $sliderProducts $productBox->getData()->getProducts();
  99.                 if ($sliderProducts === null) {
  100.                     continue;
  101.                 }
  102.                 $products = array();
  103.                 foreach ($sliderProducts->getElements() as $key => $product) {
  104.                     $refund $this->productHasRefund($product->getID(), $product$event);
  105.                     if(count($refund)>0) {
  106.                         //$product->setTranslated(array('name' => $this->generateNewProductsName($product, $event, $refund)));
  107.                         $product->hasRefund $refund;
  108.                     }
  109.                 }
  110.             }
  111.             //product-description-reviews
  112.             $productBoxes $cmsEntity->getElementsOfType('product-description-reviews');            
  113.             /** @var CmsSlotEntity $productBox */
  114.             foreach ($productBoxes as $productBox) {
  115.                 /** @var ProductBoxStruct $box */
  116.                 $box=$productBox->getData();
  117.                 /** @var SalesChannelProductEntity $product */
  118.                 
  119.                if (!method_exists($box'getProduct')) continue;
  120.                 $product $box->getProduct();
  121.                 $refund $this->productHasRefund($product->getID(), $product$event);
  122.                 
  123.                 if(count($refund)>0) {
  124.                     //$product->setTranslated(array('name' => $this->generateNewProductsName($product, $event, $refund)));
  125.                     $product->hasRefund $refund;
  126.                 }
  127.             }
  128.             //gallery-buybox
  129.             $productBoxes $cmsEntity->getElementsOfType('buy-box');
  130.             //$productBoxes = array();
  131.             /** @var CmsSlotEntity $productBox */
  132.             foreach ($productBoxes as $productBox) {
  133.                 /** @var ProductBoxStruct $box */
  134.                 $box=$productBox->getData();
  135.                 /** @var SalesChannelProductEntity $product */
  136.                 
  137.                 if (!method_exists($box'getProduct')) continue;
  138.                 $product $box->getProduct();
  139.                 $refund $this->productHasRefund($product->getID(), $product$event);
  140.                 if(count($refund)>0) {
  141.                     //$product->setTranslated(array('name' => $this->generateNewProductsName($product, $event, $refund)));
  142.                     $product->hasRefund $refund;
  143.                 }
  144.             }
  145.             //CrossSelling
  146.             $productBoxes $cmsEntity->getElementsOfType('cross-selling');
  147.             /** @var CmsSlotEntity $productBox */
  148.             foreach ($productBoxes as $productBox) {
  149.                 $crossSellings $productBox->getData()->getCrossSellings();
  150.                 if ($crossSellings === null) {
  151.                     continue;
  152.                 }
  153.                 foreach ($crossSellings->getElements() as $crossSelling) {
  154.                     $productCollection = new ProductCollection();
  155.                     $products $crossSelling->getProducts()->getElements();
  156.                     foreach ($products as $product) {
  157.                         $refund $this->productHasRefund($product->getID(), $product$event);
  158.                 
  159.                         if(count($refund)>0) {
  160.                             //$product->setTranslated(array('name' => $this->generateNewProductsName($product, $event, $refund)));
  161.                             $product->hasRefund $refund;
  162.                         }
  163.                     }
  164.                 }
  165.             }
  166.         }
  167.     }
  168.     /**
  169.     * @param ProductPageLoadedEvent $event
  170.     */
  171.     public function onProductPageLoaded(ProductPageLoadedEvent $event)
  172.     {
  173.         if ($this->isCannelActive($event->getSalesChannelContext()) !== true) {
  174.             return;
  175.         }
  176.         $product $event->getPage()->getProduct();
  177.         $id $product->getId();
  178.         $refund $this->productHasRefund($product->getId(), $product$event);
  179.         
  180.         if(count($refund)>0) {
  181.             //$product->setTranslated(array('name' => $this->generateNewProductsName($product, $event, $refund)));
  182.             $product->hasRefund $refund;
  183.         }
  184.     }
  185.     /**
  186.      * @param ProductSuggestResultEvent $event
  187.      */
  188.     public function onSuggestLoaded(ProductSuggestResultEvent $event): void
  189.     {
  190.         if ($this->isCannelActive($event->getSalesChannelContext()) !== true) {
  191.             return;
  192.         }
  193.         $result $event->getResult();
  194.         $elements $result->getElements();
  195.         foreach($elements as $id => &$product) {
  196.             $refund $this->productHasRefund($id$product$event);
  197.             if(count($refund)>0) {
  198.                 $product->setTranslated(array('name' => $this->generateNewProductsName($product$event$refund)));
  199.                 $product->hasRefund $refund;
  200.             }
  201.         }
  202.     }
  203.     /**
  204.      * @param ProductListingResultEvent $event
  205.      */
  206.     public function onProductListingResult(ProductListingResultEvent $event): void
  207.     {
  208.         if ($this->isCannelActive($event->getSalesChannelContext()) !== true) {
  209.             return;
  210.         }
  211.         $result $event->getResult();
  212.         $elements $result->getElements();
  213.         /** @var SalesChannelProductEntity $product */
  214.         foreach($elements as $id => &$product) {
  215.             $refund $this->productHasRefund($id$product$event);
  216.             if(count($refund)>0) {
  217.                 //$product->setTranslated(array('name' => $this->generateNewProductsName($product, $event, $refund)));
  218.                 $product->hasRefund $refund;
  219.             }
  220.         }
  221.     }
  222.     /**
  223.      * @param string $id
  224.      * @return array
  225.      */
  226.     private function productHasRefund($idSalesChannelProductEntity $product$event): array
  227.     {
  228.         $customFields $product->getCustomFields();
  229.         if (!array_key_exists('refund_system'$customFields)){
  230.             return array();
  231.         }
  232.         $data $this->RefundSystemRepository->search(
  233.             new Criteria([$customFields['refund_system']]),
  234.             $event->getContext()
  235.         );
  236.         $refund[]=$data->getElements();
  237.         $refundName '';
  238.         $active false;
  239.         foreach ($data->getElements() as $key => $val){
  240.             $active $val->isActive();
  241.             $refundName $val->getTranslation('name');
  242.         }
  243.         if (!$active){
  244.             return array();
  245.         }
  246.         //$return['refund']['data'] = $refund;
  247.         if (isset($customFields['refund_system_price'])) {
  248.             $return['price'] = $customFields['refund_system_price'];
  249.             if (isset($customFields['swp_refund_price_back'])) {
  250.                 if ($customFields['swp_refund_price_back']==1){
  251.                     $return['price']=$return['price']*(-1);
  252.                 }
  253.            }
  254.         }else {
  255.             $return['price'] = 0;
  256.         }
  257.         $return['name'] = $refundName;
  258.         $return['calculated_price']=$this -> calculateRefundPrice($product$event$return);
  259.         return $return;
  260.     }
  261.     /**
  262.      * @param string $id
  263.      * @return array
  264.      */
  265.     private function generateNewProductsName(SalesChannelProductEntity $product$event$refund): string
  266.     {
  267.         $productsName $product->getTranslation('name');
  268.         $currency $event->getSalesChannelContext()->getCurrency()->getIsoCode();
  269.         $currencyFactor $event->getContext()->getCurrencyFactor();
  270.         //$currencyPrecission = $event->getContext()->getCurrencyPrecision();
  271.         $refundPrice $this -> calculateRefundPrice($product$event$refund);
  272.         $newProductsName $productsName.' - '.strtoupper($refund['name']);
  273.         return $newProductsName;
  274.     }
  275.     /**
  276.      * @param string $id
  277.      * @return array
  278.      */
  279.     private function calculateRefundPrice(SalesChannelProductEntity $product$event$refund): float
  280.     {
  281.         $productsName $product->getName();
  282.         $currency $event->getSalesChannelContext()->getCurrency()->getIsoCode();
  283.         $currencyFactor $event->getContext()->getCurrencyFactor();
  284.         //$currencyPrecission = $event->getContext()->getCurrencyPrecision();
  285.         $currencyPrecission 2;
  286.         $taxRate $product->getTax()->getTaxRate();
  287.         $taxStatus $event->getContext()->getTaxState();
  288. //        if ($taxStatus == 'net'){
  289. //            //keine Steuer berechnen
  290. //        }else{
  291. //            //berechne Steuer
  292. //        }
  293.         return round($refund['price']*$currencyFactor$currencyPrecission);
  294.     }
  295.     /**
  296.      * @param SalesChannelContext $event
  297.      * @return array|mixed|null
  298.      */
  299.     private function isCannelActive(SalesChannelContext $event)
  300.     {
  301.         return $this->systemConfigService->get(
  302.             'SwpRefundSystemSix.config.channelActive',
  303.             $event->getSalesChannel()->getId()
  304.         );
  305.     }
  306. }