2

如何创建部分创建备忘录?即,如果总订单价值为 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;它仍然会全额退款。知道如何部分退款吗?

4

0 回答 0