使用自定义productcollection magento2创建分层导航

时间:2017-06-16 15:33:49

标签: php dependency-injection magento2 layered-navigation

我有一个块类,它有:

 public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Catalog\Helper\Category $categoryHelper,
    \Magento\Catalog\Model\Indexer\Category\Flat\State $categoryFlatState,
    \Magento\Catalog\Model\CategoryFactory $categoryFactory,
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,   
    ....
) {

    $this->_categoryHelper              = $categoryHelper;
    $this->_categoryFactory             = $categoryFactory;
    $this->_collectionFactory           = $collectionFactory;
    $this->categoryFlatConfig           = $categoryFlatState;
    $this->_productCollectionFactory    = $productCollectionFactory;  
    parent::__construct($context);
    ....
}

和......的功能

public function getProductCollection($childId)
{
    $categoryId = $childId;
    $category = $this->_categoryFactory->create()->load($categoryId);
    $collection = $this->_productCollectionFactory->create();
    $collection->addAttributeToSelect('name');
    $collection->addAttributeToSelect('url');
    $collection->addAttributeToSelect('image');
    $collection->addAttributeToSelect('price');
    $collection->addAttributeToSelect('special_price');
    $collection->addCategoryFilter($category);
    $collection->addAttributeToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH);
    $collection->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
    return $collection;
}

如何使用此生产集合创建分层导航,该导航使用可用于返回产品的属性,然后可以使用这些属性进行过滤。

由于网站的性质,我必须在类别页面上循环这个X次,因为我在设计时以某种方式获得了此页面上每个子类别的产品。

所以在我的E.G模板中我有:

`$products= $this->getProductCollection($idhere);
foreach ($getmeprods as $products):?>
... looping through data
<?php endforeach;?>

` 任何帮助都会受到赞赏,但我在这里感到很难受!

0 个答案:

没有答案