Foreword
Constructed from Magento app/code/core/Mage/Checkout/Block/Onepage/Shipping/Method/Available.php and others:
app/design/frontend/base/default/template/checkout/onepage/shipping_method/available.phtml uses this code to determine which shipping method was selected:
$this->getAddressShippingMethod()
app/code/core/Mage/Checkout/Block/Onepage/Shipping/Method/Available.php expands that code to this:
return $this->getAddress()->getShippingMethod();
Let's research a bit and expand it even deeper:
$this->getQuote()->getShippingAddress()->getShippingMethod();
Parent block expands method getQuote():
return $this->getCheckout()->getQuote();
And deeper:
public function getChechout() {
return Mage::getSingleton('checkout/session');
}
Merging all that code gives us this:
Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getShippingMethod()
That gives you the shipping method code. Giving that, you could manipulate it just as you wish. This data is stored within the database, so when you change shipping method, the code changes too.
Getting deeper and deeper!
If you've ever created your own shipping method, you'd know, that it has the method called collectRates().
It fills a set of shipping/rate_result_method models, stores it within the instance of shipping/rate_result model and returns it (you can get each model' instance using Mage::getModel(<model i've named>); ).
Yet, note: one could contain multiple rate_result_method instances, while the shipping method code is the same for all those instances!
Thus, in order to get the description, you need to get one of the rate_result_method instances and retrieve its methodTitle or carrierTitle.
After a small researching i've found how to retrieve all these rates:
Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getShippingRatesCollection()
This will provide you with a collection of all rates for the selected shipping method. You can operate it with getItems() and get a hash. Or you could use getFirstItem() and use it as the template.
Anyway, let's assume u've retrieved some item of that collection and stored it within the $rate variable:
$rate->getCarrier(); // This will provide you with the carrier code
$rate->getCarrierTitle(); // This will give you the carrier title
$rate->getCode(); // This will give you **current shipping method** code
$rate->getMethod(); // This will provide you with the **shipping method** code
$rate->getMethodTitle(); // This will tell you current shipping method title
$rate->getMethodDescription(); // And this is the description of the current shipping method and **it could be NULL**
That's all, folks!
I am really sorry for my poor English and for my strange mind flow. Hope this will help you or someone else. Thanks!
If I am parsing your question correctly, are you saying that getQuote()->getBillingAddress()
is getting the customer's default billing address instead of the new address the customer has entered in the order?
I have this problem in Magento 1.4.0.1 as well, I worked around it by getting all the addresses from the customer and comparing every attribute with the address specified in the order to find out the real entity ID of the address.
I copied this from my code with some parts of custom business logic removed, so consider it untested, but you get the idea:
(The code is only tested on Magento 1.4.0.1 and might not apply to Magento 1.5)
$currentCustomer = Mage::getModel('customer/customer')->load($order['customer_id']);
$attributesToCompare = array('firstname', 'lastname', 'country_id', 'region', 'region_id', 'city', 'telephone', 'postcode', 'company', 'fax', 'prefix', 'middlename', 'suffix', 'street');
$orderAddresses = array(
'billing' => $order->getBillingAddress()->getData(),
'shipping' => $order->getShippingAddress()->getData()
);
$foundExistingCustomerAddressEntityId = array(
'billing' => false,
'shipping' => false
);
$billingSameAsShipping = false;
$currentCustomerAddresses = $currentCustomer->getAddressesCollection();
// is the billing/shipping address currently found in the customer's address book?
foreach ($orderAddresses as $orderAddressKey => $orderAddress) {
//var_dump($orderAddress);
foreach ($currentCustomerAddresses as $currentCustomerAddressObj) {
$currentCustomerAddress = $currentCustomerAddressObj->getData();
$attributesMatchCount = 0;
foreach ($attributesToCompare as $attributeToCompare) {
if (empty($currentCustomerAddress[$attributeToCompare])) {
$currentCustomerAddress[$attributeToCompare] = false;
}
$attributesMatchCount += ($orderAddress[$attributeToCompare] == $currentCustomerAddress[$attributeToCompare])?1:0;
//echo 'attributesMatchCount: '.$attributesMatchCount." {$orderAddress[$attributeToCompare]} {$currentCustomerAddress[$attributeToCompare]}\n";
}
if ($attributesMatchCount == count($attributesToCompare)) {
$foundExistingCustomerAddressEntityId[$orderAddressKey] = $currentCustomerAddress['entity_id'];
//echo 'foundExistingCustomerAddressEntityId['.$orderAddressKey.']: '.$foundExistingCustomerAddressEntityId[$orderAddressKey]."\n\n";
break;
}
}
}
$billingShippingExactMatchCount = 0;
foreach ($attributesToCompare as $attributeToCompare) {
$billingShippingExactMatchCount += ($orderAddresses['billing'][$attributeToCompare] == $orderAddresses['shipping'][$attributeToCompare])?1:0;
}
if ($billingShippingExactMatchCount == count($attributesToCompare)) {
$billingSameAsShipping = true;
}
Best Answer
Shipping and billing addresses are children of quote object. So something like this shall work: