custom/plugins/XioniCmsTwig/src/Core/Content/Product/Subscriber/ProductListingSubscriber.php line 141

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace XioniCmsTwig\Core\Content\Product\Subscriber;
  3. use Shopware\Core\Framework\Struct\ArrayEntity;
  4. use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
  5. use Shopware\Core\Content\Product\Events\ProductListingRouteCacheKeyEvent;
  6. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. use Shopware\Core\Content\Product\ProductEntity;
  10. use Shopware\Core\Checkout\Cart\Price\Struct\CalculatedPrice;
  11. use Shopware\Core\Checkout\Cart\Tax\Struct\CalculatedTaxCollection;
  12. use Shopware\Core\Checkout\Cart\Tax\Struct\TaxRuleCollection;
  13. use Shopware\Core\Framework\Uuid\Uuid;
  14. class ProductListingSubscriber implements EventSubscriberInterface
  15. {
  16.     private $requestStack;
  17.     
  18.     public function __construct(RequestStack $requestStack) {
  19.         $this->requestStack $requestStack;
  20.     }
  21.     
  22.     public static function getSubscribedEvents()
  23.     {
  24.         return [
  25.             ProductListingResultEvent::class => 'onListing',
  26.             ProductListingRouteCacheKeyEvent::class => 'onRouteCacheKey'
  27.         ];
  28.     }
  29.     
  30.     public function onListing(ProductListingResultEvent $event): void
  31.     {
  32.         $request $event->getRequest();
  33.         $chosen_values "";
  34.         $variants = array();
  35.         $product_numbers = array();
  36.         $new_products = array();
  37.         $xlisting false;
  38.         $count 0;
  39.         
  40.         for ($i 0$i<=50 $i++) {
  41.             if ($request->get("xoption_".$i) != null) {
  42.                 $xlisting true;
  43.                 break;
  44.             }       
  45.         }    
  46.         if ($xlisting) {        
  47.             for ($i 0$i<=50 $i++) {
  48.                 if ($request->get("xoption_".$i) == null) {
  49.                     continue;
  50.                     
  51.                 } else {
  52.                     $count $count+1;
  53.                 }           
  54.                 if(is_array($request->get("xoption_".$i))) {
  55.                     foreach($request->get("xoption_".$i) as $index => $item) {
  56.                         if ($chosen_values != "") {
  57.                             $chosen_values $chosen_values "," $item;
  58.                         } else {
  59.                             $chosen_values $item;
  60.                         }                    
  61.                     }                    
  62.                 } else {
  63.                     if ($chosen_values != "") {
  64.                         $chosen_values $chosen_values "," $request->get("xoption_".$i);
  65.                     } else {
  66.                         $chosen_values $request->get("xoption_".$i);
  67.                     }                      
  68.                 }
  69.                 
  70.             }
  71.             if($chosen_values != "" && $count != 0) {
  72.                 $variants $this->getVariantsByValues($chosen_values$count);
  73.                 $variants $variants['data'];
  74.             }
  75.             foreach ($variants as $index => $item) {
  76.                 $product_number $this->getProductNumberByMatrixId($item['base']['id'])['data'];
  77.                 $product_numbers[] = $product_number;
  78.                 $configuration $this->getConfigurationByMatrixId($item['base']['id'])['data'];
  79.                 $configuration explode(':'$configuration);
  80.                 $new_products[$index]['groups'] = $configuration[1]; 
  81.                 $new_products[$index]['values'] = $configuration[0]; 
  82.                 $new_products[$index]['matrix_id'] = $item['base']['id'];
  83.                 $new_products[$index]['name'] = $item['product'][0]['base']['description_long'];
  84.                 $new_products[$index]['price'] = $item['product'][0]['prices'][0]['price'];
  85.                 $new_products[$index]['description'] = $item['base']['name'];
  86.                 if($item['product'][0]['media_details']) {
  87.                     $new_products[$index]['media'] = $item['product'][0]['media_details'][0]['media_path'];
  88.                 } else {
  89.                     $new_products[$index]['media'] = null;
  90.                 }
  91.                 $new_products[$index]['product_number'] = $product_number;
  92.             }
  93.             
  94.             $listing_products $event->getResult()->getElements();
  95.             $original_products = array();
  96.             foreach ($listing_products as $index => $item) {
  97.                 $product_number $item->getProductNumber();
  98.                 if(!in_array($product_number$product_numbers)) {
  99.                     $event->getResult()->remove($index);
  100.                 } else {  
  101.                     $original_products[] = $item;
  102.                     $event->getResult()->remove($index);
  103.                 }
  104.             }
  105.             foreach($new_products as $index => $item) { 
  106.                 foreach ($original_products as $l_index => $l_item) { 
  107.                     $product_number $l_item->getProductNumber();
  108.                     if ($item['product_number'] == $product_number) {
  109.                         $copied = clone($l_item);
  110.                         $copied->setUniqueIdentifier(Uuid::randomHex());
  111.                         $copied->setVersionId(Uuid::randomHex());
  112.                         $copied->setName($item['name']);
  113.                         $copied->addTranslated("name"$item['name']);
  114.                         $price floatval($item['price']);
  115.                         $price = new CalculatedPrice($price$price, new CalculatedTaxCollection(), new TaxRuleCollection());
  116.                         $copied->setCalculatedPrice($price);
  117.                         $copied->addExtension('xconfig', new ArrayEntity([
  118.                         'matrix_id' => $item['matrix_id'], 
  119.                         'media' => $item['media'],
  120.                         'new_name' => $item['name'],                        
  121.                         'product_number' => $item['product_number'],
  122.                         'groups' => $item['groups'],
  123.                         'values' => $item['values'],
  124.                         'description' => $item['description']
  125.                         ]));
  126.                         $event->getResult()->add($copied);
  127.                         unset($copied);
  128.                         break;
  129.                     }
  130.                 }
  131.             }
  132.         }
  133.     }
  134.     public function onRouteCacheKey(ProductListingRouteCacheKeyEvent $event): void
  135.     {
  136.         $xlisting false;
  137.         for ($i 0$i<=50 $i++) {
  138.             if ($event->getRequest()->get("xoption_".$i) != null) {
  139.                 $xlisting true;
  140.                 break;
  141.             }       
  142.         }    
  143.         
  144.         if ($xlisting) {   
  145.             for ($i 0$i<=50 $i++) {
  146.                 if ($event->getRequest()->get('xoption_'.$i) == null) {
  147.                     continue;
  148.                 }
  149.                 if(is_array($event->getRequest()->get('xoption_'.$i))) {
  150.                     foreach($event->getRequest()->get('xoption_'.$i) as $index => $item) {
  151.                         $event->addPart(urlencode((string)$item));                   
  152.                     }                    
  153.                 } else {
  154.                     $event->addPart(urlencode((string)$event->getRequest()->get('xoption_'.$i)));                   
  155.                 }                
  156.             }
  157.         }    
  158.     }
  159.     
  160.     private function getVariantsByValues($chosen_values$count)
  161.     {
  162.         $url        'http://cloud8-vm443.de-nserver.de:8080/api/configurators/variantsbyvalues/get/';
  163.         $data       = array('chosen_values' => $chosen_values'count' => $count);
  164.         $options    = array(
  165.             'http' => array(
  166.                 'header'  => "Content-type: application/x-www-form-urlencoded",
  167.                 'method'  => 'POST',
  168.                 'content' => http_build_query($data)
  169.             )
  170.         );
  171.         $context        stream_context_create($options);
  172.         $result         file_get_contents($urlfalse$context);
  173.         $result         json_decode($resulttrue);
  174.         
  175.         return $result;
  176.     } 
  177.     
  178.     private function getProductNumberByMatrixId($matrix_id)
  179.     {
  180.         $url        'http://cloud8-vm443.de-nserver.de:8080/api/configurators/matrix/getproductnumber/'.$matrix_id;
  181.         $value     file_get_contents($url);
  182.         
  183.         $value     json_decode($valuetrue);
  184.         
  185.         return $value;
  186.     }
  187.    
  188.     private function getConfigurationByMatrixId($matrix_id)
  189.     {
  190.         $url        'http://cloud8-vm443.de-nserver.de:8080/api/configurators/configurationbyvariant/get/'.$matrix_id;
  191.         $value     file_get_contents($url);
  192.         
  193.         $value     json_decode($valuetrue);
  194.         
  195.         return $value;
  196.     }   
  197. }