1

I am using Magento 2.3 and I want to get list of category's dropdown attributes and their values?

For example I have a Furniture category and in this category I have Color and Manufacturer attributes in sidebar navigation, I want to get these two attributes(Color and manufacturer) and their values based on Furniture category. You can check my requirement with the help of below image:- enter image description here Please help me how to get this.

Thanks in advance.

1

You can try this.

Let's say that $category is the instance of the current category;

$collection = $category->getProductCollection()->addAttributeToSelect(['manufacturer', 'color']);
$manufacturers = [];
$colors = [];
foreach ($collection as $product) {
    $manufacturer = $product->getData('manufacturer');
    if ($manufacturer && !isset($manufacturers[$manufacturer])) {
         $manufacturers[$manufacturer] = $product->getAttributeText('manufacturer');
    }

    $color = $product->getData('color');
    if ($color && !isset($colors[$color])) {
         $colors[$color] = $product->getAttributeText('color');
    }
}

after this, $manufacutrers and $colors should contain all the values you need. The key is the id, and the value is the option label.
But this may cause performance issues if you have a large product collection.

1
  • Thanks @marius to quick answer, but I also want related attributes dynamically, so when category selected, associated attributed listed in next dropdown and their value in next dropdown. Oct 1 '20 at 8:44

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.