2

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?

1

1 Answer 1

3

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;
}
6
  • it's not working Feb 24, 2017 at 11:16
  • Did you try to clear Magento Cache and try again. It will work in case: saving Creditmemo. I'm using Magento 2.13 version. Feb 24, 2017 at 11:18
  • I'm pretty sure it will work. I checked and tested. Feb 24, 2017 at 11:39
  • Ok let me try it again Feb 26, 2017 at 16:57
  • Yes It's Working Thanx @KhoaTruongDinh Jan 3, 2020 at 11:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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