我正在为自己的产品集合在magento2中获取分层导航。我已经在自定义页面上获取了自定义集合,需要显示分层导航。试图适应这种 magento1解决方案,但步伐遥遥无期。
任何想法我怎么能在magento2中实现它。我到目前为止所做的如下:
在我的自定义页面上扩展了Catalog ListProduct块,以获取自定义产品列表。
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;
}
}
将核心搜索块用于布局文件中的分层导航
<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>
扩展核心层模型可修改集合。
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();
}
}
到目前为止,我正在显示图层导航,但它并不特定于我的自定义集合。根据我的调试,集合从根类别(其中包含整个产品目录)一直被过滤,并据此获取层。