I recently came to a situation where I need to show a shipping price amount on cart before selecting the shipping address from the checkout page. Basically shipping price is shown after the user has added the shipping address and selected the shipping method. So to show shipping price on cart page load, I needed to find out the following. Shipping Address (Actually only country_id will work) Shipping Method (Set to Flatrate in the code below) So what I did was first check if the user has already specified the shipping address (when user have added the shipping address in the checkout page and navigated back to cart page). If the shipping address is not present then I checked if the user is logged in and checked for default shipping address of the customer and used its country_id (if present), but if the user is not logged in or default shipping address is not present then I set the shipping address (country_id) to some country (NL).After setting the country I then set the shipping method and then saved the quote so that the prices are calculated.Here’s the snippet.
/*** Setting Default Shipping Method
* Checking If Quote Already Has Address Or Not
* - If Yes then leave as it is
* - Else check if Customer has default Shipping Address or Not
* -- Yes then get Default Shipping Address and set Shipping Method
* -- No Set Default Shipping address to NL*/
if(!Mage::getSingleton('checkout/type_onepage')->getQuote()->getShippingAddress()->getCountryId()){
$customerSession=Mage::getSingleton("customer/session");
if($customerSession->isLoggedIn()){
$customerAddress=$customerSession->getCustomer()->getDefaultShippingAddress();
if($customerAddress->getId()){
$customerCountry=$customerAddress->getCountryId();
$shipping = Mage::getSingleton('checkout/type_onepage')->getQuote()->getShippingAddress()->setCountryId($customerCountry)->setShippingMethod('flatrate_flatrate')->save();
}else{
$shipping = Mage::getSingleton('checkout/type_onepage')->getQuote()->getShippingAddress()->setCountryId('NL')->setShippingMethod('flatrate_flatrate')->save();
}
}
}
Cheers!
[UPDATE]
The original solution seems to show some issue. Thanks to the great reader of this blog the “perfect” solution for this case can be found in comments. As always new and better solutions are always welcome.
Thanks for the posting. It helped me.
However, I added this code and it only show shipping after checkout/cart is loaded the second time.
Where do you place this code snippet to allow checkout/cart to show shipping on the first load?
Thanks for you help.
icvu
This is a great snippet, but I agree with icvu, I am not entirely sure where to place this code. Is there an event that we should write a listener for, a contoller or model we should override? If you have a moment it would be fantastic if you could share with us where you posted this.
Thanks again, really good stuff.
+1 for, where does this code go?
thanks
You can add this on cart page.
which file is this?
you mean
template\checkout\cart.phtml
or
\Mage\Checkout\controllers\CartController.php
indexAction
??
The template file cart.phtml file.
I see the logic behind the code but where exactly in cart.phtml should I put it?
Probably at the top of cart.phtml.
I’ve set the code to the top of cart.phtml.
The Code only works if the cart will be loaded the second time.
I couldn’t get this to work either, however I did find this working solution -> http://icodesnip.com/snippet/php/setting-default-shipping-method-in-magento
Has anyone figured out why it only shows the shipping method on the second time you load the cart page?
works like a charm
thanks
just an update
the script works very well with logged users
However i had to change the code ( parenthesis ) at this point
if($customerAddress->getId()){
…….
}
}else{
…..
}
}
in order to work fine with guess users
Anyway, thank you again
in order to resolve the refresh issue, you need to add these lines of code before the “main if” closure
Mage::getSingleton(‘checkout/session’)->setCartWasUpdated(true);
Mage::getModel(‘checkout/cart’)->init();
Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl(‘checkout/cart/’));
return;
Maybe should be better-written. This is the first “dirty” solution
it works. I think the script in the post should be updated.
This blog was so well written and was so incredibly informative. Even Comments are also useful in understanding the topic
Hi Daria,
Your solution helped me a lot! Thank you! I searched for it for weeks with no luck
A quick side question, maybe someone knows. If i want to show that shipping cost in the cart sidebar how do you pull the information?
Hi
I have done something similar for an ajax mini-cart box (we don’t use the sidebar)
Here il the code of a function in my helper class
public function setShippingCosts(){
/*** Setting Default Shipping Method USING SESSION OBJECTS!!!!*/
$session = Mage::getSingleton(‘checkout/session’);
$quote = $session->getQuote();
$address = $quote->getShippingAddress();
$sessionCost = $session -> getQuoteShippingCost();
$sessionCountry = $session -> getQuoteShippingCountry();
if ((!$sessionCost || $sessionCost == 0) ) {
$country = null;
$customerSession = Mage::getSingleton(“customer/session”);
if ($customerSession -> isLoggedIn()) {
$customerAddress = $customerSession -> getCustomer() -> getDefaultShippingAddress();
if ($customerAddress != null) {
if ($customerAddress -> getId()) $country = $customerAddress -> getCountryId();
}
}
if ($country == null) $country = ‘FR’;
$address->setCountryId($country)->setCollectShippingRates(true)->collectShippingRates();
$quote->setShippingMethod(‘flatrate_flatrate’)->save();
$method = $quote->getShippingMethod();
if ($method) {
foreach ($address->getAllShippingRates() as $rate) {
if ($rate->getCode()==$method) {
$session->setQuoteShippingCost($address->getQuote()->getStore()->convertPrice($rate->getPrice(), false));
Mage::getSingleton(‘checkout/type_onepage’)->getQuote()->getShippingAddress()->setCountryId($country)->setShippingMethod($method)->save();
break;
}
}
}
//$session->getQuote()->getShippingAddress()->setCollectShippingRates(false)->removeAllShippingRates()->save();
$session->getQuote()->save();
$session->resetCheckout();
}else{
//log
}
}
I call this function everytime the user accesses to the mini-cart, working this way:
$this->getHelper()->setShippingCosts();
$quote = Mage::getSingleton(‘checkout/session’)->getQuote();
$gTotal = (int)$quote->getGrandTotal() ;
$totals = $quote->getTotals();
if(!isset($totals['shipping']))
$gTotal = ( $gTotal + Mage::getSingleton(‘checkout/session’)->getQuoteShippingCost());
As you can see, I save my shipping price value in a session variable
This is not a perfect solution but it works fine
Hope this helps
This is what worked for me. This eliminates the need to refresh the page in the Magento Cart.
$shippingAddress = $this->getQuote()->getShippingAddress();
if (!$shippingAddress->getCountryId()) {
$shippingAddress->setCountryId() ==’US’;}
if ($shippingAddress->getCountryId() ==’US’) {
if($shippingAddress->getShippingMethod() ==”){
$shippingAddress->setShippingMethod(‘msmultiflat_Standard US Shipping’)->save();
$this->getQuote()->save();
Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl(‘checkout/cart/’));
}
}else{
if ($shippingAddress->getCountryId() !=’US’) {
if($shippingAddress->getShippingMethod() ==”){
$shippingAddress->setShippingMethod(‘fsmultiflat_Standard International Shipping’)->save();
$shippingAddress->save();
Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl(‘checkout/cart/’));
}
}
}
Great! This solution works perfectly and seem to be the only one