Question

I need to extend creditmemo save action:

  • Update post data before action method call
  • Update database after creditmemo save.

Which method is better Plugin or preference and if Plugin then how can I update post object?

Was it helpful?

Solution

You should try with Plugin. The code below will work:

etc/adminhtml/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

    <type name="Magento\Sales\Api\CreditmemoRepositoryInterface">
        <plugin name="add_data_custom"
                type="Vendor\Module\Model\Plugin\CreditmemoAddData"/>
    </type>

</config>

In your Plugin:

public function beforeSave(
     \Magento\Sales\Api\CreditmemoRepositoryInterface $subject,
     \Magento\Sales\Api\Data\CreditmemoInterface $entity
)
{
  //Your code here
}

public function afterSave(
    \Magento\Sales\Api\CreditmemoRepositoryInterface $subject,
    $result
) {

    //Your code here

    return $result;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top