src/AppBundle/EventListener/CheckoutEventListener.php line 112

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Enterprise License (PEL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PEL
  13.  */
  14. namespace AppBundle\EventListener;
  15. use AppBundle\Model\CustomerManagementFramework\Activity\OrderActivity;
  16. use CustomerManagementFrameworkBundle\ActivityManager\ActivityManagerInterface;
  17. use Pimcore\Bundle\EcommerceFrameworkBundle\Factory;
  18. use Pimcore\Event\Model\Ecommerce\CommitOrderProcessorEvent;
  19. use Pimcore\Event\Model\Ecommerce\OrderManagerEvent;
  20. use Pimcore\Event\Model\Ecommerce\SendConfirmationMailEvent;
  21. use Pimcore\Localization\LocaleServiceInterface;
  22. use Pimcore\Mail;
  23. use Pimcore\Model\DataObject\OnlineShopOrder;
  24. use Pimcore\Model\Document\Email;
  25. class CheckoutEventListener
  26. {
  27.     /**
  28.      * @var Factory
  29.      */
  30.     protected $ecommerceFactory;
  31.     /**
  32.      * @var ActivityManagerInterface
  33.      */
  34.     protected $activityManager;
  35.     /**
  36.      * @var LocaleServiceInterface
  37.      */
  38.     protected $localeService;
  39.     /**
  40.      * CheckoutEventListener constructor.
  41.      *
  42.      * @param Factory $ecommerceFactory
  43.      * @param ActivityManagerInterface $activityManager
  44.      */
  45.     public function __construct(Factory $ecommerceFactoryActivityManagerInterface $activityManagerLocaleServiceInterface $localeService)
  46.     {
  47.         $this->ecommerceFactory $ecommerceFactory;
  48.         $this->activityManager $activityManager;
  49.         $this->localeService $localeService;
  50.     }
  51.     /**
  52.      * @param OrderManagerEvent $event
  53.      *
  54.      * @throws \Exception
  55.      */
  56.     public function onUpdateOrder(OrderManagerEvent $event)
  57.     {
  58.         $cart $event->getCart();
  59.         /**
  60.          * @var $order OnlineShopOrder
  61.          */
  62.         $order $event->getOrder();
  63.         $checkout $this->ecommerceFactory->getCheckoutManager($cart);
  64.         $deliveryAddress $checkout->getCheckoutStep('deliveryaddress') ? $checkout->getCheckoutStep('deliveryaddress')->getData() : null;
  65.         if ($deliveryAddress) {
  66.             //inserting delivery and billing address from checkout step delivery
  67.             $order->setCustomerFirstname($deliveryAddress->firstname);
  68.             $order->setCustomerLastname($deliveryAddress->lastname);
  69.             $order->setCustomerCompany($deliveryAddress->company);
  70.             $order->setCustomerStreet($deliveryAddress->street);
  71.             $order->setCustomerZip($deliveryAddress->zip);
  72.             $order->setCustomerCity($deliveryAddress->city);
  73.             $order->setCustomerCountry($deliveryAddress->countryCode);
  74.             $order->setCustomerEmail($deliveryAddress->email);
  75.             $order->setDeliveryFirstname($deliveryAddress->firstname);
  76.             $order->setDeliveryLastname($deliveryAddress->lastname);
  77.             $order->setDeliveryCompany($deliveryAddress->company);
  78.             $order->setDeliveryStreet($deliveryAddress->street);
  79.             $order->setDeliveryZip($deliveryAddress->zip);
  80.             $order->setDeliveryCity($deliveryAddress->city);
  81.             $order->setDeliveryCountry($deliveryAddress->countryCode);
  82.             //updating customer object based on delivery address data
  83.             $customer $order->getCustomer();
  84.             if ($customer) {
  85.                 $params = ['email''firstname''lastname''street''zip''city''countryCode'];
  86.                 foreach ($params as $param) {
  87.                     $customer->{'set' ucfirst($param)}($deliveryAddress->{$param});
  88.                 }
  89.                 $customer->save();
  90.             }
  91.         }
  92.     }
  93.     /**
  94.      * @param SendConfirmationMailEvent $event
  95.      */
  96.     public function sendConfirmationMail(SendConfirmationMailEvent $event)
  97.     {
  98.         $order $event->getOrder();
  99.         $mail = new Mail();
  100.         $mail->setDocument(Email::getByPath('/' $this->localeService->getLocale() . '/mails/confirmation-mail'));
  101.         $mail->setParams([
  102.             'ordernumber' => $order->getOrdernumber(),
  103.             'order' => $order
  104.         ]);
  105.         $mail->addTo($order->getCustomerEMail());
  106.         $mail->send();
  107.         $event->setSkipDefaultBehaviour(true);
  108.     }
  109.     /**
  110.      * @param CommitOrderProcessorEvent $event
  111.      *
  112.      * @throws \Pimcore\Bundle\EcommerceFrameworkBundle\Exception\UnsupportedException
  113.      */
  114.     public function postCommitOrder(CommitOrderProcessorEvent $event)
  115.     {
  116.         $order $event->getOrder();
  117.         if ($this->activityManager && $order->getCustomer()) {
  118.             $this->activityManager->trackActivity(new OrderActivity($order->getCustomer(), $order));
  119.         }
  120.     }
  121. }