0

Basically i want to get product collection form brand_id.is there anyway to get collection.

  • Is brand_id product attribute ? – Arunprabakaran M Sep 7 at 6:07
  • basically when we add product we select band and add product in it – usman Sep 7 at 8:26
1

You can filter the product collection by attribute code and their selected value in Magento 2.

I'm writing a block class BrandProducts method to achieve your requirement.

<?php
namespace Vendor\Module\Block;
class BrandProducts extends \Magento\Framework\View\Element\Template
{    
    protected $_productCollectionFactory;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,        
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,        
        array $data = []
    )
    {    
        $this->_productCollectionFactory = $productCollectionFactory;    
        parent::__construct($context, $data);
    }

    public function getProductCollection()
    {
        $attributeSelectionId = 28; //for example 28 is attribute value of your attribute selection
        $collection = $this->_productCollectionFactory->create();
        $collection->addAttributeToSelect('*');
        $collection->addAttributeToFilter('your_attribute_code', $attributeSelectionId);
        $collection->setPageSize(3); // fetching only 3 products
        return $collection;
    }
}

Hope it helps you to find your solution.

  • Have you tried it @usman? – Sumit Sep 27 at 4:19
  • Sorry for the late reply .I have not tried it because i find other solution. – usman Oct 3 at 12:48
  • OK, perfect.... – Sumit Oct 3 at 12:53

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.