Creating order including custom options programmatically

0
123
views
Advertisement

Using the following code, you can create an order including custom options programmatically.

<?php

$quote = Mage::getModel(‘sales/quote’)->setStoreId(Mage::app()->getStore(‘default’)->getId());
if($customer->getId()){
$quote->assignCustomer($customer);
}else{
$quote->setCustomerEmail(‘customeremail@domain.com’);
}
$product = Mage::getModel(‘catalog/product’)->load($productId);
$buyInfo = array(
                    ‘qty’ => 1,
                     ‘options’ => array(‘optionId’ => ‘optionValue’)
                );
$quote->addProduct($product, new Varien_Object($buyInfo));
        
$addressData = array(
                ‘firstname’ => ‘firstname’,
                ‘lastname’ => ‘lastname’,
                ‘street’ => ‘street’,
                ‘city’ => ‘city’,
                ‘postcode’ => ‘postcode’,
                ‘telephone’ => ‘telephone’,
                ‘country_id’ => ‘country_id’, // US
                ‘region_id’ => ‘region_id’,
        );
        
$billingAddress = $quote->getBillingAddress()->addData($addressData);
$shippingAddress = $quote->getShippingAddress()->addData($addressData);
$shippingAddress->setCollectShippingRates(true)->collectShippingRates()
                        ->setShippingMethod(‘flatrate_flatrate’)
                        ->setPaymentMethod(‘bankpayment’);
        
$quote->getPayment()->importData(array(‘method’ => ‘bankpayment’));
$quote->collectTotals()->save();
        
$service = Mage::getModel(‘sales/service_quote’, $quote);
$service->submitAll();

$order = $service->getOrder();

?>

Thanks for reading my blog. I hope it will help you, Please leave your comments here if it’s really help you. 🙂
Thank You 🙂