Jul 16, 2013

Magento partial refund creditmemo programatically

Suppose you have a product which is out of stock or something and you want to refund that product line item from order programatically. The below code will create creditmemo/refund for the products with certain SKU if it finds it in the line items of the order.

Mage::app('admin'); //You can create creditmemo in admin area only
//You should have $orderID as order increment ID and $sku as product SKU you want to refund for.
$order = Mage::getModel('sales/order')->loadByIncrementId($orderID);
$orderItem = $order->getItemsCollection()->getItemByColumnValue('sku', $sku);
$service = Mage::getModel('sales/service_order', $order);
$data = array(
    'qtys' => array(
        $orderItem->getId() => 1 //qty to refund.. $orderItem->getQty()
$creditMemo = $service->prepareCreditmemo($data)->save();


  • apart from the coding help, your child is too cute.

  • it’s work but not proper code, it show that product is refunded but still show in ship product, and if i refund all items then still i can ship the order, in proper way it is not possible.
    and also not show refund offline in comment.

    i also try some other code but it still not work properly. can you please suggest anything for do it because i try googling but no any developer can do this…

Leave a comment


Welcome to my Blog

Kalpesh MehtaHelping Magento developers in their day-to-day development problems since 2011. Most of the problems and solutions here are my own experiences while working on different projects. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!




Magento top 50 contributors

Magento top 50 contributors