您的位置:首页 > 其它

How to create Magento invoice from order

2013-12-25 11:39 260 查看
Maybe you want to create an invoice from some custom script or through cron script. Here is one very useful example of code.

First of all, we have to load some order over model “sales/order”, this is very easy.

$order = Mage::getModel("sales/order")->load($order_id)


Next step, when we have loaded model “sales/ored” with data, the next procedure is:

try {
if(!$order->canInvoice())
{
Mage::throwException(Mage::helper('core')->__('Cannot create an invoice.'));
}
$invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice();
if (!$invoice->getTotalQty()) {
Mage::throwException(Mage::helper('core')->__('Cannot create an invoice without products.'));
}
$invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_ONLINE);
$invoice->register();
$transactionSave = Mage::getModel('core/resource_transaction')
->addObject($invoice)
->addObject($invoice->getOrder());
$transactionSave->save();
}
catch (Mage_Core_Exception $e) {
}


You will notice that we have set option for capture online. This option depends on payment method. Some payment methods support capture online and some don’t. If you want to set capture offline, you can do that with next line code:

$invoice->setRequestedCaptureCase(
Mage_Sales_Model_Order_Invoice::CAPTURE_OFFLINE
);


hope that this is clear
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: