Example #1
0
 /**
  * @return $this
  */
 protected function _prepareData($execute = true)
 {
     if ($this->bxHelperData->isUpsellEnabled()) {
         $products = $this->_coreRegistry->registry('product');
         $config = $this->_scopeConfig->getValue('bxRecommendations/upsell', $this->scopeStore);
         $choiceId = isset($config['widget']) && $config['widget'] != "" ? $config['widget'] : 'complementary';
         try {
             $entity_ids = $this->p13nHelper->getRecommendation($choiceId, $products, 'product', $config['min'], $config['max'], $execute);
         } catch (\Exception $e) {
             $this->bxHelperData->setFallback(true);
             $this->_logger->critical($e);
             return parent::_prepareData();
         }
         if (!$execute) {
             return null;
         }
         if (count($entity_ids) == 0) {
             $entity_ids = array(0);
         }
         $this->_itemCollection = $this->factory->create()->addFieldToFilter('entity_id', $entity_ids)->addAttributeToSelect('*');
         $this->_itemCollection->setVisibility($this->_catalogProductVisibility->getVisibleInCatalogIds());
         $this->_itemCollection->load();
         foreach ($this->_itemCollection as $product) {
             $product->setDoNotUseCategoryId(true);
         }
         return $this;
     }
     return parent::_prepareData();
 }
 /**
  * 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;
 }