custom/plugins/WbfkExtensions/src/Subscriber/ProductExportEventSubscriber.php line 24

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace WbfkExtensions\Subscriber;
  4. use Shopware\Core\Content\Product\Aggregate\ProductVisibility\ProductVisibilityDefinition;
  5. use Shopware\Core\Content\Product\SalesChannel\ProductAvailableFilter;
  6. use Shopware\Core\Content\ProductExport\Event\ProductExportChangeEncodingEvent;
  7. use Shopware\Core\Content\ProductExport\Event\ProductExportProductCriteriaEvent;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class ProductExportEventSubscriber implements EventSubscriberInterface
  11. {
  12.     public static function getSubscribedEvents(): array
  13.     {
  14.         return [
  15.             ProductExportChangeEncodingEvent::class => 'onProductExportChangeEncodingEvent',
  16.             ProductExportProductCriteriaEvent::class => 'onProductExportProductCriteriaEvent',
  17.         ];
  18.     }
  19.     public function onProductExportProductCriteriaEvent(ProductExportProductCriteriaEvent $event): void
  20.     {
  21.         $criteria $event->getCriteria();
  22.         $criteria->addFilter(new ProductAvailableFilter($event->getSalesChannelContext()->getSalesChannel()->getId(), ProductVisibilityDefinition::VISIBILITY_ALL));
  23.         $customFields $event->getProductExport()->getSalesChannel()->getCustomFields();
  24.         if (!empty($customFields['wbfk_feed_delivery_country'])) {
  25.             $countryId $customFields['wbfk_feed_delivery_country'];
  26.             $criteria->addAssociation('supplierDeliveryTimes');
  27.             $criteria->getAssociation('supplierDeliveryTimes')->addFilter(new EqualsFilter('countryId'$countryId));
  28.         }
  29.         $criteria->addAssociation('productSuppliers.wbfkSupplier.deliveryTimes');
  30.         $productSupplierAssociation $criteria->getAssociation('productSuppliers');
  31.         $productSupplierAssociation->addFilter(new EqualsFilter('active'true));
  32.         $productSupplierAssociation->addFilter(new EqualsFilter('wbfkSupplier.isActive'true));
  33.     }
  34.     public function onProductExportChangeEncodingEvent(ProductExportChangeEncodingEvent $event): void
  35.     {
  36.         $encodedContent $event->getEncodedContent();
  37.         $productExportEntity $event->getProductExportEntity();
  38.         if ($productExportEntity->getFileFormat() == 'csv') {
  39.             $encodedContent str_replace('&gt;''>'$encodedContent);
  40.             $event->setEncodedContent($encodedContent);
  41.             $csvData str_getcsv($encodedContentPHP_EOL);
  42.             $returnData = [];
  43.             foreach ($csvData as $line) {
  44.                 // skip empty csv lines
  45.                 if (str_contains($line'%skipThisLine%')) {
  46.                     continue;
  47.                 }
  48.                 $returnData[] = $line;
  49.             }
  50.             $returnData join(PHP_EOL$returnData);
  51.             $event->setEncodedContent($returnData);
  52.         }
  53.     }
  54. }