custom/plugins/WbfkExtensions/src/Subscriber/AddPurchasePricesOnCartConversion.php line 32

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace WbfkExtensions\Subscriber;
  4. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  5. use Shopware\Core\Checkout\Cart\Order\CartConvertedEvent;
  6. use Shopware\Core\Content\Product\ProductEntity;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use WbfkExtensions\Core\Checkout\WbfkOrderLineItemExtension\WbfkOrderLineItemExtensionEntity;
  11. class AddPurchasePricesOnCartConversion implements EventSubscriberInterface
  12. {
  13.     public function __construct(private readonly EntityRepository $productRepository)
  14.     {
  15.     }
  16.     /**
  17.      * @inheritDoc
  18.      */
  19.     public static function getSubscribedEvents(): array
  20.     {
  21.         return [
  22.             CartConvertedEvent::class => 'addPurchasePricesToConvertedCart',
  23.         ];
  24.     }
  25.     public function addPurchasePricesToConvertedCart(CartConvertedEvent $event): void
  26.     {
  27.         $convertedCart $event->getConvertedCart();
  28.         $cartLineItems $event->getCart()->getLineItems();
  29.         // Find products whose purchase price needs to be fetched
  30.         $productIdsWhosePurchasePricesAreNotSet = [];
  31.         foreach ($cartLineItems->filterFlatByType(LineItem::PRODUCT_LINE_ITEM_TYPE) as $cartLineItem) {
  32.             /** @var ?WbfkOrderLineItemExtensionEntity $wbfkOrderLineItemExtension */
  33.             $wbfkOrderLineItemExtension $cartLineItem->getExtension('wbfkOrderLineItemExtension');
  34.             if ($wbfkOrderLineItemExtension === null || $wbfkOrderLineItemExtension->getPurchasePrice() === null) {
  35.                 $productIdsWhosePurchasePricesAreNotSet[$cartLineItem->getId()] = $cartLineItem->getReferencedId();
  36.             }
  37.         }
  38.         $products null;
  39.         if (count($productIdsWhosePurchasePricesAreNotSet) > 0) {
  40.             $products $this->productRepository->search(new Criteria($productIdsWhosePurchasePricesAreNotSet), $event->getContext());
  41.         }
  42.         // Set wbfkOrderLineItemExtension to converted cart, after setting missing purchase prices
  43.         foreach ($convertedCart['lineItems'] as $key => $lineItem) {
  44.             if ($lineItem['type'] !== LineItem::PRODUCT_LINE_ITEM_TYPE) {
  45.                 continue;
  46.             }
  47.             // check if line item already has wbfk order line item extension (maybe already added from some other place)
  48.             $extension $lineItem['extensions']['wbfkOrderLineItemExtension'] ?? null;
  49.             if ($extension === null) {
  50.                 $cartLineItemExtension $cartLineItems->get($lineItem['identifier'])->getExtension('wbfkOrderLineItemExtension') ?? (new WbfkOrderLineItemExtensionEntity());
  51.                 $data $cartLineItemExtension->getVars();
  52.                 if (isset($datap['taricId'])) {
  53.                     $extension['taricId'] = $data['taricId'];
  54.                 }
  55.                 if (isset($data['weight'])) {
  56.                     $extension['weight'] = $data['weight'];
  57.                 }
  58.                 if (isset($data['countryOfOrigin'])) {
  59.                     $extension['countryOfOrigin'] = $data['countryOfOrigin'];
  60.                 }
  61.                 if (isset($data['serialNumbers'])) {
  62.                     $extension['serialNumbers'] = $data['serialNumbers'];
  63.                 }
  64.                 if (isset($data['purchasePrice'])) {
  65.                     $extension['purchasePrice'] = $data['purchasePrice'];
  66.                 }
  67.                 if (isset($data['id'])) {
  68.                     $extension['id'] = $data['id'];
  69.                 }
  70.             }
  71.             /*
  72.              *  Note: On an offer cart, admin might have already set the purchase price and comment.
  73.              *  This needs to be preserved in the converted cart.
  74.              *  In an offer cart, this info is available as part of line item payload specialPurchasePrice, specialPurchasePriceComment.
  75.              */
  76.             /** @var ?ProductEntity $product */
  77.             $product $products?->get($lineItem['referencedId']);
  78.             if ($product) {
  79.                 if (!isset($extension['purchasePrice'])) {
  80.                     $extension['purchasePrice'] = $lineItem['payload']['specialPurchasePrice'] ?? $product->getPurchasePrices()->first()->getNet();
  81.                 }
  82.                 if (
  83.                     !isset($convertedCart['lineItems'][$key]['customFields']['wbfk_purchase_price_comment']) ||
  84.                     !$convertedCart['lineItems'][$key]['customFields']['wbfk_purchase_price_comment']
  85.                 ) {
  86.                     $convertedCart['lineItems'][$key]['customFields']['wbfk_purchase_price_comment'] = $lineItem['payload']['specialPurchasePriceComment'] ?? ($product->getCustomFields() ? $product->getCustomFields()['wbfk_cheapest_supplier_name_and_comment'] ?? null null);
  87.                 }
  88.             }
  89.             $convertedCart['lineItems'][$key]['extensions']['wbfkOrderLineItemExtension'] = $extension;
  90.         }
  91.         $event->setConvertedCart($convertedCart);
  92.     }
  93. }