src/EventListener/CheckoutEventListener.php line 65

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 App\EventListener;
  15. use App\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;
  24. use Pimcore\Model\DataObject\OnlineShopOrder;
  25. use Pimcore\Model\Document\Email;
  26. class CheckoutEventListener
  27. {
  28.     /**
  29.      * @var Factory
  30.      */
  31.     protected $ecommerceFactory;
  32.     /**
  33.      * @var ActivityManagerInterface
  34.      */
  35.     protected $activityManager;
  36.     /**
  37.      * @var LocaleServiceInterface
  38.      */
  39.     protected $localeService;
  40.     /**
  41.      * CheckoutEventListener constructor.
  42.      *
  43.      * @param Factory $ecommerceFactory
  44.      * @param ActivityManagerInterface $activityManager
  45.      */
  46.     public function __construct(Factory $ecommerceFactoryActivityManagerInterface $activityManagerLocaleServiceInterface $localeService)
  47.     {
  48.         $this->ecommerceFactory $ecommerceFactory;
  49.         $this->activityManager $activityManager;
  50.         $this->localeService $localeService;
  51.     }
  52.     /**
  53.      * @param OrderManagerEvent $event
  54.      *
  55.      * @throws \Exception
  56.      */
  57.     public function onUpdateOrder(OrderManagerEvent $event)
  58.     {
  59.         $cart $event->getCart();
  60.         /**
  61.          * @var $order OnlineShopOrder
  62.          */
  63.         $order $event->getOrder();
  64.         $checkout $this->ecommerceFactory->getCheckoutManager($cart);
  65.         $deliveryAddress $checkout->getCheckoutStep('deliveryaddress') ? $checkout->getCheckoutStep('deliveryaddress')->getData() : null;
  66.         $billingAddress $checkout->getCheckoutStep('billingaddress') ? $checkout->getCheckoutStep('billingaddress')->getData() : null;
  67.         $collectInfo $checkout->getCheckoutStep('collectInfo') ? $checkout->getCheckoutStep('collectInfo')->getData() : null;
  68.         if ($collectInfo) {
  69.             $data $collectInfo;
  70.             //inserting customer info from checkout step delivery
  71.             $order->setCustomerFirstname(property_exists($data'firstname') ? $data->firstname '');
  72.             $order->setCustomerLastname(property_exists($data'lastname') ? $data->lastname '');
  73.             $order->setCustomerCompany(property_exists($data'company') ? $data->company '');
  74.             /*  $order->setCustomerStreet($data->street);
  75.             $order->setCustomerZip($data->zip);
  76.             $order->setCustomerCity($data->city);
  77.             $order->setCustomerCountry($data->countryCode); */
  78.             $order->setCustomerEmail($data->email);
  79.             $order->setCustomerOrderedBy(property_exists($data'orderedBy') ? $data->orderedBy '');
  80.             $order->setCustomerHcpc(property_exists($data'hcpc') && $data->hcpc $data->hcpc '');
  81.             //updating customer object based on delivery address data
  82.             /* $customer = $order->getCustomer();
  83.             if ($customer) {
  84.                 $params = ['email', 'firstname', 'lastname', 'street', 'zip', 'city', 'countryCode'];
  85.                 foreach ($params as $param) {
  86.                     $customer->{'set' . ucfirst($param)}($deliveryAddress->{$param});
  87.                 }
  88.                 $customer->save();
  89.             } */
  90.         }
  91.         if ($deliveryAddress) {
  92.             $data $deliveryAddress;
  93.             //inserting delivery and billing address from checkout step delivery
  94.             $order->setDeliveryFirstname(property_exists($data'firstname') ? $data->firstname '');
  95.             $order->setDeliveryLastname(property_exists($data'lastname') ? $data->lastname '');
  96.             $order->setDeliveryCompany($data->company);
  97.             $order->setDeliveryLine1($data->line1);
  98.             $order->setDeliveryLine2($data->line2);
  99.             $order->setDeliveryComment($data->comment);
  100.             /* $order->setDeliveryContact($data->contact); */
  101.             $order->setDeliveryPhone($data->phone);
  102.             $order->setDeliveryZip($data->zip);
  103.             $order->setDeliveryCity($data->city);
  104.             $order->setDeliveryCounty($data->county);
  105.             $order->setDeliveryCountry($data->countryCode);
  106.         }
  107.         if ($billingAddress) {
  108.             $data $billingAddress;
  109.             //inserting delivery and billing address from checkout step delivery
  110.             $order->setBillingFirstname(property_exists($data'firstname') ? $data->firstname '');
  111.             $order->setBillingLastname(property_exists($data'lastname') ? $data->lastname '');
  112.             $order->setBillingCompany($data->company);
  113.             $order->setBillingLine1($data->line1);
  114.             $order->setBillingLine2($data->line2);
  115.             $order->setBillingZip($data->zip);
  116.             $order->setBillingCity($data->city);
  117.             $order->setBillingCounty($data->county);
  118.             $order->setBillingCountry($data->countryCode);
  119.         }
  120.         //save sign-up or rejoin data to order
  121.         $order->setHiddenData(json_encode($collectInfo));
  122.         $order->save();
  123.         //attach order to customer
  124.         $customer $order->getCustomer();
  125.         if ($customer) {
  126.             $customer->setGeneralOrders(array_merge($customer->getGeneralOrders(), [$order]));
  127.             $customer->save();
  128.         }
  129.        
  130.     }
  131.     /**
  132.      * @param SendConfirmationMailEvent $event
  133.      */
  134.     public function sendConfirmationMail(SendConfirmationMailEvent $event)
  135.     {
  136.         $order $event->getOrder();
  137.         $mail = new Mail();
  138.         $mail->setDocument(Email::getByPath('/' $this->localeService->getLocale() . '/mails/confirmation-mail'));
  139.         
  140.         $mail->setParams([
  141.             'ordernumber' => $order->getOrdernumber(),
  142.             'order' => $order
  143.         ]);
  144.         $customer DataObject::getById(intval($order->getCustomerOrderedBy()));
  145.         $mail->addTo($order->getCustomerEMail());
  146.         if ($customer) {
  147.             if ($customer->getEmail() != $order->getCustomerEMail()) {
  148.                 $mail->addCc($customer->getEmail());
  149.             }
  150.         }
  151.         $mail->send();
  152.         $event->setSkipDefaultBehaviour(false);
  153.     }
  154.     /**
  155.      * @param CommitOrderProcessorEvent $event
  156.      *
  157.      * @throws \Pimcore\Bundle\EcommerceFrameworkBundle\Exception\UnsupportedException
  158.      */
  159.     public function postCommitOrder(CommitOrderProcessorEvent $event)
  160.     {
  161.         $order $event->getOrder();
  162.         if ($this->activityManager && $order->getCustomer()) {
  163.             $this->activityManager->trackActivity(new OrderActivity($order->getCustomer(), $order));
  164.         }
  165.     }
  166. }