custom/plugins/DvsnProductShippingSurcharge/src/Subscriber/Core/Content/ProductExportSubscriber.php line 47

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /**
  3.  * digitvision
  4.  *
  5.  * @category  digitvision
  6.  * @package   Shopware\Plugins\DvsnProductShippingSurcharge
  7.  * @copyright (c) 2021 digitvision
  8.  */
  9. namespace Dvsn\ProductShippingSurcharge\Subscriber\Core\Content;
  10. use Doctrine\DBAL\Connection;
  11. use Shopware\Core\Content\ProductExport\Event\ProductExportRenderBodyContextEvent;
  12. use Shopware\Core\Content\ProductExport\ProductExportEntity;
  13. use Shopware\Core\Defaults;
  14. use Shopware\Core\Framework\Uuid\Uuid;
  15. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. class ProductExportSubscriber implements EventSubscriberInterface
  18. {
  19.     private Connection $connection;
  20.     public function __construct(
  21.         Connection $connection
  22.     ) {
  23.         $this->connection $connection;
  24.     }
  25.     /**
  26.      * {@inheritDoc}
  27.      */
  28.     public static function getSubscribedEvents(): array
  29.     {
  30.         return [
  31.             ProductExportRenderBodyContextEvent::class => 'onProductExportRenderBodyContext'
  32.         ];
  33.     }
  34.     /**
  35.      * ...
  36.      *
  37.      * @param ProductExportRenderBodyContextEvent $event
  38.      */
  39.     public function onProductExportRenderBodyContext(ProductExportRenderBodyContextEvent $event)
  40.     {
  41.         /** @var SalesChannelContext $salesChannelContext */
  42.         $salesChannelContext $event->getContext()['context'];
  43.         /** @var ProductExportEntity $productExport */
  44.         $productExport $event->getContext()['productExport'];
  45.         $customFields = (array) $productExport->getSalesChannel()->getCustomFields();
  46.         if (!isset($customFields['dvsn_product_shipping_surcharge_sales_channel_status']) || $customFields['dvsn_product_shipping_surcharge_sales_channel_status'] !== true) {
  47.             return;
  48.         }
  49.         $query $this->connection->createQueryBuilder()
  50.             ->select('LOWER(HEX(product_id)) AS product_id, surcharge')
  51.             ->from('dvsn_product_shipping_surcharge')
  52.             ->where('product_version_id = :versionId')
  53.             ->setParameter('versionId'Uuid::fromHexToBytes(Defaults::LIVE_VERSION))
  54.             ->groupBy('product_id');
  55.         if (isset($customFields['dvsn_product_shipping_surcharge_sales_channel_shipping_method']) && !empty($customFields['dvsn_product_shipping_surcharge_sales_channel_shipping_method'])) {
  56.             $query->andWhere('shipping_method_id = :shippingMethodId')
  57.                 ->setParameter('shippingMethodId'Uuid::fromHexToBytes($customFields['dvsn_product_shipping_surcharge_sales_channel_shipping_method']));
  58.         } else {
  59.             $query->andWhere('shipping_method_id IS NULL');
  60.         }
  61.         if (isset($customFields['dvsn_product_shipping_surcharge_sales_channel_country']) && !empty($customFields['dvsn_product_shipping_surcharge_sales_channel_country'])) {
  62.             $query->andWhere('country_id = :countryId')
  63.                 ->setParameter('countryId'Uuid::fromHexToBytes($customFields['dvsn_product_shipping_surcharge_sales_channel_country']));
  64.         } else {
  65.             $query->andWhere('country_id IS NULL');
  66.         }
  67.         if (isset($customFields['dvsn_product_shipping_surcharge_sales_channel_sales_channel']) && !empty($customFields['dvsn_product_shipping_surcharge_sales_channel_sales_channel'])) {
  68.             $query->andWhere('sales_channel_id = :salesChannelId')
  69.                 ->setParameter('salesChannelId'Uuid::fromHexToBytes($customFields['dvsn_product_shipping_surcharge_sales_channel_sales_channel']));
  70.         } else {
  71.             $query->andWhere('sales_channel_id IS NULL');
  72.         }
  73.         $surcharges $query->execute()->fetchAllAssociative();
  74.         $arr = [];
  75.         foreach ($surcharges as $surcharge) {
  76.             $arr[$surcharge['product_id']] = (float) $surcharge['surcharge'];
  77.         }
  78.         $context $event->getContext();
  79.         $context['dvsnProductShippingSurcharge'] = $arr;
  80.         $event->setContext($context);
  81.     }
  82. }