/**
  * Products collection.
  *
  * @return array
  */
 public function getLoadedProductCollection()
 {
     $productsToDisplay = [];
     $mode = $this->getRequest()->getActionName();
     $customerId = $this->getRequest()->getParam('customer_id');
     $limit = $this->recommnededHelper->getDisplayLimitByMode($mode);
     //login customer to receive the recent products
     $session = $this->sessionFactory->create();
     $isLoggedIn = $session->loginById($customerId);
     $collection = $this->viewed;
     $productItems = $collection->getItemsCollection()->setPageSize($limit);
     //get the product ids from items collection
     $productIds = $productItems->getColumnValues('product_id');
     //get product collection to check for salable
     $productCollection = $this->productFactory->create()->getCollection()->addAttributeToSelect('*')->addFieldToFilter('entity_id', ['in' => $productIds]);
     //show products only if is salable
     foreach ($productCollection as $product) {
         if ($product->isSalable()) {
             $productsToDisplay[$product->getId()] = $product;
         }
     }
     $this->helper->log('Recentlyviewed customer  : ' . $customerId . ', mode ' . $mode . ', limit : ' . $limit . ', items found : ' . count($productItems) . ', is customer logged in : ' . $isLoggedIn . ', products :' . count($productsToDisplay));
     $session->logout();
     return $productsToDisplay;
 }
 /**
  * Process customer basket.
  */
 public function _handleCustomerBasket()
 {
     $customerSession = $this->sessionFactory->create();
     $configCartUrl = $this->quote->getStore()->getWebsite()->getConfig(\Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_CONTENT_CART_URL);
     //if customer is logged in then redirect to cart
     if ($customerSession->isLoggedIn()) {
         $checkoutSession = $this->checkoutSession->create();
         if ($checkoutSession->getQuote() && $checkoutSession->getQuote()->hasItems()) {
             $quote = $checkoutSession->getQuote();
             if ($this->quote->getId() != $quote->getId()) {
                 $this->_checkMissingAndAdd();
             }
         } else {
             $this->_loadAndReplace();
         }
         if ($configCartUrl) {
             $url = $configCartUrl;
         } else {
             $url = $customerSession->getCustomer()->getStore()->getUrl('checkout/cart');
         }
         $this->_redirect($url);
     } else {
         //set after auth url. customer will be redirected to cart after successful login
         if ($configCartUrl) {
             $cartUrl = $configCartUrl;
         } else {
             $cartUrl = 'checkout/cart';
         }
         $customerSession->setAfterAuthUrl($this->quote->getStore()->getUrl($cartUrl));
         //send customer to login page
         $configLoginUrl = $this->quote->getStore()->getWebsite()->getConfig(\Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_CONTENT_LOGIN_URL);
         if ($configLoginUrl) {
             $loginUrl = $configLoginUrl;
         } else {
             $loginUrl = 'customer/account/login';
         }
         $this->_redirect($this->quote->getStore()->getUrl($loginUrl));
     }
 }