/**
  * Basket items.
  *
  * @return mixed
  */
 public function getBasketItems()
 {
     $params = $this->getRequest()->getParams();
     if (!isset($params['quote_id']) || !isset($params['code'])) {
         $this->helper->log('Basket no quote id or code is set');
         return false;
     }
     $quoteId = $params['quote_id'];
     $quoteModel = $this->quoteFactory->create()->loadByIdWithoutStore($quoteId);
     //check for any quote for this email, don't want to render further
     if (!$quoteModel->getId()) {
         $this->helper->log('no quote found for ' . $quoteId);
         return false;
     }
     if (!$quoteModel->getIsActive()) {
         $this->helper->log('Cart is not active : ' . $quoteId);
         return false;
     }
     $this->quote = $quoteModel;
     //Start environment emulation of the specified store
     $storeId = $quoteModel->getStoreId();
     $appEmulation = $this->emulationFactory->create();
     $appEmulation->startEnvironmentEmulation($storeId);
     $quoteItems = $quoteModel->getAllItems();
     $itemsData = [];
     foreach ($quoteItems as $quoteItem) {
         //skip configurable products
         if ($quoteItem->getParentItemId() != null) {
             continue;
         }
         $_product = $quoteItem->getProduct();
         $inStock = $_product->isInStock() ? 'In Stock' : 'Out of stock';
         $total = $this->priceHelper->currency($quoteItem->getPrice());
         $productUrl = $_product->getProductUrl();
         $grandTotal = $this->priceHelper->currency($this->getGrandTotal());
         $itemsData[] = ['grandTotal' => $grandTotal, 'total' => $total, 'inStock' => $inStock, 'productUrl' => $productUrl, 'product' => $_product, 'qty' => $quoteItem->getQty()];
     }
     return $itemsData;
 }
 /**
  * Save/reset the order as transactional data.
  *
  * @param \Magento\Framework\Event\Observer $observer
  *
  * @return $this
  *
  * @throws \Magento\Framework\Exception\LocalizedException
  */
 public function execute(\Magento\Framework\Event\Observer $observer)
 {
     try {
         $order = $observer->getEvent()->getOrder();
         $status = $order->getStatus();
         $storeId = $order->getStoreId();
         $store = $this->storeManager->getStore($storeId);
         $storeName = $store->getName();
         $websiteId = $store->getWebsiteId();
         $customerEmail = $order->getCustomerEmail();
         // start app emulation
         $appEmulation = $this->emulationFactory->create();
         $initialEnvironmentInfo = $appEmulation->startEnvironmentEmulation($storeId);
         $emailOrder = $this->emailOrderFactory->create()->loadByOrderId($order->getEntityId(), $order->getQuoteId());
         //reimport email order
         $emailOrder->setUpdatedAt($order->getUpdatedAt())->setCreatedAt($order->getUpdatedAt())->setStoreId($storeId)->setOrderStatus($status);
         if ($emailOrder->getEmailImported() != \Dotdigitalgroup\Email\Model\Contact::EMAIL_CONTACT_IMPORTED) {
             $emailOrder->setEmailImported(null);
         }
         //if api is not enabled
         if (!$store->getWebsite()->getConfig(\Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_API_ENABLED)) {
             return $this;
         }
         // check for order status change
         $statusBefore = $this->registry->registry('sales_order_status_before');
         if ($status != $statusBefore) {
             //If order status has changed and order is already imported then set modified to 1
             if ($emailOrder->getEmailImported() == \Dotdigitalgroup\Email\Model\Contact::EMAIL_CONTACT_IMPORTED) {
                 $emailOrder->setModified(\Dotdigitalgroup\Email\Model\Contact::EMAIL_CONTACT_IMPORTED);
             }
         }
         // set back the current store
         $appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo);
         $emailOrder->save();
         //@codingStandardsIgnoreStart
         //Status check automation enrolment
         $configStatusAutomationMap = unserialize($this->scopeConfig->getValue(\Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_AUTOMATION_STUDIO_ORDER_STATUS, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $order->getStore()));
         //@codingStandardsIgnoreEnd
         if (!empty($configStatusAutomationMap)) {
             foreach ($configStatusAutomationMap as $configMap) {
                 if ($configMap['status'] == $status) {
                     //send to automation queue
                     $this->doAutomationEnrolment(['programId' => $configMap['automation'], 'automationType' => 'order_automation_' . $status, 'email' => $customerEmail, 'order_id' => $order->getId(), 'website_id' => $websiteId, 'store_name' => $storeName]);
                 }
             }
         }
         //If customer's first order
         if ($order->getCustomerId()) {
             $orders = $this->orderCollectionFactory->create()->addFieldToFilter('customer_id', $order->getCustomerId());
             if ($orders->getSize() == 1) {
                 $automationTypeNewOrder = \Dotdigitalgroup\Email\Model\Sync\Automation::AUTOMATION_TYPE_CUSTOMER_FIRST_ORDER;
                 $programIdNewOrder = $this->helper->getAutomationIdByType('XML_PATH_CONNECTOR_AUTOMATION_STUDIO_FIRST_ORDER', $order->getWebsiteId());
                 //send to automation queue
                 $this->doAutomationEnrolment(['programId' => $programIdNewOrder, 'automationType' => $automationTypeNewOrder, 'email' => $customerEmail, 'order_id' => $order->getId(), 'website_id' => $websiteId, 'store_name' => $storeName]);
             }
         }
         //admin oder when editing the first one is canceled
         $this->registry->unregister('sales_order_status_before');
     } catch (\Exception $e) {
         throw new \Magento\Framework\Exception\LocalizedException(__($e->getMessage()));
     }
     return $this;
 }