I have a set of SKUs which I want to display in the search result and the filters should be displayed according to my set of SKUs.
For this I have override the Magento\Catalog\Model\Layer\Search
class in etc/di.xml
<preference for="Magento\Catalog\Model\Layer\Search"
type="Mirror\Catalogexport\Model\Layer\Search"/>
In the file
Mirror\Catalogexport\Model\Layer\Search.php
I have override getProductCollection()
and prepareProductCollection($collection)
functions.
Here is the code:
<?php
namespace Mirror\Catalogexport\Model\Layer;
use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory as AttributeCollectionFactory;
class Search extends \Magento\Catalog\Model\Layer\Search
{
/**
* Catalog product visibility
*
* @var \Magento\Catalog\Model\Product\Visibility
*/
protected $productVisibility;
/**
* Catalog config
*
* @var \Magento\Catalog\Model\Config
*/
protected $catalogConfig;
/**
* @var \Magento\Search\Model\QueryFactory
*/
protected $queryFactory;
/**
* @param \Magento\Catalog\Model\Layer\ContextInterface $context
* @param \Magento\Catalog\Model\Layer\StateFactory $layerStateFactory
* @param AttributeCollectionFactory $attributeCollectionFactory
* @param \Magento\Catalog\Model\ResourceModel\Product $catalogProduct
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Framework\Registry $registry
* @param CategoryRepositoryInterface $categoryRepository
* @param \Magento\Search\Model\QueryFactory $queryFactory
* @param \Magento\Catalog\Model\Product\Visibility $productVisibility
* @param \Magento\Catalog\Model\Config $catalogConfig
* @param array $data
*/
public function __construct(
\Magento\Catalog\Model\Layer\ContextInterface $context,
\Magento\Catalog\Model\Layer\StateFactory $layerStateFactory,
AttributeCollectionFactory $attributeCollectionFactory,
\Magento\Catalog\Model\ResourceModel\Product $catalogProduct,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\Registry $registry,
CategoryRepositoryInterface $categoryRepository,
\Magento\Search\Model\QueryFactory $queryFactory,
\Magento\Catalog\Model\Product\Visibility $productVisibility,
\Magento\Catalog\Model\Config $catalogConfig,
array $data = []
) {
$this->queryFactory = $queryFactory;
$this->productVisibility = $productVisibility;
$this->catalogConfig = $catalogConfig;
parent::__construct(
$context,
$layerStateFactory,
$attributeCollectionFactory,
$catalogProduct,
$storeManager,
$registry,
$categoryRepository,
$data);
}
/**
* Retrieve current layer product collection
*
* @return \Magento\Catalog\Model\ResourceModel\Product\Collection
*/
public function getProductCollection()
{
if (isset($this->_productCollections[$this->getCurrentCategory()->getId()])) {
$collection = $this->_productCollections[$this->getCurrentCategory()->getId()];
} else {
$collection = $this->collectionProvider->getCollection($this->getCurrentCategory());
$this->prepareProductCollection($collection);
$this->_productCollections[$this->getCurrentCategory()->getId()] = $collection;
}
return $collection;
//return parent::getProductCollection();
}
/**
* Initialize product collection
*
* @param \Magento\Catalog\Model\ResourceModel\Product\Collection $collection
* @return \Magento\Catalog\Model\Layer
*/
public function prepareProductCollection($collection)
{
$category = $this->getCurrentCategory();`
//List of SKUs which I want to show in search result`
$productSkus = ["MH01","24-WB02","24-MB01","STITCHED-TEE-01","SP7475024-XL"];
$productImplode = implode("','", $productSkus);
$prodAttr = $this->catalogConfig->getProductAttributes();
$logger->info(json_encode($prodAttr));
$collection
->addAttributeToSelect($this->catalogConfig->getProductAttributes());
$collection->getSelect()
->where('sku IN (?)', $productSkus)
->order("FIELD(sku,'" . $productImplode . "')");
$collection
->setStore($this->_storeManager->getStore())
->addMinimalPrice()
->addFinalPrice()
->addTaxPercents()
->addStoreFilter()
->addUrlRewrite()
->setVisibility($this->productVisibility->getVisibleInSearchIds());
//$collection->addSearchFilter($this->queryFactory->get()->getQueryText());
//$collection->addCategoryFilter($category);
return $this;
}
}
It is not returning any product. If I remove comment then the $collection->addCategoryFilter($category);
then it is displaying my set of products but filters and sorting is not working properly.
If I remove comment from
$collection->addSearchFilter($this->queryFactory->get()->getQueryText());
then magento default search result is displayed.
Is there any better way to display our custom search result in magento 2 instead of magento default search?