Magento以编程方式部分退款

时间:2013-01-10 17:51:44

标签: magento partial memo

如何创建部分创建备忘录?即如果总订单价值为50英镑,我想创建一个10英镑的贷项通知单。我使用以下代码创建贷记凭证:

function createCreditMemo($orderId) {
$order = Mage::getModel('sales/order')->load($orderId, 'increment_id');
if (!$order->getId()) {
    print_r('order_not_exists');
}
if (!$order->canCreditmemo()) {
    Mage::log('cannot_create_creditmemo', null, 'product.txt');
    print_r('cannot_create_creditmemo');
}
$data = array();
$refundToStoreCreditAmount = '10.22';

$service = Mage::getModel('sales/service_order', $order);

$creditmemo = $service->prepareCreditmemo($data);

// refund to Store Credit
if ($refundToStoreCreditAmount) {
    // check if refund to Store Credit is available
    if ($order->getCustomerIsGuest()) {
        print_r('cannot_refund_to_storecredit');
    }
    $refundToStoreCreditAmount = max(
        0,
        min($creditmemo->getBaseCustomerBalanceReturnMax(), $refundToStoreCreditAmount)
    );


    if ($refundToStoreCreditAmount) {
        $refundToStoreCreditAmount = $creditmemo->getStore()->roundPrice($refundToStoreCreditAmount);

        $creditmemo->setBaseCustomerBalanceTotalRefunded($refundToStoreCreditAmount);
        $refundToStoreCreditAmount = $creditmemo->getStore()->roundPrice(
            $refundToStoreCreditAmount*$order->getStoreToOrderRate()
        );
        // this field can be used by customer balance observer
        $creditmemo->setBsCustomerBalTotalRefunded($refundToStoreCreditAmount);
        // setting flag to make actual refund to customer balance after credit memo save
        $creditmemo->setCustomerBalanceRefundFlag(true);
        print_r($refundToStoreCreditAmount.'<br/>');
        //die('2');
    }


}
//Mage::log($creditmemo, null, 'product.txt');
$creditmemo->setPaymentRefundDisallowed(true);

$creditmemo->register();

$orderCreditMemoStatusCode = Mage_Sales_Model_Order::STATE_CLOSED;
$orderCreditMemoStatusComment = 'Order Refunded.';
$saveTransaction = Mage::getModel('core/resource_transaction')->addObject ($creditmemo )->addObject ( $order )->save ();
$order->addStatusToHistory ( $orderCreditMemoStatusCode, $orderCreditMemoStatusComment, true );

$notifyCustomer = true;
$comment = 'testing refund';
$includeComment = true;

$creditmemo->setEmailSent(true);

// add comment to creditmemo
if (!empty($comment)) {
    $creditmemo->addComment($comment, $notifyCustomer);
}

try {
    Mage::getModel('core/resource_transaction')
        ->addObject($creditmemo)
        ->addObject($order)
        ->save();
    // send email notification

    $creditmemo->sendEmail($notifyCustomer, ($includeComment ? $comment : ''));
} catch (Mage_Core_Exception $e) {
    print_r('data_invalid', $e->getMessage());
}

echo $creditmemo->getIncrementId();

}

在下面的代码中,即使我设置了$refundToStoreCreditAmount = 10;,它仍然会退还全部金额。知道如何部分退款吗?

0 个答案:

没有答案