I have custom product attributes color_type, color, and size. I want to get product SKU by using values of these attributes.

Any help would be appreciated.

1 Answer 1


Please try with the below code. You need to just filter product collection with the custom attribute option values.

For Example color Attribute - Red option has value 999 in DB.

A] For single color value Filter.

$collection->addAttributeToFilter('color', array('eq' =>  $colorValue));

B] For Multiple color values Filter.(EX : 998,999 comma-separated by explode function)

$collection->addAttributeToFilter('color', array('in' => explode(',', $colorValues)));

C] Full code with example.

    protected $_productCollectionFactory;

    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,

    $this->_productCollectionFactory = $productCollectionFactory;

    $collection = $this->_productCollectionFactory->create();
    $collection->addAttributeToFilter('color',  array('eq' => $colorValue));//Filter collection by color attribute option value
//echo $collection->getSelect(); // Print query for verification

foreach ($collection as $product) {
        //echo '<pre>', print_r($product->getdata()); //For full product details.
    echo $product->getSku(); //print product sku.    
  • Thanks. I will try this and let you know.
    – mechanic
    Dec 31, 2021 at 10:52
  • It is working. Thanks.
    – mechanic
    Dec 31, 2021 at 13:46

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.