1

Experimenting with Magento EE 2.1.7 and creating modules. I've created an attribute by the name of "brand" and have assigned a few products with brand data so that I can filter them by brand name/value.

I've created the boilerplate M2 module code and can confirm it's registered and loading. I'm attempting to extend \Magento\Catalog\Model\Layer so here is the code and file I'm modifying:

File: app/code/MyCompany/MyModule/Model/Magento/Catalog/Layer.php

Code:

<?php

namespace MyCompany\MyModule\Model\Magento\Catalog;


class Layer extends \Magento\Catalog\Model\Layer {

    public function afterGetProductCollection($subject, $collection)
    {
        $collection->addAttributeToFilter('brand', array('eq' => 'Ryobi'));

        return $collection;
    }

}

With the above code I'm attempting to filter my custom product attribute "brand" with the value "Ryobi". Is this the correct approach for customer product attributes?

  • You want to filter product collection based on custo attribute, right? – Dhiren Vasoya Sep 1 '17 at 9:06
  • That is correct @DhirenVasoya – sparecycle Sep 1 '17 at 9:07
  • you want this every time when category page is load? Or it is based on some opetion selection? – Dhiren Vasoya Sep 1 '17 at 9:09
  • @DhirenVasoya For now, every time would be ideal. – sparecycle Sep 1 '17 at 9:09
  • As a magento 2 standard code, you need to create Plugin for above function and you can used code : $collection->addAttributeToFilter('brand', array('eq' => 'Ryobi')); – Dhiren Vasoya Sep 1 '17 at 9:12
0

You need to take not a Brand name, but it's id. Also it's called 'manufacturer'. Do something like this:

$collection->addAttributeToFilter('manufacturer', array('eq' => 1));
| improve this answer | |

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.