2

What is the $subject in a plugin?

My code is:

 Class Product 
{
public function afterGetPrice(\Magento\Catalog\Model\Product $subject, 
$result) 
{ 

    return $result*2; 
}
} 
4

The 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.

| improve this answer | |
1

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.