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, $additional = [])
The before
listener methods do not need to have a return value.
If we run get_class($subject)
in the beforeGetAddToCartUrl
listener method that we previously saw, we will have the following result:
\Magento\Catalog\Block\Product\ListProduct...