How to create Magento invoice from order

来源:互联网 发布:ubuntu安装mac os 编辑:程序博客网 时间:2024/05/21 15:50
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 :-)

0 0
原创粉丝点击