1

I want to call catalog_product_collection_load_after event in magento 2

How can i call that event in my custom module in magento 2?

EDIT

Filter is not working

events.xml

<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="product_filter" instance="Custom\Example\Observer\Productfilter" />
</event>

Productfilter.php

namespace Custom\Example\Observer;
use Magento\Framework\Event\ObserverInterface;
class Productfilter implements ObserverInterface
{

   public function execute(\Magento\Framework\Event\Observer $observer)
   {
       $observer = $observer->getEvent()->getCollection();
       $observer->addAttributeToFilter('name_day',array(
                            array('finset'=> array("Fri")),
                        ));
       return $this;
   }
}
6
  • Check this link, it shows how you can use events / obervers: estoredevs.com/how-to-use-event-and-observer-in-magento2 Commented Jan 28, 2016 at 6:07
  • @MuhammadFayyazUddinKhattak thanks it works
    – Newbie
    Commented Jan 28, 2016 at 7:28
  • should i post it as an answer? Commented Jan 28, 2016 at 7:57
  • @MuhammadFayyazUddinKhattak yes..
    – Newbie
    Commented Jan 28, 2016 at 8:10
  • @Newbie flat enabled? If yes check if the field name_day exists in the table. Can you also echo the query? ($collection->getSelectSql(true))
    – bchatard
    Commented Jan 28, 2016 at 8:54

1 Answer 1

3

I haven't too much time to play with it in Magento 2. So will share my quick findings, hope this will help. You can use core events to change standard behavior or you can create new in your modules. So you or other guys can do the same with your code. Also you can use plugins for the same purpose.

You can create observer which will catch this event. For this you need to add it to etc//events.xml of your module. For example:

<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_after">
    <observer name="your_observer_name" instance="<Vendor>\<Module>\Observer\<Yourobserver>"/>
</event>

And create Observer Class that will implement ObserverInterface and realize "execute" method where you can add your logic.

Example from core:

public function execute(\Magento\Framework\Event\Observer $observer)
{
    $collection = $observer->getEvent()->getCollection();
    /* @var $collection \Magento\Catalog\Model\ResourceModel\Product\Collection */
    $collection->addPriceData();

    return $this;
}

Also as you can see this event fired in the code in such way

$this->_eventManager->dispatch('catalog_product_collection_load_after', ['collection' => $this]);

So if you need to create custom event in your module you can inject eventManager, and use it to fire your event in similar way in your module (with your unique name of course).

5
  • I am trying to filter collection but its not working
    – Newbie
    Commented Jan 28, 2016 at 8:02
  • you can't apply a filter on collection on load after, it's too late. Use load before event.
    – bchatard
    Commented Jan 28, 2016 at 8:19
  • @Brice I try that event also but its also not working
    – Newbie
    Commented Jan 28, 2016 at 8:27
  • @Newbie please add your code (events.xml and Observer class)
    – bchatard
    Commented Jan 28, 2016 at 8:40
  • @Brice see my updates
    – Newbie
    Commented Jan 28, 2016 at 8: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.