I wanted to create orders in magento using a script. So i searched and found couple of scripts. And i thought of putting them together here. They had to be modified a little bit. For example on Magento 1.5 i got the below script working
<?php /* require_once 'app/Mage.php'; Mage::app(); $quote = Mage::getModel('sales/quote') ->setStoreId(Mage::app()->getStore('default')->getId()); if ('existing') { // for customer orders: $customer = Mage::getModel('customer/customer') ->setWebsiteId(1) ->loadByEmail('customer@email.com'); $quote->assignCustomer($customer); } else { // for guest orders only: $quote->setCustomerEmail('customer@email.com'); } // add product(s) $product = Mage::getModel('catalog/product')->load(4); $buyInfo = array( 'qty' => 1, // custom option id => value id // or // configurable attribute id => value id ); $quote->addProduct($product, new Varien_Object($buyInfo)); $quote->addProduct($product2, new Varien_Object($buyInfo)); $addressData = array( 'firstname' => 'Test', 'lastname' => 'Test', 'street' => 'Sample Street 10', 'city' => 'Somewhere', 'postcode' => '123456', 'telephone' => '123456', 'country_id' => 'US', 'region_id' => 12, // id from directory_country_region table ); $billingAddress = $quote->getBillingAddress()->addData($addressData); $shippingAddress = $quote->getShippingAddress()->addData($addressData); $shippingAddress->setCollectShippingRates(true)->collectShippingRates() ->setShippingMethod('flatrate_flatrate') ->setPaymentMethod('checkmo'); $quote->getPayment()->importData(array('method' => 'checkmo')); $quote->collectTotals()->save(); $service = Mage::getModel('sales/service_quote', $quote); $service->submitAll(); $order = $service->getOrder(); printf("Created order %s\n", $order->getIncrementId());
This code did not work on Magento enterprise. I found the below code work instead.
require_once 'app/Mage.php'; Mage::app(); $id=1; // get Customer Id $customer = Mage::getModel('customer/customer')->load($id); $transaction = Mage::getModel('core/resource_transaction'); $storeId = $customer->getStoreId(); $reservedOrderId = Mage::getSingleton('eav/config')->getEntityType('order')->fetchNewIncrementId($storeId); $order = Mage::getModel('sales/order') ->setIncrementId($reservedOrderId) ->setStoreId($storeId) ->setQuoteId(0) ->setGlobal_currency_code('USD') ->setBase_currency_code('USD') ->setStore_currency_code('USD') ->setOrder_currency_code('USD'); // set Customer data $order->setCustomer_email($customer->getEmail()) ->setCustomerFirstname($customer->getFirstname()) ->setCustomerLastname($customer->getLastname()) ->setCustomerGroupId($customer->getGroupId()) ->setCustomer_is_guest(0) ->setCustomer($customer); // set Billing Address $billing = $customer->getDefaultBillingAddress(); $billingAddress = Mage::getModel('sales/order_address') ->setStoreId($storeId) ->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_BILLING) ->setCustomerId($customer->getId()) ->setCustomerAddressId($customer->getDefaultBilling()) ->setCustomer_address_id($billing->getEntityId()) ->setPrefix($billing->getPrefix()) ->setFirstname($billing->getFirstname()) ->setMiddlename($billing->getMiddlename()) ->setLastname($billing->getLastname()) ->setSuffix($billing->getSuffix()) ->setCompany($billing->getCompany()) ->setStreet($billing->getStreet()) ->setCity($billing->getCity()) ->setCountry_id($billing->getCountryId()) ->setRegion($billing->getRegion()) ->setRegion_id($billing->getRegionId()) ->setPostcode($billing->getPostcode()) ->setTelephone($billing->getTelephone()) ->setFax($billing->getFax()); $order->setBillingAddress($billingAddress); $shipping = $customer->getDefaultShippingAddress(); $shippingAddress = Mage::getModel('sales/order_address') ->setStoreId($storeId) ->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_SHIPPING) ->setCustomerId($customer->getId()) ->setCustomerAddressId($customer->getDefaultShipping()) ->setCustomer_address_id($shipping->getEntityId()) ->setPrefix($shipping->getPrefix()) ->setFirstname($shipping->getFirstname()) ->setMiddlename($shipping->getMiddlename()) ->setLastname($shipping->getLastname()) ->setSuffix($shipping->getSuffix()) ->setCompany($shipping->getCompany()) ->setStreet($shipping->getStreet()) ->setCity($shipping->getCity()) ->setCountry_id($shipping->getCountryId()) ->setRegion($shipping->getRegion()) ->setRegion_id($shipping->getRegionId()) ->setPostcode($shipping->getPostcode()) ->setTelephone($shipping->getTelephone()) ->setFax($shipping->getFax()); $order->setShippingAddress($shippingAddress) ->setShipping_method('flatrate_flatrate') ->setShippingDescription('flatrate'); $orderPayment = Mage::getModel('sales/order_payment') ->setStoreId($storeId) ->setCustomerPaymentId(0) ->setMethod('purchaseorder') ->setPo_number(' - '); $order->setPayment($orderPayment); // let say, we have 2 products $subTotal = 0; $products = array('1' => array('qty' => 1),'2' =>array('qty' => 1)); foreach ($products as $productId=>$product) { $_product = Mage::getModel('catalog/product')->load($productId); $rowTotal = $_product->getPrice() * $product['qty']; $orderItem = Mage::getModel('sales/order_item') ->setStoreId($storeId) ->setQuoteItemId(0) ->setQuoteParentItemId(NULL) ->setProductId($productId) ->setProductType($_product->getTypeId()) ->setQtyBackordered(NULL) ->setTotalQtyOrdered($product['rqty']) ->setQtyOrdered($product['qty']) ->setName($_product->getName()) ->setSku($_product->getSku()) ->setPrice($_product->getPrice()) ->setBasePrice($_product->getPrice()) ->setOriginalPrice($_product->getPrice()) ->setRowTotal($rowTotal) ->setBaseRowTotal($rowTotal); $subTotal += $rowTotal; $order->addItem($orderItem); } $order->setSubtotal($subTotal) ->setBaseSubtotal($subTotal) ->setGrandTotal($subTotal) ->setBaseGrandTotal($subTotal); $transaction->addObject($order); $transaction->addCommitCallback(array($order, 'place')); $transaction->addCommitCallback(array($order, 'save')); $transaction->save();
相关推荐
Magento Order Export
Magento的sales模块算是模块里面关联度比较大的,checkout模块的下订单流程也写得让人一头雾水,附件里主要是用客户对象和产品sku+数量的数组生成订单的函数,内容不多,但是可是找了不少时间,希望能帮到正在攻克...
Magento 2 Beginners Guide by Gabriel Guarino English | 14 Mar. 2017 | ASIN: B01MS81BQX | 442 Pages | AZW3 | 31.84 MB Key Features Set up and manage your very first online store with a friendly and ...
NULL 博文链接:https://justcoding.iteye.com/blog/1725217
Magento-Order-Status-Image 订单页面上更好的订单状态 此模块使用更好/更简洁的方式在客户订单页面视图上显示订单状态。截屏安装打开文件path/to/magento/app/design/frontend/default/default/template/sales/...
magento数据结构分析 magento数据字典
一般magento是不可以删除订单的,有了这个就可以删除了,不过删除完就无法再恢复,此插件在1.7.0.2上可以使用
magento的SMTP插件magento的SMTP插件magento的SMTP插件
Chapter 1, Installing Magento 2 on Apache and NGINX, is a...new methods introduced in the Magento 2 framework and examples on how to create basic functions. Chapter 8, Creating Magento 2 Extensions – Ad
magik shoes magento 模板, magento 1.7 模板
深入理解Magento 由精东博客下载下来制作成pdf
Magento插件开发手册 Magento Extension Developers Guide
Athlete Magento主题,兼容Magento 1.9.x, 1.8.x, 1.7.x ,特点:完全自适应(响应式布局),自定义子主题,无限颜色,完全自定义,一键安装演示,小工具化横幅系统,颜色选择,MEGA菜单,多商店支持,自定义产品...
magento2 developers cookbook, magento 开发手册,magento教程
主要包括的内容有:magento的常用设置、Magento模板制作教程、Magento商城_目录结构说明、Magento中文教程实用文档、常用的xml的文件的作用以及自定义模块的应用
Magento Magento商城 Magento目录结构
快速在服务器上复制为另一个站,版本用的是magento,php语言
[Packt Publishing] Magento 扩展开发入门教程 (英文版) [Packt Publishing] Getting Started with Magento Extension Development (E-Book) ☆ 图书概要:☆ Understand Magento extensions, and build your own...
如果你正在创建一个Magento的管理模块(学习创造Magento管理模块),那么你一定会被要求创建一个管理列表既Grid。创建列表,Magento新手程序员有点困难,但在本教程中,我们将创建一个网格在管理模块中最简单的方式...
Magento 的配置系统就像是 Magento 的心脏,支撑着 Magento 的运行。这套 配置系统掌管着几乎所有“module/model/class /template/etc”。它把整个 Magento 系统抽象出来,用一个配置文件来描述。这里的“配置文件”...