Saya sedang mengerjakan mengambil navigasi berlapis di magento2 untuk koleksi produk khusus. Saya sudah mendapatkan koleksi khusus di halaman kustom perlu menunjukkan navigasi berlapis. Mencoba mengadaptasi solusi magento1 ini tetapi tidak bisa jauh.
Ada ide bagaimana saya bisa mencapainya di magento2. Apa yang telah saya lakukan sejauh ini adalah sebagai berikut:
Memperpanjang blok Katalog Daftar Produk untuk daftar produk khusus pada halaman kustom saya.
class View extends \Magento\Catalog\Block\Product\ListProduct
{
public function __construct(
\Magento\Catalog\Block\Product\Context $context,
\Magento\Framework\Data\Helper\PostHelper $postDataHelper,
\Magento\Catalog\Model\Layer\Resolver $layerResolver,
CategoryRepositoryInterface $categoryRepository,
\Magento\Framework\Url\Helper\Data $urlHelper,
array $data = [],
\Custom\LayerNavigation\Model\Layer $testlayerobj
) {
parent::__construct($context,$postDataHelper,$layerResolver,
$categoryRepository,$urlHelper,$data);
$this->_coreRegistry = $context->getRegistry();
$this->_testlayer = $testlayerobj;
}
protected function _getProductCollection()
{
if ($this->_productCollection === null) {
$this->_productCollection = $this->getLayer()->getProductCollection();
}
return $this->_productCollection;
}
public function getLayer()
{
$layer = $this->_coreRegistry->registry('current_layer');
if ($layer) {
return $layer;
}
return $this->_testlayer;
}
}
Digunakan blok Pencarian inti untuk navigasi berlapis dalam file tata letak
<referenceContainer name="sidebar.main">
<block class="Magento\LayeredNavigation\Block\Navigation\Search" name="catalogsearch.leftnav" before="-" template="layer/view.phtml">
<block class="Magento\LayeredNavigation\Block\Navigation\State" name="catalogsearch.navigation.state" as="state" />
<block class="Magento\LayeredNavigation\Block\Navigation\FilterRenderer" name="catalogsearch.navigation.renderer" as="renderer" template="layer/filter.phtml"/>
</block>
</referenceContainer>
Model lapisan Core Diperpanjang untuk memodifikasi koleksi.
class Layer extends \Magento\Catalog\Model\Layer
{
public function __construct(
optionStoreFactory $optionstoreFactory,
Attrhelper $attrhelper,
productCollectionFactory $productCollectionFactory,
AttributevalueFactory $attributevalueFactory,
CategoryRepositoryInterface $categoryRepository,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\App\Request\Http $request,
\Magento\Framework\Registry $registry,
\Magento\Catalog\Model\Layer\Search\CollectionFilter $filter,
array $data = []
) {
$this->optionstoreFactory = $optionstoreFactory;
$this->attributevalueFactory = $attributevalueFactory;
$this->_attrhelper = $attrhelper;
$this->request = $request;
$this->productCollectionFactory = $productCollectionFactory;
$this->categoryRepository = $categoryRepository;
$this->_storeManager = $storeManager;
$this->filter = $filter;
$this->registry = $registry;
}
public function getProductCollection()
{
$attributevalue = $this->getAttributeValue(); //eg: Manufacturer Attribute details
$attr_code = $attributevalue->getAttributeCode();
$attr_option_value = $attributevalue->getOptionId();
$collection = $this->productCollectionFactory->create();
$store_id = $this->request->getParam('store_id');
$collection->addAttributeToSelect('*')
->addAttributeToFilter($attr_code , ['finset'=>$attr_option_value ])
->addAttributeToFilter('visibility','4')
->setStore($store_id)
->addFieldToFilter('status', array('eq' => 1))
->setOrder('id', 'DESC');
$this->prepareProductCollection($collection);
return $collection;
}
public function prepareProductCollection($collection)
{
$this->filter->filter($collection, $this->getCurrentCategory());
return $this;
}
public function getCurrentCategory()
{
$category = $this->registry->registry('current_category');
if ($category) {
$this->setData('current_category', $category);
} else {
$category = $this->categoryRepository->get($this->getCurrentStore()->getRootCategoryId());
$this->setData('current_category', $category);
}
return $category;
}
public function getCurrentStore()
{
return $this->_storeManager->getStore();
}
}
Sampai sekarang saya mendapatkan navigasi layer yang ditampilkan tetapi tidak spesifik untuk koleksi khusus saya. Sesuai debug saya, koleksi disaring sepanjang jalan dari kategori root (Yang berisi katalog seluruh produk) dan menurut itu mengambil lapisan.