1

This is my code:

    $OldOrder = Mage::getModel('sales/order')->loadByIncrementId(3123121312);
    $invoices = $OldOrder->getInvoiceCollection();
    $service = Mage::getModel('sales/service_order', $OldOrder);
    foreach ($invoices as $invoice){
        $data = [];
        $data['shipping_amount'] = 0;
        $data['grand_total'] = 1;
        $data['base_grand_total'] = 1;
        $data['adjustment_positive'] = 1;
        $data['qtys'] = array(0 => 0);
        $creditmemo = $service->prepareInvoiceCreditmemo($invoice, $data);
        $creditmemo->setShippingAmount(0);
        $creditmemo->setGrandTotal(1);
        $creditmemo->setRefundRequested(true);
        $creditmemo->setOfflineRequested(false);
        $creditmemo->setPaymentRefundDisallowed(false);
        try {
            $creditmemo->refund();
        }catch(Exception $e){
            var_dump($e->getMessage());
        }
    }

I got no error, nothing, but the credit memo is not created . Any ideas why ?

0

Chester you need to add one line

$creditMemo->load()

Before save or refund. Your code should be like this

$OldOrder = Mage::getModel('sales/order')->loadByIncrementId(3123121312);
    $invoices = $OldOrder->getInvoiceCollection();
    $service = Mage::getModel('sales/service_order', $OldOrder);
    foreach ($invoices as $invoice){
        $data = [];
        $data['shipping_amount'] = 0;
        $data['grand_total'] = 1;
        $data['base_grand_total'] =1;
        $data['adjustment_positive'] = 1;
        $data['qtys'] = array(0 => 0);
        $creditmemo = $service->prepareInvoiceCreditmemo($invoice, $data);
        $creditmemo->setShippingAmount(0);
        $creditmemo->setGrandTotal(1);
        $creditmemo->setRefundRequested(true);
        $creditmemo->setOfflineRequested(false);
        $creditmemo->setPaymentRefundDisallowed(false);
        $creditMemo->load();
        try {
            $creditmemo->refund();
        }catch(Exception $e){
            var_dump($e->getMessage());
        }
    }

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.