Maybe you want to create an invoice from some custom script or through cron script. Here is one very useful example of code.
Next step, when we have loaded model “sales/ored” with data, the next procedure is:
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:
hope that this is clear
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

本文提供了一个实用的Magento代码示例,展示如何从自定义脚本或通过cron脚本创建发票。详细步骤包括加载订单模型、检查是否可以创建发票、准备发票并设置在线捕获选项。
1035

被折叠的 条评论
为什么被折叠?



