vendor/shopware/core/Checkout/Promotion/Subscriber/PromotionIndividualCodeRedeemer.php line 39

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