custom/plugins/SteamPixelSepa/src/Subscriber/OrderSubscriber.php line 37

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /*
  3.   This subscriber will listen to new incomming orders
  4.   In case a new order is created it will catch the SEPA data and will append it to the order entity.
  5.   Also it will store this data to the customer account.
  6. */
  7. namespace SteamPixelSepa\Sepa\Subscriber;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Shopware\Core\Checkout\Cart\Order\CartConvertedEvent;
  10. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  11. class OrderSubscriber implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var EntityRepositoryInterface
  15.      */
  16.     private $customerRepository;
  17.     public function __construct(
  18.         EntityRepositoryInterface $customerRepository
  19.     ) {
  20.         $this->customerRepository = $customerRepository;
  21.     }
  22.     public static function getSubscribedEvents()
  23.     {
  24.         return [
  25.           CartConvertedEvent::class => 'addCustomFieldsToConvertedCart',
  26.         ];
  27.     }
  28.     public function addCustomFieldsToConvertedCart(CartConvertedEvent $event)
  29.     {
  30.       // Get post data
  31.       $sepa_store = (isset($_POST['sepa_store'])?$_POST['sepa_store']:false);
  32.       $sepa_owner = (isset($_POST['sepa_owner'])?$_POST['sepa_owner']:false);
  33.       $sepa_iban = (isset($_POST['sepa_iban'])?$_POST['sepa_iban']:false);
  34.       $sepa_bic = (isset($_POST['sepa_bic'])?$_POST['sepa_bic']:false);
  35.       $sepa_already = (isset($_POST['sepa_already'])?true:false);
  36.       if($sepa_store !== false) {
  37.         // Store SEPA data to order
  38.         $cart = $event->getConvertedCart();
  39.         $cart['customFields']['custom_order_sepa_owner'] = $sepa_owner;
  40.         $cart['customFields']['custom_order_sepa_iban'] = $sepa_iban;
  41.         $cart['customFields']['custom_order_sepa_bic'] = $sepa_bic;
  42.         $cart['customFields']['custom_order_sepa_already'] = $sepa_already;
  43.         $event->setConvertedCart($cart);
  44.         // Store SEPA data to customer account
  45.         $this->customerRepository->upsert([
  46.             [
  47.                 'id' => $cart['orderCustomer']['customerId'],
  48.                 'customFields' => [
  49.                   'custom_customer_sepa_owner' => $sepa_owner,
  50.                   'custom_customer_sepa_iban' => $sepa_iban,
  51.                   'custom_customer_sepa_bic' => $sepa_bic,
  52.                   'custom_customer_sepa_already' => $sepa_already
  53.                 ]
  54.             ],
  55.         ], $event->getContext());
  56.       }
  57.     }
  58. }