0

I've got a module in magento 1 - it stops certain products being listed depending upon a set of attributes.

For example if the user logs in and an customer attribute set with a value of 100, they would see all products with an attribute called "test" with the value of 100 and no others...

I'm looking at porting this functionality accross to magento 2, I was hoping for some pointers on where magento 2 does the category listing...

In magento 1 I extend the mage/catalog/model/category and add an addattributetofilter... along with a few others things...

Looking breifly at magento 2, I thought that I'd be able to duplicate the behaviour by overwriting/extending CollectionFilter in the namespace 'Magento\Catalog\Model\Layer\Category' yet anything I do here results in no results regardless of what I do.

Any tips/help would be great at the minute.

1
  • Hey, did you get any solution to your problem ? Commented May 11, 2016 at 11:39

1 Answer 1

3

It is best to avoid overwriting/extending core classes because this introduces conflicts between extensions. You can add custom filter to product collection by adding observer on catalog_product_collection_load_before event.

Declare your observer, create MyModule/etc/frontend/events.xml with the following content

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="catalog_product_collection_load_before">
        <observer name="add-filter-to-product-collection-observer" instance="MyCompany\MyModule\Observer\AddFilterToProductCollectionObserver"/>
    </event>
</config>

Create observer MyModule\Observer\AddFilterToProductCollectionObserver.php

<?php
namespace MyCompany\MyModule\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer as EventObserver;

class AddStockStatusToCollectionObserver implements ObserverInterface
{
    /**
     * Add filter to product collection
     *
     * @param EventObserver $observer
     * @return void
     */
    public function execute(EventObserver $observer)
    {
        $productCollection = $observer->getEvent()->getCollection();
        $productCollection->addAttributeToFilter('my_attribute', 'value');
    }
}

Hope this will lead you in right direction.

7
  • not tested it yet. But that looks spot on.
    – encodes
    Commented Jan 19, 2016 at 9:38
  • 1
    it works fine but a problem with pagination, pagination showing wrong count and same for layer navigation. Commented Jan 28, 2016 at 11:39
  • thanks for the advice. Thats all great but there doesn't seem to be a catalog_product_collection_load_before was removed from changelog Removed some events (plugins must be used instead):
    – encodes
    Commented Mar 10, 2016 at 14:56
  • I've wrote a plugin, but am a little unsure where magento 2 actually gets the product collection for the category, any pointers for this would be apprichiated atm.
    – encodes
    Commented Mar 10, 2016 at 17:30
  • Also as user3383912 says - it always shows the wrong count.
    – encodes
    Commented Mar 22, 2016 at 16:02

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.