Follow by Email

Thursday, October 15, 2015

Creating Magento order programmatically

If you’re working with Magento , most probably you’ll face a situation when you need to create orders programmatically, It’s not relevant to create orders (or customers) using the Magento interface, as you can do it programmatically, which takes less time and effort.
This action can be of great help if you need to create a number of orders quickly to test your store features.
Now, in this article I’ll explain now to create Magento orders programmatically, also adding information on creating customers programmatically, as these two actions are closely connected.

 $order_list = array('32'=>array('12'=>3));
//here 32 is my customer id, 12 is my product id, 3 is my QTY for that product.


foreach($order_list as $retailer_id=>$retailer_order){
                                                 $customer = Mage::getModel('customer/customer')->load($retailer_id);
                                                 $storeId = $customer->getStoreId();
                                                 $quote = Mage::getModel('sales/quote')->setStoreId($storeId);
                                                 $quote->assignCustomer($customer);
                                                 foreach($retailer_order as $item_id=>$item_qty){
                                                                 $product = Mage::getModel('catalog/product')->load($item_id);
                                                                 $params = array();
                                                                 $params['qty'] = $item_qty;
                                                                 $request = new Varien_Object();
                                                                 $request->setData($params);
                                                                 $quote->addProduct($product, $request);
                                                 }
                                                 $customerAddressId = $customer->getDefaultBilling();
                                                 $address = Mage::getModel('customer/address')->load($customerAddressId);
                                                 $addressData = array (
                                                                                'prefix' => $address->getPrefix(),
                                                                                'firstname' => $address->getFirstname(),
                                                                                'lastname' => $address->getLastname(),
                                                                                'street' => $address->getStreet(),
                                                                                'city' => $address->getCity(),
                                                                                'region_id' => $address->getRegionId(),
                                                                                'region' => $address->getRegion(),
                                                                                'postcode' => $address->getPostcode(),
                                                                                'country_id' => $address->getCountryId(),
                                                                                'telephone' => $address->getTelephone(),
                                                                                'email' => $address->getEmail()
                                                );
                                                $billingAddress = $quote->getBillingAddress()->addData($addressData);
                                                $shippingAddress = $quote->getShippingAddress()->addData($addressData);
                                               
                                                $shippingAddress->setFreeShipping( true )
                                                                ->setCollectShippingRates(true)->collectShippingRates()
                                                                ->setShippingMethod('freeshipping_freeshipping')
                                                                ->setPaymentMethod('checkmo');


                                                $quote->getPayment()->importData(array('method' => 'checkmo'));
                                                $quote->collectTotals()->save();
                                                $service = Mage::getModel('sales/service_quote', $quote);
                                                $service->submitAll();
                                                $order = $service->getOrder();
                                                                                                 
                                }

1 comment:

  1. Thank you for sharing valuable source code. Nice post. I enjoyed reading this post.
    eCommerce solution providers

    ReplyDelete