1

我有一个块类,它有:

 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 次,因为由于设计原因,我以某种方式在此页面上获取每个子类别的产品。

所以在我的 EG 模板中,我有:

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

` 任何帮助将不胜感激,但我在这里相当困惑!

4

0 回答 0