<?php declare(strict_types=1);
/**
* digitvision
*
* @category digitvision
* @package Shopware\Plugins\DvsnProductShippingSurcharge
* @copyright (c) 2021 digitvision
*/
namespace Dvsn\ProductShippingSurcharge\Subscriber\Core\Content;
use Doctrine\DBAL\Connection;
use Shopware\Core\Content\ProductExport\Event\ProductExportRenderBodyContextEvent;
use Shopware\Core\Content\ProductExport\ProductExportEntity;
use Shopware\Core\Defaults;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ProductExportSubscriber implements EventSubscriberInterface
{
private Connection $connection;
public function __construct(
Connection $connection
) {
$this->connection = $connection;
}
/**
* {@inheritDoc}
*/
public static function getSubscribedEvents(): array
{
return [
ProductExportRenderBodyContextEvent::class => 'onProductExportRenderBodyContext'
];
}
/**
* ...
*
* @param ProductExportRenderBodyContextEvent $event
*/
public function onProductExportRenderBodyContext(ProductExportRenderBodyContextEvent $event)
{
/** @var SalesChannelContext $salesChannelContext */
$salesChannelContext = $event->getContext()['context'];
/** @var ProductExportEntity $productExport */
$productExport = $event->getContext()['productExport'];
$customFields = (array) $productExport->getSalesChannel()->getCustomFields();
if (!isset($customFields['dvsn_product_shipping_surcharge_sales_channel_status']) || $customFields['dvsn_product_shipping_surcharge_sales_channel_status'] !== true) {
return;
}
$query = $this->connection->createQueryBuilder()
->select('LOWER(HEX(product_id)) AS product_id, surcharge')
->from('dvsn_product_shipping_surcharge')
->where('product_version_id = :versionId')
->setParameter('versionId', Uuid::fromHexToBytes(Defaults::LIVE_VERSION))
->groupBy('product_id');
if (isset($customFields['dvsn_product_shipping_surcharge_sales_channel_shipping_method']) && !empty($customFields['dvsn_product_shipping_surcharge_sales_channel_shipping_method'])) {
$query->andWhere('shipping_method_id = :shippingMethodId')
->setParameter('shippingMethodId', Uuid::fromHexToBytes($customFields['dvsn_product_shipping_surcharge_sales_channel_shipping_method']));
} else {
$query->andWhere('shipping_method_id IS NULL');
}
if (isset($customFields['dvsn_product_shipping_surcharge_sales_channel_country']) && !empty($customFields['dvsn_product_shipping_surcharge_sales_channel_country'])) {
$query->andWhere('country_id = :countryId')
->setParameter('countryId', Uuid::fromHexToBytes($customFields['dvsn_product_shipping_surcharge_sales_channel_country']));
} else {
$query->andWhere('country_id IS NULL');
}
if (isset($customFields['dvsn_product_shipping_surcharge_sales_channel_sales_channel']) && !empty($customFields['dvsn_product_shipping_surcharge_sales_channel_sales_channel'])) {
$query->andWhere('sales_channel_id = :salesChannelId')
->setParameter('salesChannelId', Uuid::fromHexToBytes($customFields['dvsn_product_shipping_surcharge_sales_channel_sales_channel']));
} else {
$query->andWhere('sales_channel_id IS NULL');
}
$surcharges = $query->execute()->fetchAllAssociative();
$arr = [];
foreach ($surcharges as $surcharge) {
$arr[$surcharge['product_id']] = (float) $surcharge['surcharge'];
}
$context = $event->getContext();
$context['dvsnProductShippingSurcharge'] = $arr;
$event->setContext($context);
}
}