I'm trying to get available shipping methods and create an order in model. I have Flatrate and UPS enabled, and see these options in checkout on frontend.
Particularly, list of available methods is formed here:
app/code/core/Mage/Checkout/Block/Onepage/Shipping/Method/Available.php
$this->getAddress()->collectShippingRates()->save();
$groups = $this->getAddress()->getGroupedAllShippingRates();
Here is my code in Model I'm using to create order:
$email = '[email protected]';
$websiteId = Mage::app()->getWebsite()->getId();
$store = Mage::app()->getStore();
// Start New Sales Order Quote
$quote = Mage::getModel('sales/quote')->setStoreId($store->getId());
$product = Mage::getModel('catalog/product')->load(1);
$quote->addProduct($product, new Varien_Object(array('qty' => 1)));
// Set Sales Order Quote Currency
$quote->setCurrency(Mage::app()->getStore()->getCurrentCurrencyCode());
try{
$customer = Mage::getModel('customer/customer')
->setWebsiteId($websiteId)
->loadByEmail($email);
}
catch (Exception $e)
{
$error['error'] = $e->getMessage();
return $error;
}
// Assign Customer To Sales Order Quote
$quote->assignCustomer($customer);
// Set Sales Order Billing Address
$defaultBillingAdress = $customer->getDefaultBillingAddress();
$billingAddress = $quote->getBillingAddress()->addData(
[
'customer_address_id' => $defaultBillingAdress->getEntityId(),
'prefix' => $defaultBillingAdress->getPrefix(),
'firstname' => $defaultBillingAdress->getFirstname(),
'middlename' => $defaultBillingAdress->getMiddlename(),
'lastname' => $defaultBillingAdress->getLastname(),
'suffix' => $defaultBillingAdress->getSuffix(),
'company' => $defaultBillingAdress->getCompany(),
'street' => [
'0' => $defaultBillingAdress->getStreet1(),
'1' => $defaultBillingAdress->getStreet2(),
],
'city' => $defaultBillingAdress->getCity(),
'country_id' => $defaultBillingAdress->getCountryId(),
// 'region' => $defaultBillingAdress->getRegion(),
'region_id' => $defaultBillingAdress->getRegionId(),
'postcode' => $defaultBillingAdress->getPostcode(),
'telephone' => $defaultBillingAdress->getTelephone(),
'fax' => $defaultBillingAdress->getFax(),
'vat_id' => $defaultBillingAdress->getVatId(),
]
);
// Set Sales Order Shipping Address
$defaultShippingAdress = $customer->getDefaultShippingAddress();
$shippingAddress = $quote->getShippingAddress()->addData(
[
'customer_address_id' => $defaultShippingAdress->getEntityId(),
'prefix' => $defaultShippingAdress->getPrefix(),
'firstname' => $defaultShippingAdress->getFirstname(),
'middlename' => $defaultShippingAdress->getMiddlename(),
'lastname' => $defaultShippingAdress->getLastname(),
'suffix' => $defaultShippingAdress->getSuffix(),
'company' => $defaultShippingAdress->getCompany(),
'street' => [
'0' => $defaultShippingAdress->getStreet1(),
'1' => $defaultShippingAdress->getStreet2(),
],
'city' => $defaultShippingAdress->getCity(),
'country_id' => $defaultShippingAdress->getCountryId(),
// 'region' => $defaultBillingAdress->getRegion(),
'region_id' => $defaultShippingAdress->getRegionId(),
'postcode' => $defaultShippingAdress->getPostcode(),
'telephone' => $defaultShippingAdress->getTelephone(),
'fax' => $defaultShippingAdress->getFax(),
'vat_id' => $defaultShippingAdress->getVatId(),
]
);
$shippingAddress->setCollectShippingRates(true)
->collectShippingRates();
$quote->getPayment()->importData(array('method' => 'checkmo'));
$address = $quote->getShippingAddress();
// here things go wrong
$rates = $address->getGroupedAllShippingRates();
Only Flatrate in $rates, as opposed to checkout Block. What Am I doing wrong? Do I miss adding some vital info to quote?
Extra: Using code from this answer worked a bit: https://stackoverflow.com/a/12344228/6505678
// Update the cart's quote.
$cart = Mage::getSingleton('checkout/cart');
$address = $cart->getQuote()->getShippingAddress();
$address->setCountryId($country)
->setPostcode($zipcode)
->setCollectShippingrates(true);
$cart->save();
// Find if our shipping has been included.
$rates = $address->collectShippingRates()
->getGroupedAllShippingRates();
This gave me the right shipping rates: flatrate and UPS. Maybe because I used 'checkout/cart' instead 'sales/quote'. But I couldn't add items and customer to the cart object as I added to quote object.
Is there a right way to do it?