Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 6 additions & 30 deletions src/module-elasticsuite-catalog/Helper/Autocomplete.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@
*/
class Autocomplete extends \Smile\ElasticsuiteCore\Helper\Autocomplete
{
/**
* @var \Magento\CatalogInventory\Api\StockConfigurationInterface
*/
private $stockConfiguration;

/**
* @var MappingHelper
*/
Expand All @@ -39,34 +34,15 @@ class Autocomplete extends \Smile\ElasticsuiteCore\Helper\Autocomplete
/**
* Constructor.
*
* @param Context $context Helper context.
* @param StoreManagerInterface $storeManager Store manager.
* @param StockConfigurationInterface $stockConfiguration Stock Configuration Interface.
* @param MappingHelper $mappingHelper Mapping helper.
* @param Context $context Helper context.
* @param StoreManagerInterface $storeManager Store manager.
* @param MappingHelper $mappingHelper Mapping helper.
*/
public function __construct(
Context $context,
StoreManagerInterface $storeManager,
StockConfigurationInterface $stockConfiguration,
MappingHelper $mappingHelper
) {
public function __construct(Context $context, StoreManagerInterface $storeManager, MappingHelper $mappingHelper)
{
parent::__construct($context, $storeManager);

$this->storeManager = $storeManager;
$this->stockConfiguration = $stockConfiguration;
$this->mappingHelper = $mappingHelper;
}

/**
* Check if Stock configuration allows to display out of stock products
*
* @param int $storeId The store Id. Will use current store if null.
*
* @return bool
*/
public function isShowOutOfStock($storeId = null)
{
return $this->stockConfiguration->isShowOutOfStock($storeId);
$this->mappingHelper = $mappingHelper;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@

use Magento\Search\Model\Autocomplete\DataProviderInterface;
use Magento\Search\Model\Autocomplete\Item as AutocompleteItem;
use Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection as AttributeCollection;
use Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory as AttributeCollectionFactory;
use Smile\ElasticsuiteCatalog\Model\Autocomplete\Product\AttributeConfig;
use Smile\ElasticsuiteCatalog\Model\ResourceModel\Product\Fulltext\Collection as ProductCollection;
use Smile\ElasticsuiteCore\Search\Request\BucketInterface;
use Magento\Store\Model\StoreManagerInterface;
use Smile\ElasticsuiteCatalog\Helper\Autocomplete as AutocompleteHelper;

/**
Expand Down Expand Up @@ -49,19 +46,13 @@ class DataProvider implements DataProviderInterface
private $type;

/**
* @var ProductCollection
* @var AttributeConfig
*/
private $productCollection;

private $attributeConfig;
/**
* @var AttributeCollection
*/
private $attributeCollection;

/**
* @var StoreManagerInterface
* @var ProductCollection
*/
private $storeManager;
private $productCollection;

/**
* @var AutocompleteHelper
Expand All @@ -71,30 +62,24 @@ class DataProvider implements DataProviderInterface
/**
* Constructor.
*
* @param ItemFactory $itemFactory Autocomplete item factory.
* @param ProductCollection $productCollection Autocomplete product collection.
* @param AttributeCollectionFactory $attributeCollectionFactory Product attribute collection factory.
* @param AutocompleteHelper $autocompleteHelper Autocomplete configuration helper.
* @param StoreManagerInterface $storeManager Store manager.
* @param string $type Autocomplete type code.
* @param ItemFactory $itemFactory Autocomplete item factory.
* @param AttributeConfig $attributeConfig Autocomplete attribute config.
* @param ProductCollection $productCollection Autocomplete product collection.
* @param AutocompleteHelper $autocompleteHelper Autocomplete configuration helper.
* @param string $type Autocomplete type code.
*/
public function __construct(
ItemFactory $itemFactory,
AttributeConfig $attributeConfig,
ProductCollection $productCollection,
AttributeCollectionFactory $attributeCollectionFactory,
AutocompleteHelper $autocompleteHelper,
StoreManagerInterface $storeManager,
$type = self::AUTOCOMPLETE_TYPE
) {
$this->itemFactory = $itemFactory;
$this->type = $type;
$this->storeManager = $storeManager;
$this->attributeConfig = $attributeConfig;
$this->productCollection = $productCollection;
$this->attributeCollection = $attributeCollectionFactory->create();
$this->autocompleteHelper = $autocompleteHelper;

$this->loadAttributeCollection();
$this->prepareProductCollection();
}

/**
Expand All @@ -115,8 +100,8 @@ public function getItems()
$items = [];

if ($this->autocompleteHelper->isEnabled($this->getType())) {
foreach ($this->attributeCollection as $attribute) {
$filterField = $this->getFilterField($attribute);
foreach ($this->attributeConfig->getAutocompleteAttributeCollection() as $attribute) {
$filterField = $this->attributeConfig->getFilterField($attribute);
$facetData = $this->productCollection->getFacetedData($filterField);

foreach ($facetData as $currentFilter) {
Expand All @@ -137,50 +122,6 @@ public function getItems()
return $items;
}

/**
* Append facets used to select suggested attributes.
*
* @return \Smile\ElasticsuiteCatalog\Model\Autocomplete\Product\Attribute\DataProvider
*/
private function prepareProductCollection()
{
foreach ($this->attributeCollection as $attribute) {
$facetSize = $this->getResultsPageSize();
$filterField = $this->getFilterField($attribute);
$this->productCollection->addFacet($filterField, BucketInterface::TYPE_TERM, ['size' => $facetSize]);
}

return $this;
}

/**
* Load the attributes displayed in the suggest.
*
* @return \Smile\ElasticsuiteCatalog\Model\Autocomplete\Product\Attribute\DataProvider
*/
private function loadAttributeCollection()
{
$storeId = $this->storeManager->getStore()->getId();

$this->attributeCollection->addStoreLabel($storeId)
->addFieldToFilter('is_displayed_in_autocomplete', true)
->load();

return $this;
}

/**
* Get filter field for an attribute.
*
* @param Magento\Catalog\Model\ResourceModel\Product\Attribute $attribute Product attribute.
*
* @return string
*/
private function getFilterField($attribute)
{
return $this->autocompleteHelper->getAttributeAutocompleteField($attribute);
}

/**
* Retrieve number of products to display in autocomplete results
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Smile Elastic Suite to newer
* versions in the future.
*
* @category Smile
* @package Smile\ElasticsuiteCatalog
* @author Aurelien FOUCRET <aurelien.foucret@smile.fr>
* @copyright 2016 Smile
* @license Open Software License ("OSL") v. 3.0
*/

namespace Smile\ElasticsuiteCatalog\Model\Autocomplete\Product;

use Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection as AttributeCollection;
use Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory as AttributeCollectionFactory;
use Magento\Catalog\Api\Data\ProductAttributeInterface;
use Magento\Store\Model\StoreManagerInterface;
use Smile\ElasticsuiteCatalog\Helper\Autocomplete as AutocompleteHelper;

/**
* Autocomplete attribute configuration.
*
* @category Smile
* @package Smile\ElasticsuiteCatalog
* @author Aurelien FOUCRET <aurelien.foucret@smile.fr>
*/
class AttributeConfig
{
/**
* @var string[]
*/
private $defaultSelectedAttributes = ['name', 'thumbnail'];

/**
* @var string[]
*/
private $selectedAttributes;

/**
* @var AttributeCollection
*/
private $autocompleteAttributeCollection;

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @var AutocompleteHelper
*/
private $autocompleteHelper;

/**
* Constructor.
*
* @param AttributeCollectionFactory $attributeCollectionFactory Product attribute collection factory.
* @param StoreManagerInterface $storeManager Store manager.
* @param AutocompleteHelper $autocompleteHelper Autocomplete helper.
* @param string[] $additionalSelectedAttributes Additional product attribute to be added to the \
* product collection.
*/
public function __construct(
AttributeCollectionFactory $attributeCollectionFactory,
StoreManagerInterface $storeManager,
AutocompleteHelper $autocompleteHelper,
$additionalSelectedAttributes = []
) {
$this->storeManager = $storeManager;
$this->autocompleteHelper = $autocompleteHelper;

$this->selectedAttributes = array_merge($this->defaultSelectedAttributes, $additionalSelectedAttributes);

$this->autocompleteAttributeCollection = $attributeCollectionFactory->create();
$this->prepareAutocompleteAttributeCollection();
}

/**
* List of attributes selected.
*
* @return string[]
*/
public function getSelectedAttributeCodes()
{
return $this->selectedAttributes;
}

/**
* List of attribute selected by default.
*
* @return string[]
*/
public function getDefaultSelectedAttributes()
{
return $this->defaultSelectedAttributes;
}

/**
* User defined list of selected attributes.
*
* @return string[]
*/
public function getAdditionalSelectedAttributes()
{
return array_diff($this->selectedAttributes, $this->defaultSelectedAttributes);
}

/**
* List of attributes displayed in autocomplete.
*
* @return AttributeCollection
*/
public function getAutocompleteAttributeCollection()
{
return $this->autocompleteAttributeCollection;
}

/**
* Get filter field for an attribute.
*
* @param ProductAttributeInterface $attribute Product attribute.
*
* @return string
*/
public function getFilterField(ProductAttributeInterface $attribute)
{
return $this->autocompleteHelper->getAttributeAutocompleteField($attribute);
}

/**
* Init the list of attribute displayed in autocomplete.
*
* @return $this
*/
private function prepareAutocompleteAttributeCollection()
{
$storeId = $this->storeManager->getStore()->getId();

$this->autocompleteAttributeCollection->addStoreLabel($storeId)
->addFieldToFilter('is_displayed_in_autocomplete', true)
->load();

return $this;
}
}
Loading