0

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;
?>

2 Answers 2

0

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');
5
  • hi, Thanks for the response. how to verify if flat rate or money order is enabled? Commented May 16, 2016 at 7:18
  • Go to system->configuration->sales->shipping method->flat rate shipping same for payment system->configuration->sales->payment method->check money order. Commented May 16, 2016 at 7:22
  • Thanks., one more query how to find out what method names to be used. Like checkmo for check money order. I have cash on delivery enabled and freeshipping enabled, so what names shld i use? Commented May 16, 2016 at 7:31
  • For freeshipping code is "freeshipping_freeshipping" and for cash on delivery code is "cashondelivery". Its for default magento code if any one have not customized related to it. Commented May 16, 2016 at 7:37
  • Hey, Thanks for you answer. I have just one more doubt. For cash on delivery, we have a charge of 60 rs that shloud get added to the total order amount. How should i go about it? Commented Jun 6, 2016 at 9:20
1

Rep won't let me comment. But I get an error when trying this. on 1.9

Invalid method Mage_Customer_Model_Address::collectShippingRates(Array())

Your Answer

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

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