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 before listener

The before listeners are used when we want to change the arguments of an original method or add some behavior before an original method is called.

Looking back at the beforeGetAddToCartUrl listener method definition, you will see that it has three properties assigned in sequence—$subject, $product, and $additional.

With the before method listener, the first property is always the $subject property, which contains the instance of the object type being observed. Properties following the $subject property match the properties of the observed getAddToCartUrl method in a sequential order.

This simple rule used for transformation is as follows:

getAddToCartUrl($product, $additional = []) beforeGetAddToCartUrl($subject, $product, ...

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