I'm trying to make the status of a review be approved
instead of pending
when the user posts it in the frontend in Magento 2.
And I took this approach. Create a before plugin, available only in the frontend area, for the method setStatusId
for the Magento\Review\Model\Review
that looks like this
public function beforeSetStatusId(\Magento\Review\Model\Review $review, $status)
{
return [\Magento\Review\Model\Review::STATUS_APPROVED];
}
It seamed to me like a good idea. And it should work since I'm returning the approved status. The actual method should then pick this up as a parameter.
but to my surprise it didn't work.
Then I dug up and found that the method setStatusId
does not exist in the review model. It is magically called and it actually runs setData('status_id', $status)
.
I took a look then in the generated interceptor, and indeed there is not setStatusId
method.
How can I pluginize the magic getters/setters in magento 2? Is that even possible?
Note: I don't need a solution for making the reviews auto-approved. I know I can take other approaches, like the save_before
events. This is not important for now.
Best Answer
Yes.
In the same way as a other public method. You need to declare plugin in
di.xml
configuration and add you custom code in plugin.But pluginization of DTO classes is not good idea. Try to customize appropriate controllers/services for modify application behaviour and do not add plugins on DTO object. This customization will destroy application layers. I understand, that you are use most quick and easy way, but in some case it is wrong strategy.