custom/plugins/WosoExtCrossSellingFeatures/src/Subscriber/defSubscriber.php line 24

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace WosoExtCrossSellingFeatures\Subscriber;
  3. use Shopware\Core\Content\Product\Events\ProductCrossSellingsLoadedEvent;
  4. use Shopware\Core\Content\Product\Aggregate\ProductCrossSelling\ProductCrossSellingCollection;
  5. use Shopware\Core\Content\Product\Aggregate\ProductCrossSelling\ProductCrossSellingDefinition;
  6. use Shopware\Core\Content\Product\Aggregate\ProductCrossSelling\ProductCrossSellingEntity;
  7. use Shopware\Core\Content\Product\Aggregate\ProductVisibility\ProductVisibilityDefinition;
  8. use Shopware\Core\Framework\Struct\ArrayEntity;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Shopware\Core\System\SystemConfig\SystemConfigService;
  11. class defSubscriber  implements EventSubscriberInterface
  12. {
  13.     private SystemConfigService $systemConfigService;
  14.     public function __construct(SystemConfigService $systemConfigService)
  15.     {
  16.         $this->systemConfigService $systemConfigService;
  17.     }
  18.     public static function getSubscribedEvents(): array
  19.     {
  20.         return [
  21.             ProductCrossSellingsLoadedEvent::class => 'onCrossSellingsLoaded'
  22.         ];
  23.     }
  24.     public function onCrossSellingsLoaded(ProductCrossSellingsLoadedEvent $event)
  25.     {
  26.         $config $this->systemConfigService->get('WosoExtCrossSellingFeatures.config');
  27.         if ( (is_null($config)) || (empty($config)) ) { return; }
  28.         if ($config['active'] !== true) { return; }
  29.         if ( (trim($config['prefixestodelete'])=='') && (trim($config['prefixestohide'])=='') ) { return; }
  30.         $prefDelete=[];$prefHide=[];
  31.         if ($this->genPrefixArraysByConfig($config$prefDelete$prefHide) <= 0) { return; }
  32.         $context $event->getSalesChannelContext();
  33.         $results $event->getCrossSellings();
  34.         $results->addExtension('wosoExtCrossSelling', new ArrayEntity(['test'=>'xxx']));
  35.         foreach ($results as $csElement) {
  36.             $crossSelling $csElement->getCrossSelling();
  37.             $elementname $crossSelling->getName();
  38.             $f='';
  39.             foreach ($prefDelete as $n) {
  40.                 if (strpos($elementname,$n) === 0) {
  41.                     $f=$n;break;
  42.                 }
  43.             }
  44.             if ($f!='') {
  45.                 $nn substr($elementnamestrlen($f),10000);
  46.                 $csElement->getCrossSelling()->setName($nn);
  47.                 $csElement->getCrossSelling()->setTranslated(['name' => $nn]);
  48.             }
  49.             $f='';
  50.             foreach ($prefHide as $n) {
  51.                 if (strpos($elementname$n) === 0) {
  52.                     $f=$n;break;
  53.                 }
  54.             }
  55.             if ($f != '') {
  56.                 $crossSelling->setActive(false);
  57.             }
  58.         }
  59.     }
  60.     private function genPrefixArraysByConfig(array $config, array &$prefDelete, array &$prefHide) : int {
  61.         $ret 0;
  62.         if (trim($config['prefixestodelete'])!='') {
  63.             $x=explode(' '$config['prefixestodelete']);
  64.             foreach ($x as $e) {
  65.                 $prefDelete[]=$e;
  66.                 $ret++;
  67.             }
  68.         }
  69.         if (trim($config['prefixestohide'])!='') {
  70.             $x=explode(' '$config['prefixestohide']);
  71.             foreach ($x as $e) {
  72.                 $prefHide[]=$e;
  73.                 $ret++;
  74.             }
  75.         }
  76.         return $ret;
  77.     }
  78. }