O'Reilly logo

Magento 2 Developer's Guide by Branko Ajzele

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using the after listener

The after listeners are used when we want to change the values returned by an original method or add some behavior after an original method is called.

Looking back at the afterGetAddToCartUrl listener method definition, you will see that it has only one $subject property assigned.

With the after method listener, the first and only property is always the $subject property, which contains the instance of the object type being observed and not the return value of the observed method.

This simple rule used for transformation is as follows:

getAddToCartUrl($product, $additional = [])
afterGetAddToCartUrl($subject)

The after listener methods do not need to have a return value.

Like the before interceptor method, the $subject property ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required