Magento代码之订单创建流程
2015-08-01 10:49
309 查看
Magento代码之订单创建流程
直接看代码吧。下面的代码是如何通过程序创建一个完美订单。
<?php
require_once 'app/Mage.php';
Mage::app('default');//初始化程序,设置当前店铺
$store = Mage::app()->getStore('default');
//通过电子邮件获取用户,当然也可以不获取,创建guest订单
$customer = Mage::getModel('customer/customer');
$customer->setStore($store);
$customer->loadByEmail('email_address@gmail.com');
//初始化Quote,Magento的订单是通过Quote来转化过去的
$quote = Mage::getModel('sales/quote');
$quote->setStore($store);
$quote->assignCustomer($customer);//如果有用户则执行这个
$product1 = Mage::getModel('catalog/product')->load(166); /* HTC Touch Diamond */
$buyInfo1 = array('qty' => 1);
$product2 = Mage::getModel('catalog/product')->load(18); /* Sony Ericsson W810i */
$buyInfo2 = array('qty' => 3);
//添加商品到Quote
$quote->addProduct($product1, new Varien_Object($buyInfo1));
$quote->addProduct($product2, new Varien_Object($buyInfo2));
//设置账单和收货品地址
$billingAddress = $quote->getBillingAddress()->addData($customer->getPrimaryBillingAddress());
$shippingAddress = $quote->getShippingAddress()->addData($customer->getPrimaryShippingAddress());
//设置配送和支付方式
$shippingAddress->setCollectShippingRates(true)->collectShippingRates()
->setShippingMethod('flatrate_flatrate')
->setPaymentMethod('checkmo');
$quote->getPayment()->importData(array('method' => 'checkmo'));
//Quote计算运费
$quote->collectTotals()->save();
//将Quote转化为订单
$service = Mage::getModel('sales/service_quote', $quote);
$service->submitAll();
$order = $service->getOrder();
/***至此订单已经成功生成,下面是注册付款信息***/
$invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice();
$invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_ONLINE);
$invoice->register();
$transaction = Mage::getModel('core/resource_transaction')
->addObject($invoice)
->addObject($invoice->getOrder());
$transaction->save();
?>
直接看代码吧。下面的代码是如何通过程序创建一个完美订单。
<?php
require_once 'app/Mage.php';
Mage::app('default');//初始化程序,设置当前店铺
$store = Mage::app()->getStore('default');
//通过电子邮件获取用户,当然也可以不获取,创建guest订单
$customer = Mage::getModel('customer/customer');
$customer->setStore($store);
$customer->loadByEmail('email_address@gmail.com');
//初始化Quote,Magento的订单是通过Quote来转化过去的
$quote = Mage::getModel('sales/quote');
$quote->setStore($store);
$quote->assignCustomer($customer);//如果有用户则执行这个
$product1 = Mage::getModel('catalog/product')->load(166); /* HTC Touch Diamond */
$buyInfo1 = array('qty' => 1);
$product2 = Mage::getModel('catalog/product')->load(18); /* Sony Ericsson W810i */
$buyInfo2 = array('qty' => 3);
//添加商品到Quote
$quote->addProduct($product1, new Varien_Object($buyInfo1));
$quote->addProduct($product2, new Varien_Object($buyInfo2));
//设置账单和收货品地址
$billingAddress = $quote->getBillingAddress()->addData($customer->getPrimaryBillingAddress());
$shippingAddress = $quote->getShippingAddress()->addData($customer->getPrimaryShippingAddress());
//设置配送和支付方式
$shippingAddress->setCollectShippingRates(true)->collectShippingRates()
->setShippingMethod('flatrate_flatrate')
->setPaymentMethod('checkmo');
$quote->getPayment()->importData(array('method' => 'checkmo'));
//Quote计算运费
$quote->collectTotals()->save();
//将Quote转化为订单
$service = Mage::getModel('sales/service_quote', $quote);
$service->submitAll();
$order = $service->getOrder();
/***至此订单已经成功生成,下面是注册付款信息***/
$invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice();
$invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_ONLINE);
$invoice->register();
$transaction = Mage::getModel('core/resource_transaction')
->addObject($invoice)
->addObject($invoice->getOrder());
$transaction->save();
?>
相关文章推荐
- MapRed编程自定义Partitioner
- php-高级(多维数组,日期,include)
- C++语句简洁化
- LeapFTP3.0.1注册码
- 浅析Java中的访问权限控制
- 详解Python3中的Sequence type的使用
- C语言计算某天是一年中的第几天(熟练使用状态位flag)
- [置顶] Qt中使用html语言对word进行图表操作
- Struts2 Hello World 实例
- Spring+Struts2+mybatis三大框架的整合配置
- Github免费账户使用”私有库“
- 新的公司,新的技术
- java学习第二天笔记
- PHP---注册界面的各种验证函数
- Eclipse常用快捷键大全
- Java集合源码解读(一):集合总体框架
- C# socket 局域网聊天程序的一点心得
- 【排序算法】用C++实现各种排序算法
- struts2学习笔记(五)值栈与ognl
- weiphp的学习