I am having trouble getting my Script to create an order on my live site that uses Magento 1.7. The error I am getting is “please specify a shipping method” more detail is
blockquote Fatal error: Uncaught exception 'Mage_Core_Exception' with message 'Please specify a shipping method.' in /home/mysite/public_html/app/Mage.php:594 Stack trace: #0 /home/mysite/public_html/app/code/core/Mage/Sales/Model/Service/Quote.php(303): Mage::throwException('Please specify ...') #1 /home/mysite/public_html/app/code/core/Mage/Sales/Model/Service/Quote.php(222): Mage_Sales_Model_Service_Quote->_validate() #2 /home/mysite/public_html/app/code/core/Mage/Sales/Model/Service/Quote.php(238): Mage_Sales_Model_Service_Quote->submitNominalItems() #3 /home/mysite/public_html/apitest/magento_order_create.php(82): Mage_Sales_Model_Service_Quote->submitAll() #4 {main} thrown in /home/mysite/public_html/app/Mage.php on line 594
I am trying to use the script below to create the order and I am passing the skus and quantities in the post from another page. `
<?php
// Link Mage Class
require ('../app/Mage.php');
// Initialize Magento framework
Mage::app('mysite');
//create a cart
$quote = Mage::getModel('sales/quote')
->setStoreId(Mage::app()->getStore('mysite')->getId());
//Get Customer by Id
$customer = Mage::getModel('customer/customer')->load('1');
//attach customer to cart
$quote->assignCustomer($customer);
//attach products
foreach ($_POST as $sku=>$qty)
{
$product = Mage::helper('catalog/product')->getProduct($sku,Mage::app()->getStore()->getId(), 'sku');
$buyInfo = array(
'qty' => $qty,
// custom option id => value id
// or
// configurable attribute id => value id
);
$quote->addProduct($product, new Varien_Object($buyInfo));
}
//get and set customer billing address
//need to work on this encase we use diffrent billing and shipping addresses
$addressData = Mage::getModel('customer/address')->load('1');
$billingAddress = $quote->getBillingAddress()->addData($addressData);
$shippingAddress = $quote->getShippingAddress()->addData($addressData);
// set shipping and payment methods. assumes freeshipping and check payment
// have been enabled.
/*
$shippingAddress->setCollectShippingRates(true)->collectShippingRates()
->setShippingMethod('freeshipping_freeshipping')
->setPaymentMethod('checkmo');
*/
// THIS IS WHERE THE ERROR SEEMS TO BE
$quote->getShippingAddress()->setShippingMethod('freeshipping_freeshipping');
$quote->getShippingAddress()->setCollectShippingRates(true);
$quote->getShippingAddress()->collectShippingRates();
//set payment method
$quote->getPayment()->importData(array('method' => 'checkmo'));
//save cart and check out
$quote->collectTotals()->save();
$service = Mage::getModel('sales/service_quote', $quote);
$service->submitAll();
$order = $service->getOrder();
printf("Created order %s\n", $order->getIncrementId());
`
The above script originally came from http://pastebin.com/8cft4d8v and it works like a charm on my test environment (also 1.7). I have commented out the original shipping method code and broke is out into separate lines. The live site has two sites and two stores, I have made sure that free shipping is enabled on the backend and tested that it shows up when I run this script.
<?php
// Link Mage Class
require ('../app/Mage.php');
// Initialize Magento framework
Mage::app('mysite');
$methods = Mage::getSingleton('shipping/config')->getActiveCarriers();
var_dump($methods);
I am fairly certain the address is set correctly, if I echo is out to the screen that address is there.
I have tried reading the documentation, looking at other sample code and changing parts of it to see if I can get the shipping method set, but no luck.