Exemplo n.º 1
0
 /**
  * @return $this|MageCrosssell
  */
 protected function _prepareData($execute = true)
 {
     if ($this->bxHelperData->isCrosssellEnabled()) {
         $products = $this->_coreRegistry->registry('product');
         $config = $this->_scopeConfig->getValue('bxRecommendations/cart', $this->scopeStore);
         $choiceId = isset($config['widget']) && $config['widget'] != "" ? $config['widget'] : 'complementary';
         try {
             $entity_ids = $this->p13nHelper->getRecommendation($choiceId, $products, 'basket', $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->load();
         foreach ($this->_itemCollection as $product) {
             $product->setDoNotUseCategoryId(true);
         }
         return $this;
     }
     return parent::_prepareData();
 }
Exemplo n.º 2
0
 /**
  * Get applied to layer filter items
  *
  * @return Item[]
  */
 public function getFilters()
 {
     try {
         if ($this->bxHelperData->isFilterLayoutEnabled($this->_layer)) {
             $category = $this->_categoryViewBlock->getCurrentCategory();
             if ($category != null && $category->getDisplayMode() == \Magento\Catalog\Model\Category::DM_PAGE) {
                 return parent::getFilters();
             }
             $filters = array();
             $facets = $this->p13nHelper->getFacets();
             if ($facets) {
                 foreach ($this->bxHelperData->getAllFacetFieldNames() as $fieldName) {
                     if ($facets->isSelected($fieldName)) {
                         $filter = $this->objectManager->create("Boxalino\\Intelligence\\Model\\LayerFilterItem");
                         $filter->setFacets($facets);
                         $filter->setFieldName($fieldName);
                         $filters[] = $filter;
                     }
                 }
             }
             return $filters;
         }
         return parent::getFilters();
     } catch (\Exception $e) {
         $this->bxHelperData->setFallback(true);
         $this->_logger->critical($e);
         return parent::getFilters();
     }
 }
Exemplo n.º 3
0
 /**
  * @param bool $execute
  * @return $this|array|null
  */
 public function getItems($execute = true)
 {
     if ($this->bxHelperData->isCrosssellEnabled()) {
         $config = $this->_scopeConfig->getValue('bxRecommendations/cart', $this->scopeStore);
         $products = array();
         foreach ($this->getQuote()->getAllItems() as $item) {
             $product = $item->getProduct();
             if ($product) {
                 $products[] = $product;
             }
         }
         $choiceId = isset($config['widget']) && $config['widget'] != "" ? $config['widget'] : 'basket';
         try {
             $entity_ids = $this->p13nHelper->getRecommendation($choiceId, $products, 'basket', $config['min'], $config['max'], $execute);
         } catch (\Exception $e) {
             $this->bxHelperData->setFallback(true);
             $this->_logger->critical($e);
             return parent::getItems();
         }
         if (!$execute) {
             return null;
         }
         if (count($entity_ids) == 0) {
             $entity_ids = array(0);
         }
         $items = $this->factory->create()->addFieldToFilter('entity_id', $entity_ids)->addAttributeToSelect('*');
         $items->load();
         foreach ($items as $product) {
             $product->setDoNotUseCategoryId(true);
         }
         return $items;
     }
     return parent::getItems();
 }
Exemplo n.º 4
0
 /**
  * @param \Magento\Catalog\Model\Layer $layer
  * @return array|\Magento\Catalog\Model\Layer\Filter\AbstractFilter[]
  */
 public function getFilters(\Magento\Catalog\Model\Layer $layer)
 {
     try {
         if ($this->bxHelperData->isFilterLayoutEnabled($layer) && $this->bxHelperData->isLeftFilterEnabled()) {
             $filters = array();
             $facets = $this->getBxFacets();
             if ($facets) {
                 foreach ($this->bxHelperData->getLeftFacetFieldNames() as $fieldName) {
                     $attribute = $this->objectManager->create("Magento\\Catalog\\Model\\ResourceModel\\Eav\\Attribute");
                     $filter = $this->objectManager->create("Boxalino\\Intelligence\\Model\\Attribute", ['data' => ['attribute_model' => $attribute], 'layer' => $layer]);
                     $filter->setFacets($facets);
                     $filter->setFieldName($fieldName);
                     $filters[] = $filter;
                 }
             }
             return $filters;
         } else {
             return parent::getFilters($layer);
         }
     } catch (\Exception $e) {
         $this->bxHelperData->setFallback(true);
         $this->_logger->critical($e);
         return parent::getFilters($layer);
     }
 }
Exemplo n.º 5
0
 /**
  * @return $this
  */
 protected function _prepareData($execute = true)
 {
     if ($this->bxHelperData->isRelatedEnabled()) {
         $products = $this->_coreRegistry->registry('product');
         $config = $this->_scopeConfig->getValue('bxRecommendations/related', $this->scopeStore);
         $choiceId = isset($config['widget']) && $config['widget'] != "" ? $config['widget'] : 'similar';
         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('*');
         if ($this->moduleManager->isEnabled('Magento_Checkout')) {
             $this->_addProductAttributesAndPrices($this->_itemCollection);
         }
         $this->_itemCollection->setVisibility($this->_catalogProductVisibility->getVisibleInCatalogIds());
         $this->_itemCollection->load();
         foreach ($this->_itemCollection as $product) {
             $product->setDoNotUseCategoryId(true);
         }
         return $this;
     }
     return parent::_prepareData();
 }
Exemplo n.º 6
0
 /**
  * @param string $label
  * @param $value
  * @param int $count
  * @param null $selected
  * @param null $type
  */
 public function addItemData($label, $value, $count, $selected = null, $type = null)
 {
     if ($this->bxDataHelper->isFilterLayoutEnabled($this->_layer)) {
         $this->_itemsData[] = ['label' => $label, 'value' => $value, 'count' => $count, 'selected' => $selected, 'type' => $type];
     } else {
         parent::addItemData($label, $value, $count);
     }
 }
Exemplo n.º 7
0
 /**
  * @return int|mixed
  */
 public function hasSubPhrases()
 {
     if ($this->bxHelperData->isSearchEnabled()) {
         if ($this->subPhrases == null) {
             $this->subPhrases = $this->p13Helper->areThereSubPhrases();
         }
         return $this->subPhrases;
     }
     return false;
 }
Exemplo n.º 8
0
 /**
  * @return string
  */
 public function getScripts()
 {
     $html = '';
     foreach ($this->bxHelperData->getScripts() as $script) {
         $html .= $script;
     }
     if ($this->customerSession->getCustomerId()) {
         $html .= $this->bxHelperData->reportLogin($this->customerSession->getCustomerId());
     }
     return $html;
 }
Exemplo n.º 9
0
 /**
  * @return AbstractCollection|mixed
  */
 public function _getProductCollection()
 {
     try {
         $layer = $this->getLayer();
         if ($this->bxHelperData->isFilterLayoutEnabled($layer)) {
             if (count($this->_productCollection) && !$this->p13nHelper->areThereSubPhrases()) {
                 return $this->_productCollection;
             }
             if ($layer instanceof \Magento\Catalog\Model\Layer\Category) {
                 if ($this->categoryViewBlock->isContentMode()) {
                     return parent::_getProductCollection();
                 }
             }
             if ($this->p13nHelper->areThereSubPhrases()) {
                 $this->queries = $this->p13nHelper->getSubPhrasesQueries();
                 $entity_ids = $this->p13nHelper->getSubPhraseEntitiesIds($this->queries[self::$number]);
                 $entity_ids = array_slice($entity_ids, 0, $this->bxHelperData->getSubPhrasesLimit());
             } else {
                 $entity_ids = $this->p13nHelper->getEntitiesIds();
             }
             if (count($entity_ids) == 0) {
                 $entity_ids = array(0);
             }
             $this->_setupCollection($entity_ids);
             return $this->_productCollection;
         } else {
             return parent::_getProductCollection();
         }
     } catch (\Exception $e) {
         $this->bxHelperData->setFallback(true);
         $this->_logger->critical($e);
         return parent::_getProductCollection();
     }
 }
Exemplo n.º 10
0
 /**
  * @param $event
  * @throws \Exception
  */
 protected function onCategoryPageView($event)
 {
     try {
         $script = $this->bxHelperData->reportCategoryView($event->getCategory()->getId());
         $this->addScript($script);
     } catch (\Exception $e) {
         $this->_logger->critical($e);
     }
 }
Exemplo n.º 11
0
 /**
  * @return \Magento\Framework\Controller\Result\Json|\Magento\Framework\Controller\Result\Redirect|null
  */
 public function aroundExecute()
 {
     $query = $this->request->getParam('q', false);
     if (!$query) {
         /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
         $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
         $resultRedirect->setUrl($this->url->getBaseUrl());
         return $resultRedirect;
     }
     if ($this->bxHelperData->isAutocompleteEnabled()) {
         $responseData = $this->p13nHelper->autocomplete($query, $this->autocompleteHelper);
         /** @var \Magento\Framework\Controller\Result\Json $resultJson */
         $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
         $resultJson->setData($responseData);
         return $resultJson;
     }
     return null;
 }
Exemplo n.º 12
0
 /**
  * Display search result
  *
  * @return void
  */
 public function execute()
 {
     if ($this->bxHelperData->isSearchEnabled()) {
         $this->layerResolver->create(Resolver::CATALOG_LAYER_SEARCH);
         /* @var $query \Magento\Search\Model\Query */
         $query = $this->_queryFactory->get();
         try {
             if ($this->p13Helper->areThereSubPhrases()) {
                 $queries = $this->p13Helper->getSubPhrasesQueries();
                 if (count($queries) == 1) {
                     $this->_redirect('*/*/*', array('_current' => true, '_query' => array('q' => $queries[0])));
                 }
             }
             if ($this->p13Helper->areResultsCorrected()) {
                 $correctedQuery = $this->p13Helper->getCorrectedQuery();
                 $query->setQueryText($correctedQuery);
             }
         } catch (\Exception $e) {
             $this->bxHelperData->setFallback(true);
             $this->_logger->critical($e);
         }
         $query->setStoreId($this->_storeManager->getStore()->getId());
         if ($query->getQueryText() != '') {
             if ($this->_objectManager->get('Magento\\CatalogSearch\\Helper\\Data')->isMinQueryLength()) {
                 $query->setId(0)->setIsActive(1)->setIsProcessed(1);
             } else {
                 $query->saveIncrementalPopularity();
                 if ($query->getRedirect()) {
                     $this->getResponse()->setRedirect($query->getRedirect());
                     return;
                 }
             }
             $this->_objectManager->get('Magento\\CatalogSearch\\Helper\\Data')->checkNotes();
             $this->_view->loadLayout();
             $this->_view->renderLayout();
         } else {
             $this->getResponse()->setRedirect($this->_redirect->getRedirectUrl());
         }
     } else {
         parent::execute();
     }
 }
Exemplo n.º 13
0
 /**
  * @param $widgetName
  * @param array $context
  * @param string $widgetType
  * @param int $minAmount
  * @param int $amount
  * @param bool $execute
  * @return array|void
  */
 public function getRecommendation($widgetName, $context = array(), $widgetType = '', $minAmount = 3, $amount = 3, $execute = true)
 {
     if (!$execute) {
         if (!isset(self::$choiceContexts[$widgetName])) {
             self::$choiceContexts[$widgetName] = array();
         }
         if (in_array(json_encode($context), self::$choiceContexts[$widgetName])) {
             return;
         }
         self::$choiceContexts[$widgetName][] = json_encode($context);
         if ($widgetType == '') {
             $bxRequest = new \com\boxalino\bxclient\v1\BxRecommendationRequest($this->bxHelperData->getLanguage(), $widgetName, $amount);
             $bxRequest->setGroupBy('products_group_id');
             $bxRequest->setMin($minAmount);
             $bxRequest->setFilters($this->getSystemFilters());
             if (isset($context[0])) {
                 $product = $context[0];
                 $bxRequest->setProductContext($this->getEntityIdFieldName(), $product->getId());
             }
             self::$bxClient->addRequest($bxRequest);
         } else {
             if ($minAmount >= 0 && $amount >= 0 && $minAmount <= $amount) {
                 $bxRequest = new \com\boxalino\bxclient\v1\BxRecommendationRequest($this->bxHelperData->getLanguage(), $widgetName, $amount, $minAmount);
                 $bxRequest->setGroupBy('products_group_id');
                 $bxRequest->setFilters($this->getSystemFilters());
                 $bxRequest->setReturnFields(array($this->getEntityIdFieldName()));
                 if ($widgetType === 'basket' && is_array($context)) {
                     $basketProducts = array();
                     foreach ($context as $product) {
                         $basketProducts[] = array('id' => $product->getId(), 'price' => $product->getPrice());
                     }
                     $bxRequest->setBasketProductWithPrices($this->getEntityIdFieldName(), $basketProducts);
                 } elseif ($widgetType === 'product' && !is_array($context)) {
                     $product = $context;
                     $bxRequest->setProductContext($this->getEntityIdFieldName(), $product->getId());
                 } elseif ($widgetType === 'category' && $context != null) {
                     $filterField = "category_id";
                     $filterValues = is_array($context) ? $context : array($context);
                     $filterNegative = false;
                     $bxRequest->addFilter(new BxFilter($filterField, $filterValues, $filterNegative));
                 }
                 self::$bxClient->addRequest($bxRequest);
             }
         }
         return array();
     }
     $count = array_search(json_encode(array($context)), self::$choiceContexts[$widgetName]);
     return $this->getClientResponse()->getHitIds($widgetName, true, $count, 10, $this->getEntityIdFieldName());
 }
Exemplo n.º 14
0
 /**
  * @return int|mixed
  */
 public function hasSubPhrases()
 {
     if ($this->fallback) {
         return 0;
     }
     try {
         if ($this->bxHelperData->isSearchEnabled()) {
             if ($this->subPhrases == null) {
                 $this->subPhrases = $this->p13nHelper->areThereSubPhrases();
             }
             return $this->subPhrases;
         }
     } catch (\Exception $e) {
         $this->fallback = true;
         $this->_logger->critical($e);
     }
     return 0;
 }
 /**
  * @return $this
  */
 protected function _prepareData()
 {
     $context = isset($this->_data['context']) ? $this->_data['context'] : array();
     $entity_ids = array();
     try {
         $entity_ids = $this->p13nHelper->getRecommendation($this->_data['widget'], $context);
     } catch (\Exception $e) {
         $this->bxHelperData->setFallback(true);
         $this->_logger->critical($e);
         return $this;
     }
     if (count($entity_ids) == 0) {
         $entity_ids = array(0);
     }
     $this->_itemCollection = $this->factory->create()->addFieldToFilter('entity_id', $entity_ids)->addAttributeToSelect('*');
     $this->_itemCollection->load();
     foreach ($this->_itemCollection as $product) {
         $product->setDoNotUseCategoryId(true);
     }
     return $this;
 }
Exemplo n.º 16
0
 public function isTopFilterEnabled()
 {
     return $this->bxHelperData->isTopFilterEnabled();
 }
Exemplo n.º 17
0
 /**
  * checks if the block content contains a bxRecommendationBlock reference
  *
  * @see \Magento\Widget\Model\Template\Filter::filter()
  * @param string $value
  * @return \Magento\Framework\Controller\Result\Forward
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public function beforeFilter($subject, $value)
 {
     if (strpos($value, 'BxRecommendationBlock') !== false) {
         $this->_bxHelperData->setCmsBlock($value);
     }
 }
Exemplo n.º 18
0
 /**
  * @return array
  */
 protected function _getItemsData()
 {
     $this->_requestVar = str_replace('bx_products_', '', $this->bxFacets->getFacetParameterName($this->fieldName));
     if (!$this->bxDataHelper->isHierarchical($this->fieldName)) {
         $attributeModel = $this->_config->getAttribute('catalog_product', substr($this->fieldName, 9))->getSource();
         $order = $this->bxDataHelper->getFieldSortOrder($this->fieldName);
         if ($order == 2) {
             $values = $attributeModel->getAllOptions();
             $responseValues = $this->bxDataHelper->useValuesAsKeys($this->bxFacets->getFacetValues($this->fieldName));
             $selectedValues = $this->bxDataHelper->useValuesAsKeys($this->bxFacets->getSelectedValues($this->fieldName));
             foreach ($values as $value) {
                 $label = is_array($value) ? $value['label'] : $value;
                 if (isset($responseValues[$label])) {
                     $facetValue = $responseValues[$label];
                     $selected = isset($selectedValues[$facetValue]) ? true : false;
                     $paramValue = $this->is_bx_attribute ? $this->bxFacets->getFacetValueParameterValue($this->fieldName, $facetValue) : $attributeModel->getOptionId($this->bxFacets->getFacetValueParameterValue($this->fieldName, $facetValue));
                     $this->itemDataBuilder->addItemData($this->tagFilter->filter($this->bxFacets->getFacetValueLabel($this->fieldName, $facetValue)), $selected ? 0 : $paramValue, $this->bxFacets->getFacetValueCount($this->fieldName, $facetValue), $selected, 'flat');
                 }
             }
         } else {
             $selectedValues = $this->bxDataHelper->useValuesAsKeys($this->bxFacets->getSelectedValues($this->fieldName));
             $responseValues = $this->bxFacets->getFacetValues($this->fieldName);
             foreach ($responseValues as $facetValue) {
                 $selected = isset($selectedValues[$facetValue]) ? true : false;
                 $paramValue = $this->is_bx_attribute ? $this->bxFacets->getFacetValueParameterValue($this->fieldName, $facetValue) : $attributeModel->getOptionId($this->bxFacets->getFacetValueParameterValue($this->fieldName, $facetValue));
                 $this->itemDataBuilder->addItemData($this->tagFilter->filter($this->bxFacets->getFacetValueLabel($this->fieldName, $facetValue)), $selected ? 0 : $paramValue, $this->bxFacets->getFacetValueCount($this->fieldName, $facetValue), $selected, 'flat');
             }
         }
     } else {
         $count = 1;
         $facetValues = array();
         $parentCategories = $this->bxFacets->getParentCategories();
         $parentCount = count($parentCategories);
         $value = false;
         foreach ($parentCategories as $key => $parentCategory) {
             if ($count == 1) {
                 $count++;
                 $homeLabel = __("All Categories");
                 $this->itemDataBuilder->addItemData($this->tagFilter->filter($homeLabel), 2, $this->bxFacets->getParentCategoriesHitCount($key), $value, 'home parent');
                 continue;
             }
             if ($parentCount == $count++) {
                 $value = true;
             }
             $this->itemDataBuilder->addItemData($this->tagFilter->filter($parentCategory), $key, $this->bxFacets->getParentCategoriesHitCount($key), $value, 'parent');
         }
         $sortOrder = $this->bxDataHelper->getFieldSortOrder($this->fieldName);
         if ($sortOrder == 2) {
             $facetLabels = $this->bxFacets->getCategoriesKeyLabels();
             $childId = explode('/', end($facetLabels))[0];
             $childParentId = $this->categoryFactory->create()->load($childId)->getParentId();
             end($parentCategories);
             $parentId = key($parentCategories);
             $id = $parentId == null ? 2 : ($parentId == $childParentId ? $parentId : $childParentId);
             $cat = $this->categoryFactory->create()->load($id);
             foreach ($cat->getChildrenCategories() as $category) {
                 if (isset($facetLabels[$category->getName()])) {
                     $facetValues[] = $facetLabels[$category->getName()];
                 }
             }
         }
         if ($facetValues == null) {
             $facetValues = $this->bxFacets->getCategories();
         }
         foreach ($facetValues as $facetValue) {
             $id = $this->bxFacets->getFacetValueParameterValue($this->fieldName, $facetValue);
             if ($sortOrder == 2 || $this->categoryHelper->canShow($this->categoryFactory->create()->load($id))) {
                 $this->itemDataBuilder->addItemData($this->tagFilter->filter($this->bxFacets->getFacetValueLabel($this->fieldName, $facetValue)), $id, $this->bxFacets->getFacetValueCount($this->fieldName, $facetValue), false, $value ? 'children' : 'home');
             }
         }
     }
     return $this->itemDataBuilder->build();
 }