What is the $subject
in a plugin?
My code is:
Class Product {
public function afterGetPrice(\Magento\Catalog\Model\Product $subject, $result) {
return $result*2;
}
}
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityThe parameter $subject
is the original object or class you are writing a plugin for.
See: https://alanstorm.com/magento_2_object_manager_plugin_system/
An after plugin method has two parameters. The first ($subject above), is the object the method was called on (in our case, that’s Pulsestorm\TutorialPlugin\Model\Example\Interceptor object). The second parameter ($result above) is the result of the original method call.
Also explained here:
https://devdocs.magento.com/guides/v2.2/extension-dev-guide/plugins.html
The first argument for the before, after, and around methods is an object that provides access to all public methods of the observed method’s class.
Plugin concepts introduced to do some function which was declared in public methods in core functions.
getPrice() -> function available extended methods "before, after and around".
For example:
For "x" product want to add extra price while loading product page.
In our custom module will declare,
BeforeGetPrice - this method will execute before execution core Catalog Model getPrice function.
AfterGetPrice - method will execute after execution core Catalog Model getPrice function.
AroundGetPrice - execute before and after execution core Catalog Model getPrice function.
I was given simple explanation, these function have limitation please go through Magento devdocs plugin concepts.
afterGetPrice($subject, $procees)
Parameters
$subject -> denotes the public function which going to be replace or extend the parent or original Class object.
$proceed -> core function final result or return output.