How to create creditmemo for order item programmatically in magento 2?

We can create credit memo for the specific item based on below way in Magento 2. We need to pass some basic data to generate credit memo in Magento 2.

By above way, We can create credit memo in magento 2.

We have pass do_offline = 1 means we have generated credit memo offline. If you want to refund shipping amount pass shipping amount value to shipping_amount field.

You can set adjustment_positive and adjustment_negative value if you want.

if you want to notify customer, set comment_text field in above function.

Pass orderItemId of order item and set qty to refund.


Order item id is 10 and qty to refund is 1, you can pass multiple item using foreach loop in above syntax.


4 Replies to “How to create creditmemo for order item programmatically in magento 2?”

  1. You can definitely do using CreditmemoFactory but i have taken reference from core Credit memo where original credit memo generates using Magento_Sales module. By above way you can do it single/multiple item credit memo easily.
    You can more details from core Module, Magento\Sales\Controller\Adminhtml\Order\Creditmemo\Save.php file

  2. Hi Rakesh Jesadiya,
    I would like to know, if i set do_offline = 0, this return the credit memo amount to customer account / credit card/debit card or to something he/she used to pay online while ordered?

    I need this kind of custom script to refund online.

Leave a Reply

Your email address will not be published. Required fields are marked *