2

I need a code to create order in Magento programmatically.

I used this script for simple product order and it works good.

require_once 'app/Mage.php'; 
Mage::app(); 
$quote = Mage::getModel('sales/quote')
        ->setStoreId(Mage::app()->getStore('default')->getId());
        $customer = Mage::getModel('customer/customer')
                ->setWebsiteId(1)
                ->loadByEmail('[email protected]');
        $quote->assignCustomer($customer); 
$product = Mage::getModel('catalog/product')->load(2770);
$buyInfo = array(
        'qty' => 1, 
);
$quote->addProduct($product, 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('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();
printf("Created order %s\n", $order->getIncrementId());

But i also want to create order of grouped product as well as bundle product, I try with using grouped and bundle product id but it does not work

2 Answers 2

3

Don't use Mage_Sales_Model_Quote::addProduct(), especially for complex products. Use Mage_Checkout_Model_Cart::addProduct() which is also used by the add to cart controller action and takes care for configuring the product and quote item. It takes a product id and the "request info" as parameters, where the request info can be simply the qty to buy (for simple products) or data for a buyRequest object. This is basically the $_POST data from an add to cart action.

So have a look at what form data is sent to Magento when adding a grouped or bundled product to the cart, then you can simulate this request with:

Mage::getSingleton('checkout/cart')->addProduct($productId, $request);

You can read more about the buyRequest in this reference.

1

There is sence that every group and bundle product has a simple product and magento add all those simple product individual not as full product sku of group or bundle, so we can use every product's sku in order for example if we have group product with id 2828 & 2827 so we can order that grouped product like below

<?php
require_once 'app/Mage.php'; 
Mage::app();  
ini_set('display_errors', 1);

$skus = array('2828'=>1,'2827'=>3); // 2828&282 are idproducts and 1&3 are Qty
$email = '[email protected]';

$quote = Mage::getModel('sales/quote')->setStoreId(Mage::app()->getStore('default')->getId());
$customer = Mage::getModel('customer/customer')->setWebsiteId(1)->loadByEmail($email);
$quote->assignCustomer($customer);
foreach($skus as $key=>$val)
{
    $quote[] = $quote->addProduct(Mage::getModel('catalog/product')->load($key), new Varien_Object(array('qty' => $val,)));
}
$addressData = array(
    'firstname' => 'Test',
    'lastname' => 'Test',
    'street' => 'test Street 10',
    'city' => 'test',
    'postcode' => '123456',
    'telephone' => '123456',
    'country_id' => 'US',
    'region_id' => 12,
    );
$billingAddress = $quote->getBillingAddress()->addData($addressData);
$shippingAddress = $quote->getShippingAddress()->addData($addressData);

$shippingAddress->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(); 
echo  $order->getIncrementId();
1
  • This works for grouped product which really just add the simple products to the cart. But bundle products are added as a parent item with child items. The parent can have its own price and sku (fixed or dynamic), there can be shopping cart price rules for it, and so on. Mar 25, 2016 at 11:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.