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?

2

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;
}
  • it's not working – Vaibhav Ahalpara Feb 24 '17 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. – Khoa TruongDinh Feb 24 '17 at 11:18
  • I'm pretty sure it will work. I checked and tested. – Khoa TruongDinh Feb 24 '17 at 11:39
  • Ok let me try it again – Vaibhav Ahalpara Feb 26 '17 at 16:57
  • Yes It's Working Thanx @KhoaTruongDinh – Praveen Chelumalla Jan 3 at 11:04

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.