Magento 2:对产品集合应用可见性和状态过滤器


11

我想在产品集合上应用可见性和状态过滤器。像在Magento 1中一样,我们这样做:

Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);

Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);

我在Magento 2中获得了产品集合,但是找不到适用于上述过滤器的功能。

Answers:


16

要获取具有状态启用和可见性的产品集合:

您必须将以下代码保存在阻止文件中。

protected $productCollectionFactory;
protected $productVisibility;
protected $productStatus;

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,        
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
    \Magento\Catalog\Model\Product\Attribute\Source\Status $productStatus,
    \Magento\Catalog\Model\Product\Visibility $productVisibility,
    array $data = []
)
{
    $this->productCollectionFactory = $productCollectionFactory;
    $this->productStatus = $productStatus;
    $this->productVisibility = $productVisibility;
    parent::__construct($context, $data);
}

public function getProductCollection()
{
    $collection = $this->productCollectionFactory->create();
    $collection->addAttributeToFilter('status', ['in' => $this->productStatus->getVisibleStatusIds()]);
    $collection->setVisibility($this->productVisibility->getVisibleInSiteIds());
    return $collection;
}

上面的代码可用于以下可见性类型:

  • VISIBILITY_IN_SEARCH
  • VISIBILITY_IN_CATALOG
  • VISIBILITY_BOTH

我已经以类似的方式实现了@Rakesh,我需要根据所应用的过滤器来获得产品的结果,但是产品不能按照过滤器进行渲染...需要帮助... magento.stackexchange.com/questions / 136959 /…
Sushivam

@Rakesh Jesadiya有什么办法可以在产品列表页面中显示“不可见”
amith lal

如何添加过滤器来SearchCriteriaBuilder使用时ProductRepositoryInterface?(见magento.stackexchange.com/a/84347/60128。
亚尼斯Elmeris

3

这应该为您工作:

    $collection = $this->collectionFactory->create();

    $collection->addAttributeToFilter('status', ['in' => $this->productStatus->getVisibleStatusIds()]);
    $collection->setVisibility($this->productVisibility->getVisibleInSiteIds());

使用的类是:

/**
 * @var \Magento\Catalog\Model\Resource\Product\CollectionFactory
 */
protected $collectionFactory;

/**
 * @var \Magento\Catalog\Model\Product\Attribute\Source\Status
 */
protected $productStatus;

/**
 * @var \Magento\Catalog\Model\Product\Visibility
 */
protected $productVisibility;

复查这些类以检查其他方法以获得可能的状态或可见性值。

但是,据我所知,Magento 2建议回顾存储库的概念,建议使用存储库访问产品数据而不是使用集合。例如:

$searchCriteria = $this->searchCriteriaBuilder->create();

$products = $this->productRepository->getList($searchCriteria)->getItems();

嗨,可以检查我的问题是否与此相似,并且我必须使用searchCriteria吗?我也尝试过,但一切都失败了。magento.stackexchange.com/questions/117112/…–
ntzz

1

具有状态启用和可见性的产品收集:

您可以尝试以下代码

protected $productCollectionFactory;
protected $productVisibility;
protected $productStatus;

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,        
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
    \Magento\Catalog\Model\Product\Attribute\Source\Status $productStatus,
    \Magento\Catalog\Model\Product\Visibility $productVisibility,
    array $data = []
)
{
    $this->productCollectionFactory = $productCollectionFactory;
    $this->productStatus = $productStatus;
    $this->productVisibility = $productVisibility;
    parent::__construct($context, $data);
}

public function getProductCollection()
{
    $collection = $this->productCollectionFactory->create();
    $collection->addAttributeToFilter('status', ['in' => $this->productStatus->getVisibleStatusIds()]);
    $collection->setVisibility($this->productVisibility->getVisibleInSiteIds());
    return $collection;
}

该代码适用于可见性类型,例如:

VISIBILITY_IN_SEARCH
VISIBILITY_IN_CATALOG
VISIBILITY_BOTH
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.