<?php
declare(strict_types=1);
namespace WbfkExtensions\Core\Content\Product\Cms;
use Exception;
use Shopware\Core\Content\Cms\Aggregate\CmsSlot\CmsSlotEntity;
use Shopware\Core\Content\Cms\DataResolver\CriteriaCollection;
use Shopware\Core\Content\Cms\DataResolver\Element\ElementDataCollection;
use Shopware\Core\Content\Cms\DataResolver\ResolverContext\ResolverContext;
use Shopware\Core\Content\Cms\SalesChannel\Struct\BuyBoxStruct;
use Shopware\Core\Content\Product\Cms\BuyBoxCmsElementResolver;
use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Wbfk\Bundles\Service\BundleProductDeliveryAndAvailabilityCalculator;
use WbfkExtensions\Core\Checkout\Cart\Delivery\ShippingAndDeliveryInformationService;
class BuyBoxCmsElementResolverDecorator extends BuyBoxCmsElementResolver
{
public function __construct(
private readonly BuyBoxCmsElementResolver $decorated,
protected readonly ShippingAndDeliveryInformationService $shippingAndDeliveryInformationService,
protected readonly ?BundleProductDeliveryAndAvailabilityCalculator $bundleProductDeliveryAndAvailabilityCalculator
) {
}
public function getDecorated(): BuyBoxCmsElementResolver
{
return $this->decorated;
}
public function collect(CmsSlotEntity $slot, ResolverContext $resolverContext): ?CriteriaCollection
{
$criteriaCollection = $this->decorated->collect($slot, $resolverContext);
if ($criteriaCollection === null) {
return null;
}
$productCriteriaCollection = $criteriaCollection->all()[SalesChannelProductDefinition::class] ?? [];
// Add necessary criteria to find delivery dates based on m24 delivery date calculation
foreach ($productCriteriaCollection as $criteria) {
$criteria->addAssociation('productSuppliers.wbfkSupplier.deliveryTimes');
$criteria->getAssociation('productSuppliers')->addFilter(
new EqualsFilter('wbfkSupplier.isActive', true),
new EqualsFilter('active', true)
);
$criteria->addAssociation('supplierDeliveryTimes');
$criteria->getAssociation('supplierDeliveryTimes')->addFilter(
new EqualsFilter('countryId', $resolverContext->getSalesChannelContext()->getShippingLocation()->getCountry()->getId())
);
}
return $criteriaCollection;
}
/**
* @throws Exception
*/
public function enrich(CmsSlotEntity $slot, ResolverContext $resolverContext, ElementDataCollection $result): void
{
$this->decorated->enrich($slot, $resolverContext, $result);
/** @var BuyBoxStruct $buyBoxStruct */
$buyBoxStruct = $slot->getData();
if (!$buyBoxStruct instanceof BuyBoxStruct) {
return;
}
$product = $buyBoxStruct->getProduct();
if (!$product) {
return;
}
if (($product->getExtension('wbfk_bundle_product')?->count() ?? false) && ($this->bundleProductDeliveryAndAvailabilityCalculator !== null)) {
$this->bundleProductDeliveryAndAvailabilityCalculator->calculatedDeliveryAndAvailability($product, $resolverContext->getSalesChannelContext());
return;
}
$product->addExtension(
'shippingAndDeliveryInformations',
$this->shippingAndDeliveryInformationService->getShippingAndDeliveryInformationSortedBySupplierPriority($product, $resolverContext->getSalesChannelContext())
);
}
}