/**
  * @param \Magento\Catalog\Model\Product $product
  * @return array
  */
 public function getCategoryIds(\Magento\Catalog\Model\Product $product)
 {
     if (!$this->_config->getItem(Config::SIMILARITY_USE_CATEGORY_FILTER)) {
         return [];
     }
     return $product->getCategoryIds();
 }
 /**
  * Check on customer login if they have any product view to capture
  *
  * @param Observer $observer
  */
 public function execute(Observer $observer)
 {
     if ($this->_config->isEnabled()) {
         $this->_eventClient->saveCustomerData($this->_customerSession->getCustomerId());
         $guestProductViews = $this->_getGuestCustomerProductViews();
         if ($guestProductViews) {
             $this->_sendAllGuestProductViews($guestProductViews);
         }
     }
 }
 /**
  * Send the query to PredictionIO engine for product data set
  *
  * @param array $productIds
  * @param array $categories
  * @param array $whitelist
  * @param array $blacklist
  * @return array|bool
  */
 public function sendQuery(array $productIds, array $categories = [], array $whitelist = [], array $blacklist = [])
 {
     try {
         $data = ['items' => $productIds, 'num' => (int) $this->_config->getProductCount(Config::COMPLEMENTARY_PRODUCT_COUNT)];
         return $this->_engineClient->sendQuery($data);
     } catch (\Exception $e) {
         $this->_logger->addCritical($e);
     }
     return false;
 }
 /**
  * @param $productId
  * @return bool|void
  */
 public function processViews($productId)
 {
     if (!$this->_config->isEnabled()) {
         return false;
     }
     if ($this->_customerSession->isLoggedIn()) {
         return $this->_eventClient->saveCustomerViewProduct($this->_customerSession->getCustomerId(), $productId);
     }
     return false;
 }
 /**
  * Check the customer has an account and send the order product colllection
  * to PredictionIO
  *
  * @param Observer $observer
  */
 public function execute(Observer $observer)
 {
     if (!$this->_config->isEnabled()) {
         return;
     }
     $order = $observer->getEvent()->getOrder();
     $productCollection = $order->getItems();
     if ($this->_customerSession->isLoggedIn()) {
         $this->_sendPurchaseEvent($productCollection);
         return;
     }
 }
 /**
  * Send the query to PredictionIO engine for product data set
  *
  * @param array $productIds
  * @param array $categories
  * @param array $whitelist
  * @param array $blacklist
  * @return array|bool
  */
 public function sendQuery(array $productIds, array $categories = [], array $whitelist = [], array $blacklist = [])
 {
     try {
         $data = ['items' => $productIds, 'num' => (int) $this->_config->getProductCount(Config::SIMILARITY_PRODUCT_COUNT)];
         $this->_addProperties('categories', $data, $categories);
         $this->_addProperties('whitelist', $data, $whitelist);
         $this->_addProperties('blacklist', $data, $blacklist);
         return $this->_engineClient->sendQuery($data);
     } catch (\Exception $e) {
         $this->_logger->addCritical($e);
     }
     return false;
 }
 /**
  * Get the crossell items for the basket page
  * Rewrites the target rules for Enterprise edition
  *
  * @return array
  */
 public function getItemCollection()
 {
     if (!$this->_config->isEnabled() || !$this->_config->getItem(Config::COMPLEMENTARY_REPLACE_RULES)) {
         return parent::getItemCollection();
     }
     $personalisedIds = $this->_crosssell->getProductCollection();
     if (!$personalisedIds) {
         return parent::getItemCollection();
     }
     $collection = $this->_crosssell->getPersonalisedProductCollection($personalisedIds);
     if ($this->_moduleManager->isEnabled('Magento_Checkout')) {
         $this->_addProductAttributesAndPrices($collection);
     }
     $this->_items = [];
     foreach ($collection as $item) {
         $this->_items[] = $item;
     }
     return $this->_items;
 }
 /**
  * Rewrite parent getAllItems method to use PredictionIO results when available
  * Rewrites the target rules for Enterprise edition
  *
  * @return array
  */
 public function getAllItems()
 {
     if (!$this->_config->isEnabled() || !$this->_config->getItem(Config::SIMILARITY_REPLACE_RULES)) {
         return parent::getAllItems();
     }
     $product = $this->_coreRegistry->registry('product');
     $categoryIds = $this->_upsell->getCategoryIds($product);
     $personalisedIds = $this->_upsell->getProductCollection([$product->getId()], $categoryIds);
     if (!$personalisedIds) {
         return parent::getAllItems();
     }
     $collection = $this->_upsell->getPersonalisedProductCollection($personalisedIds);
     if ($this->_moduleManager->isEnabled('Magento_Checkout')) {
         $this->_addProductAttributesAndPrices($collection);
     }
     $items = [];
     foreach ($collection as $product) {
         $product->setDoNotUseCategoryId(true);
         $items[] = $product;
     }
     return $items;
 }
 /**
  * Rewrite parent _prepareData method to use PredictionIO results when available
  *
  * @return $this
  */
 protected function _prepareData()
 {
     if (!$this->_config->isEnabled()) {
         return parent::_prepareData();
     }
     $product = $this->_coreRegistry->registry('product');
     $categoryIds = $this->_upsell->getCategoryIds($product);
     $personalisedIds = $this->_upsell->getProductCollection([$product->getId()], $categoryIds);
     if (!$personalisedIds) {
         return parent::_prepareData();
     }
     $this->_isPredictedResults = true;
     $collection = $this->_upsell->getPersonalisedProductCollection($personalisedIds);
     $this->_itemCollection = $collection;
     if ($this->moduleManager->isEnabled('Magento_Checkout')) {
         $this->_addProductAttributesAndPrices($this->_itemCollection);
     }
     $this->_eventManager->dispatch('catalog_product_upsell', ['product' => $product, 'collection' => $this->_itemCollection, 'limit' => null]);
     foreach ($this->_itemCollection as $product) {
         $product->setDoNotUseCategoryId(true);
     }
     return $this;
 }