Domanda

I want to filter the products with custom attribute and display the result on homepage, i need to do it pragmatically? Is that possible ? If yes please explain

È stato utile?

Soluzione

Please follow script:

protected $_productCollectionFactory;
protected $_categoryFactory;    

public function __construct(
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
\Magento\Catalog\Model\CategoryFactory $categoryFactory,
\Magento\Framework\View\Element\Template\Context $context,
) {
    $this->_categoryFactory = $categoryFactory;
    $this->_productCollectionFactory = $productCollectionFactory;
    parent::__construct($context);
}

public function getProductCollection()
{        
    $collection = $this->_productCollectionFactory->create();
    $collection->addAttributeToSelect('*');        
    $collection->addAttributeToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH);
    $collection->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
    return $collection;
}  }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a magento.stackexchange
scroll top