I'm working on a mobile application, that is creating an order on Magento programmatically through a custom REST implementation, and everything is working fine, except one thing that i can't find any documentation on: How to set the shipping method for the quote that complies with the available shipping method rules.
So for example, from the Magento backend there are 3 shipping methods enabled:
- Free - Only when cart amount is above 80 USD
- Fixed Rate - Only if Customer is ordering from USA
- Tablerate - If the customer is ordering from outside the USA.
What I was doing was as follows (assuming $quote
has the quote built up and the products added to it):
$shippingAddress = $quote->getShippingAddress()->addData($shipping_address);
$methods = Mage::getSingleton('shipping/config')->getActiveCarriers();
foreach($methods as $_code => $_method){
// Get the first allowed method of this shipping carrier
$m = key($_method->getAllowedMethods());
$shipping_method = $_code . '_' . $m;
break;
}
$quote->collectTotals();
$shippingAddress->setWeight( $weights )
->setFreeMethodWeight( $weights )
->setShippingMethod( $shipping_method );
Which is kinda retarded, I'm simply getting all ActiveCarriers, and assigning the first one to the quote, which can work in simple scenarios. However, in this example, the first Active Carrier won't work, because i'm not validating the country rules or free shipping rules. So my question is, how can I get the best (and valid) shipping methods for this quote. Is there any built-in function or helper method that I can use?
I'm assuming that I don't want to give the option to the customer to select the shipping method. I want to just assign the complying shipping method to the quote.