php magento 2按根类别添加过滤器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php magento 2按根类别添加过滤器相关的知识,希望对你有一定的参考价值。

<?php
// ...

    public function getProductCollection()
    {
        $collection = $this->_productCollectionFactory->create();
        $collection->addAttributeToSelect('*');

        // If is in other store then default add filter by root category
        if ($this->_storeManager->getStore()->getCode() !== 'default') {
            $rootCategory = $this->_storeManager->getStore()->getRootCategoryId();
            $category = $this->_categoryRepository->get($rootCategory);
            $collection->addCategoriesFilter(['in' => $category->getAllChildren(true, $rootCategory)]);
        }

        $collection->addAttributeToFilter('type_id', 'configurable');
        $collection->addAttributeToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH);
        $collection->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);

        $collection->setPageSize(4);
        return $collection;
    }

以上是关于php magento 2按根类别添加过滤器的主要内容,如果未能解决你的问题,请参考以下文章