遇到一种情况,需要在下单后再由管理员添加订单运费,然后顾客再付款。那么问题来了,如何给订单添加运费呢?下面是一段代码,可以实现该功能:
$orderId = 'your order id';
$order = Mage::getModel('sales/order')->load($orderId); $baseCurrencyCode = Mage::app()->getStore()->getBaseCurrencyCode(); $currentCurrencyCode = $order->getOrderCurrencyCode(); $shippingAmount = Mage::helper('directory')->currencyConvert($params['shipping']['amount'], $currentCurrencyCode, $baseCurrencyCode); $notify = isset($params['shipping']['is_customer_notified']) ? true : false;
$order->setBaseGrandTotal($order->getBaseGrandTotal() + $shippingAmount);
$order->settBaseSubtotal($order->getBaseSubtotal() + $shippingAmount);
//$order->setBaseSubtotalInvoiced($order->getBaseSubtotalInvoiced() + $shippingAmount);
//$order->setBaseTotalInvoiced($order->getBaseTotalInvoiced() + $shippingAmount);
//$order->setBaseTotalPaid($order->getBaseTotalPaid() + $shippingAmount);
$order->setGrandTotal($order->getGrandTotal() + $shippingAmount);
$order->setSubtotal($order->getSubtotal());
//$order->setSubtotalInvoiced($order->getSubtotalInvoiced() + $shippingAmount);
//$order->setTotalInvoiced($order->getTotalInvoiced() + $shippingAmount);
//$order->setTotalPaid($order->getTotalPaid() + $shippingAmount);
$order->setBaseSubtotalInclTax($order->getBaseSubtotalInclTax() + $shippingAmount);
$order->setSubtotalInclTax($order->getSubtotalInclTax() + $shippingAmount);
$order->setTotalDue($order->getTotalDue() - $order->getShippingAmount() + $shippingAmount);
$order->setShippingAmount($shippingAmount); $order->setBaseShippingAmount($shippingAmount);
Mage::getModel('wholesale/core')->sendOrderEmail($order, 'wholesale_savers_order_update', $attch = '');
$order->setCarriageConfirmed(1);
$order->save();
上述的代码中,关于Invoice的都是注释掉的,下面分两个情况说一下:
当订单还是pending的时候,如果上面的Invoice代码没有注释掉,那么当你在后台点击生成Invoice的时候,生成的invoice将不会包含你添加的运费的金额。例如订单的金额是1000,后台添加了20块运费,那么在生成invoice的时候,invoice的金额就是1000,不会是1020。只有在添加运费时不设置SubtotalInvoiced等才会是1020.
权当备忘。