custom/plugins/EnetCredit/src/Subscriber/OrderPlacedSubscriber.php line 41

Open in your IDE?
  1. <?php
  2. namespace EnetCredit\Subscriber;
  3. use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
  4. use Shopware\Core\Checkout\Order\OrderEvents;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class OrderPlacedSubscriber implements EventSubscriberInterface
  10. {
  11.     /** @var EntityRepository $orderRepository */
  12.     private $orderRepository;
  13.     /** @var EntityRepository $customerRepository */
  14.     private $customerRepository;
  15.     /** @var EntityRepository $orderTransactionRepository */
  16.     private $orderTransactionRepository;
  17.     public function __construct(EntityRepository $orderRepositoryEntityRepository $orderTransactionRepositoryEntityRepository $customerRepository)
  18.     {
  19.         $this->orderRepository $orderRepository;
  20.         $this->customerRepository $customerRepository;
  21.         $this->orderTransactionRepository $orderTransactionRepository;
  22.     }
  23.     public static function getSubscribedEvents()
  24.     {
  25.         return [
  26. //            OrderEvents::ORDER_WRITTEN_EVENT => 'onOrderPlaced',
  27.             // TODO: dieses event nutzen, das triggered wenn der paymentstatus sich ändern
  28.             OrderEvents::ORDER_TRANSACTION_WRITTEN_EVENT => 'onOrderPlaced',
  29.         ];
  30.     }
  31.     // TODO: den bezahlstatus auf true setzen
  32.     public function onOrderPlaced(EntityWrittenEvent $event)
  33.     {
  34.         if ($writeResults $event->getWriteResults()) {
  35.             $context $event->getContext();
  36.             if (isset($writeResults[0])) {
  37.                 $payload $writeResults[0]->getPayload();
  38.                 // check for payload existence
  39.                 // when using PayPal the first statusChange has no stateId in payload
  40.                 if(!isset($payload['stateId'])) {
  41.                     return;
  42.                 }
  43.                 // TODO: get status by technical name, not the id!
  44.                 if($payload['stateId'] !== '5f96feede9e94ffbaa98b60c8dfac558') { // paid
  45.                     return;
  46.                 }
  47.                 $orderEntity $this->getOrder($payload$context);
  48.                 $lineItem $orderEntity->getLineItems();
  49.                 $orderCustomerEntity $orderEntity->getOrderCustomer();
  50.                 if(count($lineItem->getElements()) > 0) {
  51.                     foreach($lineItem->getElements() as $lineItemEntity) {
  52.                         $product $lineItemEntity->getProduct();
  53.                         $customFields $product->getCustomFields();
  54.                         if(isset($customFields['enet_credit_product']) && $customFields['enet_credit_product'] === true) {
  55.                             $bonus floatval($customFields['enet_credit_bonus']);
  56.                             $quantity $lineItemEntity->getQuantity();
  57.                             $totalPrice $lineItemEntity->getTotalPrice();
  58.                             $customerAvailCredit $this->getCustomerAvailCredit($orderCustomerEntity$context);
  59.                             $newCreditValue $totalPrice + ($bonus $quantity) + $customerAvailCredit;
  60.                             $this->customerRepository->update(
  61.                                 [
  62.                                     [
  63.                                         'id' => $orderCustomerEntity->getCustomerId(),
  64.                                         'customFields' => ['enet_credit_value' => $newCreditValue]
  65.                                     ]
  66.                                 ],
  67.                                 $context
  68.                             );
  69.                         }
  70.                     }
  71.                 }
  72.             }
  73.         }
  74.     }
  75.     private function getCustomerAvailCredit($orderCustomerEntity$context)
  76.     {
  77.         $orderCustomerId $orderCustomerEntity->getCustomerId();
  78.         $criteria = new Criteria([$orderCustomerId]);
  79.         $customerEntity $this->customerRepository->search($criteria$context)->first();
  80.         $creditValue $customerEntity->getCustomFields();
  81.         if(isset($creditValue['enet_credit_value']) && $creditValue['enet_credit_value']) {
  82.             return floatval($creditValue['enet_credit_value']);
  83.         }
  84.         return 0;
  85.     }
  86.     private function getOrder($payload$context)
  87.     {
  88.         $criteria = new Criteria([$payload['id']]);
  89.         $criteria->addAssociation('order');
  90.         $criteria->addAssociation('order.lineItems');
  91.         $criteria->addAssociation('order.lineItems.product');
  92.         $criteria->addAssociation('order.transactions');
  93.         $orderTransactionResult $this->orderTransactionRepository->search(
  94.             $criteria,
  95.             $context
  96.         );
  97.         return $orderTransactionResult->getElements()[
  98.             array_keys($orderTransactionResult->getElements())[0]
  99.         ]->getOrder();
  100.     }
  101. }