custom/plugins/NetiNextEasyCouponDesigns/src/Core/Checkout/Cart/ValidateSelectedDesign/CartCollector.php line 34

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace NetInventors\NetiNextEasyCouponDesigns\Core\Checkout\Cart\ValidateSelectedDesign;
  4. use NetInventors\NetiNextEasyCouponDesigns\Service\PluginConfig;
  5. use NetInventors\NetiNextEasyCouponDesigns\Struct\LineItemStruct;
  6. use NetInventors\NetiNextEasyCouponDesigns\Struct\SelectedDesignCollection;
  7. use Shopware\Core\Checkout\Cart\Cart;
  8. use Shopware\Core\Checkout\Cart\CartBehavior;
  9. use Shopware\Core\Checkout\Cart\CartDataCollectorInterface;
  10. use Shopware\Core\Checkout\Cart\LineItem\CartDataCollection;
  11. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  12. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  14. use Shopware\Core\Framework\Uuid\Uuid;
  15. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  16. class CartCollector extends AbstractCartValidator implements CartDataCollectorInterface
  17. {
  18.     public const CACHE_KEY 'easy-coupon-designs-products-with-selected-designs';
  19.     protected PluginConfig              $pluginConfig;
  20.     protected EntityRepositoryInterface $designRepository;
  21.     public function __construct(PluginConfig $pluginConfigEntityRepositoryInterface $designRepository)
  22.     {
  23.         $this->pluginConfig     $pluginConfig;
  24.         $this->designRepository $designRepository;
  25.     }
  26.     public function collect(
  27.         CartDataCollection  $data,
  28.         Cart                $original,
  29.         SalesChannelContext $context,
  30.         CartBehavior        $behavior
  31.     ): void {
  32.         if (!$this->pluginConfig->isActive()) {
  33.             return;
  34.         }
  35.         $selected = new SelectedDesignCollection();
  36.         $products $original->getLineItems()->filterType(LineItem::PRODUCT_LINE_ITEM_TYPE);
  37.         foreach ($products as $product) {
  38.             if (!$product->hasPayloadValue(LineItemStruct::PAYLOAD_NAME)) {
  39.                 continue;
  40.             }
  41.             $designsSettings $product->getPayloadValue(LineItemStruct::PAYLOAD_NAME);
  42.             if (
  43.                 isset($designsSettings['selectedDesign'])
  44.                 && (
  45.                     !\is_string($designsSettings['selectedDesign'])
  46.                     || '' === $designsSettings['selectedDesign']
  47.                 )
  48.             ) {
  49.                 continue;
  50.             }
  51.             if (!isset($designsSettings['selectedDesign']) || !Uuid::isValid($designsSettings['selectedDesign'])) {
  52.                 $this->handleNotExistingDesign($original$original$product->getId());
  53.                 continue;
  54.             }
  55.             $selected->set($product->getId(), $designsSettings['selectedDesign']);
  56.         }
  57.         $selectedIds $selected->getElements();
  58.         $designs $this->designRepository->search(
  59.             new Criteria(\array_values($selectedIds)),
  60.             $context->getContext()
  61.         );
  62.         $data->set(self::CACHE_KEY$designs);
  63.     }
  64. }