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.