4

We want to get a Product Collection based on a filter with the category ID.

In our case, we have the desired Category with ID 43.

Our Call looks like this:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$productcollection = $productCollection->create()
                    ->addAttributeToSelect('*')
                    ->load();

I tried it with ->addFieldToFilter('category', '43'), but it didn't worked for me. I also tried it with the following: ->addCategoriesFilter(['eq' => 43]), but that also don't work. With the last statement my Query looks like this:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$productcollection = $productCollection->create()
                    ->addAttributeToSelect('*')
                    ->addCategoriesFilter(['eq' => 43])
                    ->load();

What am I doing wrong here?

6

You should never use the ObjectManager.

You can put this in the Block, and call the function getProductCollection() in the phtml :

namespace Company\Module\Block;

class CustomBlock extends \Magento\Framework\View\Element\Template 
{
    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()
    {
        $categoryId = '43';
        $category = $this->_categoryFactory->create()->load($categoryId);
        $collection = $this->_productCollectionFactory->create();
        $collection->addAttributeToSelect('*');
        $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;
    }
}
  • Do you advise here to modify a core file? If not, please advise how "to put this in the block". Thank you. – Max Jul 8 '16 at 10:59
  • 1
    no you should never edit a core file. This guide mageinferno.com/blog/how-create-template-block-magento-2 will help you to create a custom block for the phtml – Manashvi Birla Jul 8 '16 at 13:28
  • Thanks for that link. Which class should be extended when using your code? I understand that I need to write an extra module for that, hence the question. I can only inject a custom function into the block when I need which block is needed to be extended here. I hope you can follow my question. – Max Jul 8 '16 at 14:51
  • The custom block should extend the class \Magento\Framework\View\Element\Template – Manashvi Birla Jul 13 '16 at 5:00
  • Check the edited answer – Manashvi Birla Jul 13 '16 at 5:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.