Magento 1.9 – Create Order Programmatically

checkoutmagento-1.9payment-methodsquoteshipping-methods

I am using the below code to create orders, But getting exception as "Please specify a shipping method." Also the payment method, The code says invalid. Please let me know where i can check these shipping and payment methods. Kindly explain in detail. Beginner in magento

<?php
include 'connect.php';
$db=mysqli_select_db($conn,'onceaga9_aw');
$mageFilename = 'app/Mage.php';
require_once $mageFilename;
Mage::setIsDeveloperMode(true);
umask(0);
Mage::app();

extract($_POST);
$productids = array(983);
$websiteId = Mage::app()->getWebsite()->getId();
$store = Mage::app()->getStore();
// Start New Sales Order Quote
$quote = Mage::getModel('sales/quote')->setStoreId($store->getId());

// Set Sales Order Quote Currency
$quote->setCurrency($order->AdjustmentAmount->currencyID);
$customer = Mage::getModel('customer/customer')
    ->setWebsiteId($websiteId)
    ->loadByEmail($email);
if ($customer->getId() == "") {
$customer = Mage::getModel('customer/customer');
$customer->setWebsiteId($websiteId)
        ->setStore($store)
        ->setFirstname('Jhon')
        ->setLastname('Deo')
        ->setEmail($email)
        ->setPassword("password");
$customer->save();
}

// Assign Customer To Sales Order Quote
$quote->assignCustomer($customer);

// Configure Notification
$quote->setSendCconfirmation(1);
foreach ($productids as $id)
{
$product = Mage::getModel('catalog/product')->load($id);
$quote->addProduct($product, new Varien_Object(array('qty' => 1)));
}

// Set Sales Order Billing Address
$billingAddress = $quote->getBillingAddress()->addData(array(
'customer_address_id' => '',
'prefix' => '',
'firstname' => 'john',
'middlename' => '',
'lastname' => 'Deo',
'suffix' => '',
'company' => '',
'street' => array(
    '0' => 'Noida',
    '1' => 'Sector 64'
),
'city' => 'Noida',
'country_id' => 'IN',
'region' => 'UP',
'postcode' => '201301',
'telephone' => '78676789',
'fax' => 'gghlhu',
'vat_id' => '',
'save_in_address_book' => 1
    ));

// Set Sales Order Shipping Address
$shippingAddress = $quote->getShippingAddress()->addData(array(
'customer_address_id' => '',
'prefix' => '',
'firstname' => 'john',
'middlename' => '',
'lastname' => 'Deo',
'suffix' => '',
'company' => '',
'street' => array(
    '0' => 'Noida',
    '1' => 'Sector 64'
),
'city' => 'Noida',
'country_id' => 'IN',
'region' => 'UP',
'postcode' => '201301',
'telephone' => '78676789',
'fax' => 'gghlhu',
'vat_id' => '',
'save_in_address_book' => 1
    ));
if ($shipprice == 0) {
$shipmethod = 'freeshipping_freeshipping';
}

// Collect Rates and Set Shipping & Payment Method
$shippingAddress->setCollectShippingRates(true)
    ->collectShippingRates()
    ->setShippingMethod('flatrate_flatrate')
    ->setPaymentMethod('checkmo');

// Set Sales Order Payment
//$quote->getPayment()->importData(array('method' => 'checkmo'));
$quote->setShippingMethod('flatrate_flatrate');

// Collect Totals & Save Quote
$quote->collectTotals()->save();

try {
// Create Order From Quote
$service = Mage::getModel('sales/service_quote', $quote);
$service->submitAll();
$increment_id = $service->getOrder()->getRealOrderId();
}
catch (Exception $ex) {
echo $ex->getMessage();
}
catch (Mage_Core_Exception $e) {
echo $e->getMessage();
}



// Resource Clean-Up
$quote = $customer = $service = null;

// Finished
return $increment_id;
?>

Best Answer

Change below code :

// Collect Rates and Set Shipping & Payment Method
$shippingAddress->setCollectShippingRates(true)
    ->collectShippingRates()
    ->setShippingMethod('flatrate_flatrate')
    ->setPaymentMethod('checkmo');

// Set Sales Order Payment
//$quote->getPayment()->importData(array('method' => 'checkmo'));
$quote->setShippingMethod('flatrate_flatrate');

to:

// Collect Rates and Set Shipping & Payment Method
$shippingAddress->setCollectShippingRates(true)
    ->collectShippingRates()
    ->setShippingMethod('flatrate_flatrate') 
    ->setPaymentMethod('checkmo');

// Set Sales Order Payment
$quote->getPayment()->importData(array('method' => 'checkmo'));
//$quote->setShippingMethod('flatrate_flatrate');

In above code you using shipping method flat rate, Make sure flat rate is enable.

setShippingMethod('flatrate_flatrate') 

same for payment method using below code you use check money order, make sure it is enable

->setPaymentMethod('checkmo');