custom/plugins/WbfkExtensions/src/Core/Content/Product/Cms/BuyBoxCmsElementResolverDecorator.php line 78

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace WbfkExtensions\Core\Content\Product\Cms;
  4. use Exception;
  5. use Shopware\Core\Content\Cms\Aggregate\CmsSlot\CmsSlotEntity;
  6. use Shopware\Core\Content\Cms\DataResolver\CriteriaCollection;
  7. use Shopware\Core\Content\Cms\DataResolver\Element\ElementDataCollection;
  8. use Shopware\Core\Content\Cms\DataResolver\ResolverContext\ResolverContext;
  9. use Shopware\Core\Content\Cms\SalesChannel\Struct\BuyBoxStruct;
  10. use Shopware\Core\Content\Product\Cms\BuyBoxCmsElementResolver;
  11. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductDefinition;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  13. use Wbfk\Bundles\Service\BundleProductDeliveryAndAvailabilityCalculator;
  14. use WbfkExtensions\Core\Checkout\Cart\Delivery\ShippingAndDeliveryInformationService;
  15. class BuyBoxCmsElementResolverDecorator extends BuyBoxCmsElementResolver
  16. {
  17. public function __construct(
  18. private readonly BuyBoxCmsElementResolver $decorated,
  19. protected readonly ShippingAndDeliveryInformationService $shippingAndDeliveryInformationService,
  20. protected readonly ?BundleProductDeliveryAndAvailabilityCalculator $bundleProductDeliveryAndAvailabilityCalculator
  21. ) {
  22. }
  23. public function getDecorated(): BuyBoxCmsElementResolver
  24. {
  25. return $this->decorated;
  26. }
  27. public function collect(CmsSlotEntity $slot, ResolverContext $resolverContext): ?CriteriaCollection
  28. {
  29. $criteriaCollection = $this->decorated->collect($slot, $resolverContext);
  30. if ($criteriaCollection === null) {
  31. return null;
  32. }
  33. $productCriteriaCollection = $criteriaCollection->all()[SalesChannelProductDefinition::class] ?? [];
  34. // Add necessary criteria to find delivery dates based on m24 delivery date calculation
  35. foreach ($productCriteriaCollection as $criteria) {
  36. $criteria->addAssociation('productSuppliers.wbfkSupplier.deliveryTimes');
  37. $criteria->getAssociation('productSuppliers')->addFilter(
  38. new EqualsFilter('wbfkSupplier.isActive', true),
  39. new EqualsFilter('active', true)
  40. );
  41. $criteria->addAssociation('supplierDeliveryTimes');
  42. $criteria->getAssociation('supplierDeliveryTimes')->addFilter(
  43. new EqualsFilter('countryId', $resolverContext->getSalesChannelContext()->getShippingLocation()->getCountry()->getId())
  44. );
  45. }
  46. return $criteriaCollection;
  47. }
  48. /**
  49. * @throws Exception
  50. */
  51. public function enrich(CmsSlotEntity $slot, ResolverContext $resolverContext, ElementDataCollection $result): void
  52. {
  53. $this->decorated->enrich($slot, $resolverContext, $result);
  54. /** @var BuyBoxStruct $buyBoxStruct */
  55. $buyBoxStruct = $slot->getData();
  56. if (!$buyBoxStruct instanceof BuyBoxStruct) {
  57. return;
  58. }
  59. $product = $buyBoxStruct->getProduct();
  60. if (!$product) {
  61. return;
  62. }
  63. if (($product->getExtension('wbfk_bundle_product')?->count() ?? false) && ($this->bundleProductDeliveryAndAvailabilityCalculator !== null)) {
  64. $this->bundleProductDeliveryAndAvailabilityCalculator->calculatedDeliveryAndAvailability($product, $resolverContext->getSalesChannelContext());
  65. return;
  66. }
  67. $product->addExtension(
  68. 'shippingAndDeliveryInformations',
  69. $this->shippingAndDeliveryInformationService->getShippingAndDeliveryInformationSortedBySupplierPriority($product, $resolverContext->getSalesChannelContext())
  70. );
  71. }
  72. }