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电子商务平台设计的一个功能,用于导出订单数据。这个模块允许管理员方便地将Magento商店中的订单信息导出为可处理的格式,例如CSV或XML,以便进行数据分析、备份或其他外部...
### Magento 1.4.2 简便生成订单函数详解 #### 一、引言 在Magento系统中,特别是1.4.2版本中,处理订单生成的过程相对较为复杂,尤其是在sales模块与checkout模块之间存在着较为紧密的关联。本文将详细介绍如何...
在这个主题中,“Create Dynamic CMS Navigation For Magento Frontend”指的是在Magento的前端生成可以根据CMS页面自动生成的动态菜单。 在Magento中,静态菜单通常是通过后台管理界面手动创建和维护的,而动态...
"DeleteOrder"插件是针对Magento系统的一个特定解决方案,解决了在默认情况下,Magento系统不支持直接删除订单的问题。这个插件允许用户在必要时永久删除订单,但需要注意的是,一旦订单被删除,数据将无法恢复,...
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 ...
在电商领域,经常会有需求将一个已经建立并运行良好的Magento站点快速复制到另一个服务器,用于测试、备份或者创建一个新的独立站点。这个过程涉及到数据库的备份与还原、文件系统的复制以及配置的调整等多个步骤。 ...
Magento-Order-Status-Image 订单页面上更好的订单状态 此模块使用更好/更简洁的方式在客户订单页面视图上显示订单状态。截屏安装打开文件path/to/magento/app/design/frontend/default/default/template/sales/...
15. **SALESTRANSACTION**, **POLL**, **SALESORDER**:销售交易、投票和销售订单管理,涉及到具体的交易处理和订单状态追踪。 16. **EAVENTITY**, **CUSTOMERENTITY**, **CUSTOMERADDRESSENTITY**:实体基表、客户...
### Magento深入理解——强大配置系统解析 #### 一、引言 Magento是一款极其灵活且功能丰富的电子商务平台,其核心竞争力之一在于其强大的配置系统。这一系统不仅为开发者提供了极高的定制化能力,还确保了平台的...
这会创建 Magento 需要的所有表,包括 `catalog_product_entity`(产品信息)、`sales_flat_order`(订单数据)、`customer_entity`(客户信息)等。 4. **加载样本数据**:除了基础架构,这个包可能还包含一些示例...
Magento是一款强大的开源电子商务平台,以其高度可定制性和灵活性著称。在进行Magento的二次开发时,你需要理解并掌握以下几个核心概念和技术: 1. **MVC架构**:Magento基于Model-View-Controller(MVC)设计模式...
Magento是开源的电子商务平台,广泛用于在线商店的建设。SMTP(Simple Mail Transfer Protocol)是用于发送电子邮件的标准协议。在Magento中,SMTP插件扮演着关键角色,它允许商家通过更安全、可靠的SMTP服务器发送...
Magento是一款强大的开源电子商务平台,以其高度可定制性和灵活性著称。作为一款基于PHP开发的系统,它为商家提供了丰富的功能,包括商品管理、订单处理、客户管理、营销工具等。以下将详细介绍`magento入门学习资料...
这个“magento-java-master.zip_magento”压缩包可能是为了提供一个Java连接Magento源码的示例或者库,帮助开发者实现Java与Magento系统的交互。 在Java中与Magento进行交互通常涉及到以下几个关键知识点: 1. **...
Magento是一款强大的开源电子商务平台,它的灵活性和可扩展性使得开发者能够根据需求定制各种功能。在电商网站中,图片是至关重要的元素,它们可以展示产品细节,吸引顾客注意力。然而,大量的图片也会对网站性能...
根据给定文件信息,以下为《Magento 2 Developer's Cookbook》一书中的知识点介绍。 首先,《Magento 2 Developer's Cookbook》是一本针对Magento 2开发的指导手册,它向开发者提供了实用的食谱来解决在Magento 2...
Magento是一款强大的开源电子商务平台,为开发者提供了广泛的定制和扩展能力。《Magento插件开发手册》是一份详尽的指南,旨在帮助开发者理解Magento的核心架构、编码标准以及如何创建和部署自定义插件。 ### ...
Magento是一款开源的电子商务平台,专为在线商家设计,提供强大的购物车系统和丰富的功能。"Magik Shoes Magento 模板"是专为Magento 1.7版本设计的商店主题,旨在提升在线鞋类销售商店的用户体验和视觉吸引力。在这...
return $this->resultPageFactory->create()->setBlock($block)->renderView(); } } ``` 3. **View**: 视图负责展示内容。在`View`目录下创建`frontend`子目录,然后创建`templates`子目录。在这里,创建一个名为...
Magento是一款流行的开源电子商务平台,其功能强大且模块化,它支持在线零售业务的创建和管理。Magento使用PHP语言编写,它为开发者提供了丰富的扩展性和灵活性,使得定制网站功能和外观成为可能。本指南是为后台...