If you follow your written procedure as a human using the interface. Does the rate show up with the correct information? Is that user able to use that rate? Are the rate settings correct?
To me it looks like your trying to replicate the checkout process which might not be your way to go. Can you tell me some more about where and how you try to run this code? Custom module, template file, php file in root etc. Be as specific as possible when asking questions here. I ran your code and it doesn't work at all on my end as i expected. There are some odd things going on. I suggest you take a good look at a working example. I found this article, now the article itself doesn't have the example. i'm talking about but one of the commentors who posted a really nice example which you can run from your root directory. I tested it on magento 1.9.1 with sample data and it works like a charm.
The developers at Inchoo write really good articles like this one:
http://inchoo.net/magento/programmatically-create-order-in-magento/
Following the code which works in my demo shop. I'm sure if you look closely at this you'll solve your problem. Just let me know if you get stuck again. Good luck ;)
//these lines are required to run this php file from root. If you include it somewhere within magento you won't need it.
require_once 'app/Mage.php';
Mage::app();
//set the store
$quote = Mage::getModel('sales/quote')
->setStoreId(Mage::app()->getStore('default')->getId());
//load a customer or go for guest, known or unknown customer it works
if ('do customer orders') {
// for customer orders:
$customer = Mage::getModel('customer/customer')
->setWebsiteId(1)
->loadByEmail('customerrr@example.com');
$quote->assignCustomer($customer);
} else {
// for guesr orders only:
$quote->setCustomerEmail('customerrr@example.com');
}
// add product(s)
$product = Mage::getModel('catalog/product')->load(231);
$buyInfo = array(
'qty' => 1,
// custom option id => value id
// or
// configurable attribute id => value id
);
$quote->addProduct($product, new Varien_Object($buyInfo));
$addressData = array(
'firstname' => 'Test',
'lastname' => 'Test',
'street' => 'Sample Street 1000',
'city' => 'Somewhere',
'postcode' => '123456789',
'telephone' => '123456789',
'country_id' => 'US',
'region_id' => 12, // id from directory_country_region table
);
//set shipping and billing address
$billingAddress = $quote->getBillingAddress()->addData($addressData);
$shippingAddress = $quote->getShippingAddress()->addData($addressData);
//get and set methods
$shippingAddress->setCollectShippingRates(true)->collectShippingRates()
->setShippingMethod('flatrate_flatrate')
->setPaymentMethod('checkmo');
$quote->getPayment()->importData(array('method' => 'checkmo'));
$quote->collectTotals()->save();
$service = Mage::getModel('sales/service_quote', $quote);
$service->submitAll();
$order = $service->getOrder();
//these lines are required to run this php file from root. If you include it somewhere within magento you won't need it.
require_once 'app/Mage.php';
Mage::app();
//set the store
$quote = Mage::getModel('sales/quote')
->setStoreId(Mage::app()->getStore('default')->getId());
//load a customer or go for guest, known or unknown customer it works
if ('do customer orders') {
// for customer orders:
$customer = Mage::getModel('customer/customer')
->setWebsiteId(1)
->loadByEmail('customerrr@example.com');
$quote->assignCustomer($customer);
} else {
// for guesr orders only:
$quote->setCustomerEmail('customerrr@example.com');
}
// add product(s)
$product = Mage::getModel('catalog/product')->load(231);
$buyInfo = array(
'qty' => 1,
// custom option id => value id
// or
// configurable attribute id => value id
);
$quote->addProduct($product, new Varien_Object($buyInfo));
$addressData = array(
'firstname' => 'Test',
'lastname' => 'Test',
'street' => 'Sample Street 1000',
'city' => 'Somewhere',
'postcode' => '123456789',
'telephone' => '123456789',
'country_id' => 'US',
'region_id' => 12, // id from directory_country_region table
);
//set shipping and billing address
$billingAddress = $quote->getBillingAddress()->addData($addressData);
$shippingAddress = $quote->getShippingAddress()->addData($addressData);
//get and set methods
$shippingAddress->setCollectShippingRates(true)->collectShippingRates()
->setShippingMethod('flatrate_flatrate')
->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());
To force shipping method reloading try,
define(
[
'Magento_Checkout/js/model/quote',
'Magento_Checkout/js/model/shipping-rate-processor/new-address',
'Magento_Checkout/js/model/shipping-rate-processor/customer-address',
'Magento_Checkout/js/model/shipping-rate-registry'
],
function (quote, defaultProcessor, customerAddressProcessor, rateRegistry) {
'use strict';
var processors = [];
rateRegistry.set(quote.shippingAddress().getCacheKey(), null);
processors.default = defaultProcessor;
processors['customer-address'] = customerAddressProcessor;
var type = quote.shippingAddress().getType();
if (processors[type]) {
processors[type].getRates(quote.shippingAddress());
} else {
processors.default.getRates(quote.shippingAddress());
}
}
);
Best Answer
I had the same problem. The problem is on your xml definition of the rates validation:
In your xml, when you define the js validator:
<item name="inchoo" xsi:type="array">
You must change the name to:
<item name="inchoo-rates-validation" xsi:type="array">
And inchoo must be the same as carrier code of your shipping method. The problem is when magento try lo find and load all validators in :
...vendor/magento/module-checkout/Block/Checkout/LayoutProcessor.php
on method processShippingChildrenComponents
If magento does not find the same name as carrier, unset the file in layout.
Regards
Mauro M. Martinez