-
Notifications
You must be signed in to change notification settings - Fork 1.7k
7786-Changed the code for afterSave function in adding-attributes.md file #8246
7786-Changed the code for afterSave function in adding-attributes.md file #8246
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your contribution - just some formatting queries
@@ -106,14 +106,20 @@ Likewise, the `afterSave` plugin should manipulate the entity data before return | |||
```php | |||
public function afterSave | |||
( | |||
\Magento\Catalog\Api\ProductRepositoryInterface $subject, | |||
\Magento\Catalog\Api\Data\ProductInterface $entity | |||
\Magento\Catalog\Api\ProductRepositoryInterface $subject, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indenting on this line (and next 2) should have one less space
\Magento\Catalog\Api\ProductRepositoryInterface $subject, | ||
\Magento\Catalog\Api\Data\ProductInterface $entity | ||
\Magento\Catalog\Api\ProductRepositoryInterface $subject, | ||
\Magento\Catalog\Api\Data\ProductInterface $result, /** result from the save call **/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Normally in PHP single line comments are done with // e.g. // result from the save call
Is there a reason the /** .... **/ format is used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As in the site(https://devdocs.magento.com/guides/v2.4/extension-dev-guide/extension_attributes/adding-attributes.html), every comment was done with /** .... **/ that's why I followed it.
Should I need to change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No - if it's being used elsewhere then you should stick to it for convention. It's not a must.
@BarnyShergold As in the site(https://devdocs.magento.com/guides/v2.4/extension-dev-guide/extension_attributes/adding-attributes.html), every comment was done with /** .... **/ that's why I followed it. |
@BarnyShergold Is there anything that needs to be changed? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your contribution!
running tests |
Hi @chiranjeevi-cj, thank you for your contribution! |
Purpose of this pull request
This pull request (PR)
https://devdocs.magento.com/guides/v2.4/extension-dev-guide/extension_attributes/adding-attributes.html
Directly following the general patterns described on this page - for an extension attribute on a product, with a plugin connected to the product repository - the afterSave function will not update the extension attribute. For example, making an API call with updated extension attribute will leave it unchanged.
Description
Essentially, the data in the product instance returned as the result of a save call (passed as the 2nd parameter to an 'after' plugin function) is not what was sent via the API.
After a little digging, the data that reaches the beforeSave function is correct. However, the final step in the product repository save process is a get call, presumably to refresh data, which correspondingly triggers the afterGet function to (re-)populate the extension attributes on the product returned as the result.
The code example on the page is using the ProductInterface that is the result returned from the function, due to the attribute sequence, hence why it can't / won't update the extension attribute.
However, it should also be noted that is specifically a peculiarity of the product repository and does not necessarily hold true for other repositories.
Affected DevDocs pages
Fixes #7786