vendor/lnb/shopware6-lnb-subscription/src/Subscriber/Subscription/SubscriptionCreatedSubscriber.php line 85

Open in your IDE?
  1. <?php
  2. /**
  3.  * Permission is hereby granted, free of charge, to any person obtaining a
  4.  * copy of this software and associated documentation files (the "Software"),
  5.  * to deal in the Software without restriction, including without limitation
  6.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  7.  * and/or sell copies of the Software, and to permit persons to whom the
  8.  * Software is furnished to do so, subject to the following conditions:
  9.  *
  10.  * The above copyright notice and this permission notice shall be included in
  11.  * all copies or substantial portions of the Software.
  12.  *
  13.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16.  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18.  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19.  * IN THE SOFTWARE.
  20.  *
  21.  * @package    LnbSubscription
  22.  * @author     Michael Lämmlein <michael.laemmlein@liebscher-bracht.com>
  23.  * @copyright  ©2022 Liebscher & Bracht
  24.  * @license    http://www.opensource.org/licenses/mit-license.php MIT-License
  25.  * @version    1.0.0
  26.  * @since      04.03.22
  27.  */
  28. declare(strict_types=1);
  29. namespace Lnb\Shopware6\LnbSubscription\Subscriber\Subscription;
  30. use Lnb\Shopware6\LnbSubscription\Entity\SubscriptionOrder\SubscriptionOrderEntity;
  31. use Lnb\Shopware6\LnbSubscription\Event\SubscriptionCreatedEvent;
  32. use Shopware\Core\Checkout\Cart\Price\Struct\CalculatedPrice;
  33. use Shopware\Core\Checkout\Cart\Tax\Struct\CalculatedTaxCollection;
  34. use Shopware\Core\Checkout\Cart\Tax\Struct\TaxRuleCollection;
  35. use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
  36. use Shopware\Core\Content\Product\ProductEntity;
  37. use Shopware\Core\Framework\Context;
  38. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  39. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  40. use Shopware\Core\Framework\Event\ShopwareEvent;
  41. use Symfony\Component\DependencyInjection\Attribute\Target;
  42. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  43. class SubscriptionCreatedSubscriber implements EventSubscriberInterface
  44. {
  45.     public function __construct(
  46.         #[Target('order_line_item.repository')] private readonly EntityRepository $orderLineItemRepository,
  47.         #[Target('order.repository')] private readonly EntityRepository $orderRepository,
  48.         #[Target('lnb_subscription_order.repository')] private readonly EntityRepository $subscriptionOrderRepository,
  49.         #[Target('product.repository')] private readonly EntityRepository $productRepository
  50.     ) {
  51.     }
  52.     public static function getSubscribedEvents(): array
  53.     {
  54.         return [
  55.             SubscriptionCreatedEvent::class => [
  56.                 ['addSubscriptionOrderIdToOrderLineItem'5],
  57.                 ['addSubscriptionOrderIdToOrder'6],
  58.                 ['updatePriceIfZero'10]
  59.             ],
  60. //            SubscriptionCreatedFromRequestEvent::class => ['updatePriceIfZero']
  61.         ];
  62.     }
  63.     public function addSubscriptionOrderIdToOrderLineItem(SubscriptionCreatedEvent $event): void
  64.     {
  65.         $criteria = new Criteria([$event->getOrderLineItemId()]);
  66.         /** @var OrderLineItemEntity $orderLineItem */
  67.         $orderLineItem $this->orderLineItemRepository->search($criteria$event->getContext())->first();
  68.         $customFields $orderLineItem->getCustomFields();
  69.         $customFields['LnbSubscription']['lnbSubscriptionOrderId'] = $event->getSubscriptionId();
  70.         $this->orderLineItemRepository->update([[
  71.             'id' => $orderLineItem->getId(),
  72.             'customFields' => $customFields,
  73.         ]], $event->getContext());
  74.     }
  75.     public function addSubscriptionOrderIdToOrder(SubscriptionCreatedEvent $event): void
  76.     {
  77.         $criteria = new Criteria([$event->getOrderLineItemId()]);
  78.         $criteria->addAssociation('order');
  79.         /** @var OrderLineItemEntity $orderLineItem */
  80.         $orderLineItem $this->orderLineItemRepository->search($criteria$event->getContext())->first();
  81.         $customFields $orderLineItem->getOrder()->getCustomFields();
  82.         $customFields['LnbSubscription']['lnbSubscriptionOrderId'] = $event->getSubscriptionId();
  83.         // new field for Ids because lnbSubscriptionOrderId always gets overwritten by the last subscription id, and we want to catch all ids
  84.         if (!isset($customFields['LnbSubscription']['lnbSubscriptionOrderIds']) || !in_array($event->getSubscriptionId(), $customFields['LnbSubscription']['lnbSubscriptionOrderIds'])) {
  85.             $customFields['LnbSubscription']['lnbSubscriptionOrderIds'][] = $event->getSubscriptionId();
  86.         }
  87.         $this->orderRepository->upsert([[
  88.             'id' => $orderLineItem->getOrderId(),
  89.             'customFields' => $customFields,
  90.             'lnbSubscriptions' => [
  91.                 [
  92.                     'id' => $event->getSubscriptionId(),
  93.                 ]
  94.             ],
  95.         ]], $event->getContext());
  96.     }
  97.     public function updatePriceIfZero(ShopwareEvent $event): void
  98.     {
  99.         $subscriptionOrder $this->getSubscriptionOrder($event->getSubscriptionId(), $event->getContext());
  100.         if ($subscriptionOrder->getPrice()->getUnitPrice()) {
  101.             return;
  102.         }
  103.         $product $this->getProduct($subscriptionOrder->getProductId(), $event->getContext());
  104.         $newPrice $product->getCurrencyPrice($event->getContext()->getCurrencyId())->getNet();
  105.         $quantity $subscriptionOrder->getQuantity();
  106.         $calculatedPrice = new CalculatedPrice(
  107.             $newPrice,
  108.             $newPrice $quantity,
  109.             new CalculatedTaxCollection(),
  110.             new TaxRuleCollection(),
  111.             $quantity
  112.         );
  113.         $this->subscriptionOrderRepository->update([[
  114.             'id' => $event->getSubscriptionId(),
  115.             'price' => $calculatedPrice,
  116.         ]], $event->getContext());
  117.     }
  118.     private function getSubscriptionOrder(string $subscriptionOrderIdContext $context): SubscriptionOrderEntity
  119.     {
  120.         $criteria = new Criteria([$subscriptionOrderId]);
  121.         $result $this->subscriptionOrderRepository->search($criteria$context);
  122.         $entity $result->first();
  123.         if (!$entity) {
  124.             throw new \Exception('no subscription order found');
  125.         }
  126.         return $entity;
  127.     }
  128.     private function getProduct(string $productIdContext $context): ProductEntity
  129.     {
  130.         $criteria = new Criteria([$productId]);
  131.         $result $this->productRepository->search($criteria$context);
  132.         /** @var ?ProductEntity $entity */
  133.         $entity $result->first();
  134.         if (!$entity) {
  135.             throw new \Exception('no product found');
  136.         }
  137.         return $entity;
  138.     }
  139. }