1

I am trying to get product collection with filter by attribute Color(code) with value Black(5). I have one configurable product with color Black(5), Red(6). Here Product Name Product 1(configurable product), Product 1 Black (simple product), Product 1 Red (simple product).

So I get the resulting Product 1 Black (simple product) but I want to result from Product 1(configurable product).

So please let me know the code for this. My code is

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();               
$categoryProducts = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');

$categoryProducts->addAttributeToSelect('*')->addAttributeToFilter('color', array('eq' => '5'));
foreach ($categoryProducts as $product) {
    echo "<br>";
    echo $i.'==='.$product->getId().'==='.$product->getName();
    echo "<br>";
    //$i++;
}

Thanks in Advance!

2 Answers 2

2

Try to use this below code :

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();               
$categoryProducts = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');

$categoryProducts->addAttributeToSelect('*')->addAttributeToFilter('color', array('eq' => '5'));
$categoryProducts->addAttributeToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH);
$categoryProducts->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);

foreach ($categoryProducts as $product) {
    echo "<br>";
    echo $i.'==='.$product->getId().'==='.$product->getName();
    echo "<br>";
    //$i++;
}

UPDATED :

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $categoryProducts = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');

        $categoryProducts->addAttributeToSelect('*')->addAttributeToFilter('color', array('eq' => 5476));
        $categoryProducts->addAttributeToFilter('status', \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
        $categoryProducts->getSelect()->joinLeft(array('link_table' => 'catalog_product_super_link'), 'link_table.product_id = e.entity_id',
            array('product_id', 'parent_id')
        );

        $categoryProducts->getSelect()->group('link_table.parent_id');

        foreach ($categoryProducts as $product) {
            echo "<br>";
            echo $product->getParentId() . "====" . $product->getName(); // Parent Id and name
            echo "<br>";
        }

        foreach ($categoryProducts as $product) {
            echo "<br>";
            echo $product->getId() . '===' . $product->getName();
            echo "<br>";
        }

Clean cache and check it.

7
  • So no product comes. please check it. Commented May 5, 2020 at 6:33
  • check my updated answer. Commented May 5, 2020 at 7:01
  • still, child product comes not parents Commented May 5, 2020 at 7:13
  • $product->getParentId() => It will return parent id. Please check it. I tested and it's working fine Commented May 5, 2020 at 7:14
  • okay got it, so do you mean I need to use that. Commented May 5, 2020 at 7:16
0

Use Below code

<?php

namespace NameSpace\FilterProducts\Block;

use Magento\Catalog\Block\Product\Context;

use Magento\Catalog\Model\Product\Visibility;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
use Magento\Catalog\Block\Product\AbstractProduct;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Catalog\Model\CategoryFactory;
use Magento\Catalog\Model\Category;


/**
 * For FreshDeals
 */
class FilterProducts extends AbstractProduct
{


     * @var CollectionFactory
     */
    protected $productCollectionFactory;

    /**
     * @var Visibility
     */
    protected $catalogProductVisibility;

    /**
     * @var Data
     */
    protected $helper;

    /**
     * @var CategoryCollectionFactory
     */
    protected $categoryCollection;

    /**
     * @var StoreManagerInterface
     */
    protected $storeManager;

    /**
     * @var CategoryFactory
     */
    protected $categoryFactory;

    /**
     * @var Category
     */
    protected $category;



    public function __construct(
        Context $context,
        CollectionFactory $productCollectionFactory,
        Visibility $catalogProductVisibility,
        Data $helper,
        CategoryCollectionFactory $categoryCollection,
        StoreManagerInterface $storeManager,
        CategoryFactory $categoryFactory,
        Category $category,
        array $data = []
    ) {
        $this->catalogProductVisibility = $catalogProductVisibility;
        $this->productCollectionFactory = $productCollectionFactory;
        $this->helper = $helper;
        $this->categoryCollection = $categoryCollection;
        $this->storeManager = $storeManager;
        $this->categoryFactory = $categoryFactory;
        $this->category = $category;


        parent::__construct($context, $data);
    }

    /**
     * get collection of FreshDeals Products
     * @return mixed
     */
    public function getProductCollection($categoryparams = null)
    { 

        $productCount = $this->helper->getProductCount();
        $storeId = $this->_storeManager->getStore()->getId();

        $collection = $this->productCollectionFactory->create();
        $collection->setVisibility($this->catalogProductVisibility->getVisibleInCatalogIds());

        if ($categoryparams) {
            $collection->addCategoryFilter($categoryparams);
        } else {
            $category = $this->getCurrentCategory();
            if (!empty($category)) {
                $collection->addCategoryFilter($category);
            }
        }

        $collection->addMinimalPrice()
            ->addFinalPrice()
            ->addTaxPercents()
            ->addAttributeToSelect('*')
            ->addStoreFilter($storeId)->setPageSize($productCount)
            ->addAttributeToFilter('color', 'value of your attribute')

            ->setOrder(self::SORT_ORDER_FIELD, self::SORT_ORDER_VALUE);

        return $collection;
    }


}//end class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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