platform/src/Core/Checkout/Promotion/Subscriber/PromotionIndividualCodeRedeemer.php line 43

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Shopware\Core\Checkout\Promotion\Subscriber;
  4. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  5. use Shopware\Core\Checkout\Order\Aggregate\OrderCustomer\OrderCustomerEntity;
  6. use Shopware\Core\Checkout\Promotion\Aggregate\PromotionIndividualCode\PromotionIndividualCodeCollection;
  7. use Shopware\Core\Checkout\Promotion\Aggregate\PromotionIndividualCode\PromotionIndividualCodeEntity;
  8. use Shopware\Core\Checkout\Promotion\Cart\PromotionProcessor;
  9. use Shopware\Core\Checkout\Promotion\Exception\CodeAlreadyRedeemedException;
  10. use Shopware\Core\Checkout\Promotion\Exception\PromotionCodeNotFoundException;
  11. use Shopware\Core\Framework\Context;
  12. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. class PromotionIndividualCodeRedeemer implements EventSubscriberInterface
  18. {
  19.     /**
  20.      * @var EntityRepositoryInterface
  21.      */
  22.     private $codesRepository;
  23.     public function __construct(EntityRepositoryInterface $codesRepository)
  24.     {
  25.         $this->codesRepository $codesRepository;
  26.     }
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         return [
  30.             CheckoutOrderPlacedEvent::class => 'onOrderPlaced',
  31.         ];
  32.     }
  33.     /**
  34.      * @throws CodeAlreadyRedeemedException
  35.      * @throws InconsistentCriteriaIdsException
  36.      */
  37.     public function onOrderPlaced(CheckoutOrderPlacedEvent $event): void
  38.     {
  39.         foreach ($event->getOrder()->getLineItems() as $item) {
  40.             // only update promotions in here
  41.             if ($item->getType() !== PromotionProcessor::LINE_ITEM_TYPE) {
  42.                 continue;
  43.             }
  44.             /** @var string $code */
  45.             $code $item->getPayload()['code'];
  46.             try {
  47.                 // first try if its an individual
  48.                 // if not, then it might be a global promotion
  49.                 $individualCode $this->getIndividualCode($code$event->getContext());
  50.             } catch (PromotionCodeNotFoundException $ex) {
  51.                 $individualCode null;
  52.             }
  53.             // if we did not use an individual code we might have
  54.             // just used a global one or anything else, so just quit in this case.
  55.             if (!($individualCode instanceof PromotionIndividualCodeEntity)) {
  56.                 return;
  57.             }
  58.             /** @var OrderCustomerEntity $customer */
  59.             $customer $event->getOrder()->getOrderCustomer();
  60.             // set the code to be redeemed
  61.             // and assign all required meta data
  62.             // for later needs
  63.             $individualCode->setRedeemed(
  64.                 $item->getOrderId(),
  65.                 $customer->getCustomerId(),
  66.                 $customer->getFirstName() . ' ' $customer->getLastName()
  67.             );
  68.             // save in database
  69.             $this->codesRepository->update(
  70.                 [
  71.                     [
  72.                         'id' => $individualCode->getId(),
  73.                         'payload' => $individualCode->getPayload(),
  74.                     ],
  75.                 ],
  76.                 $event->getContext()
  77.             );
  78.         }
  79.     }
  80.     /**
  81.      * Gets all individual code entities for the provided code value.
  82.      *
  83.      * @throws PromotionCodeNotFoundException
  84.      * @throws InconsistentCriteriaIdsException
  85.      */
  86.     private function getIndividualCode(string $codeContext $context): PromotionIndividualCodeEntity
  87.     {
  88.         $criteria = new Criteria();
  89.         $criteria->addFilter(
  90.             new EqualsFilter('code'$code)
  91.         );
  92.         /** @var PromotionIndividualCodeCollection $result */
  93.         $result $this->codesRepository->search($criteria$context)->getEntities();
  94.         if (\count($result->getElements()) <= 0) {
  95.             throw new PromotionCodeNotFoundException($code);
  96.         }
  97.         // return first element
  98.         return $result->first();
  99.     }
  100. }