I am trying to create a dropdown menu of available shipping methods to be selected by the customer on any page of my Magento store (it is situated at the top of the page in a ‘floating’ bar).
I also display the order total in this bar, when I select a different shipping method the order total doesn’t update unless I am on the cart page.
Does anyone have any ideas why? Is there something on the cart page that I need to include on every page?
My current code is as follows:
Mage :: app ( "default" ); Mage :: getSingleton ( "core/session" , array( "name" => "frontend" )); $cartHelper = Mage :: helper ( 'checkout/cart' ); $quote = Mage :: getSingleton ( 'checkout/cart' )-> getQuote (); if( $cartHelper -> getItemsCount ()< 1 ) { echo '<select name="delivery" style="font-size:9px; width:100%;"> <option selected="selected" style="padding:3px;">' . "Please add at-least 1 product to see shipping methods" . '</option></select>' ; return; } $shippingAddress = $quote -> getShippingAddress (); $groups = $shippingAddress -> getGroupedAllShippingRates (); if(! $groups ) { $shippingCountry = "GB" ; $shippingRegionID = "20" ; $shippingRegion = "" ; $postcode = "" ; $shippingAddress -> setCountryId ( $shippingCountry ) -> setCity ( "" ) -> setPostcode ( $postcode ) -> setRegionId ( $shippingRegionID ) -> setRegion ( $shippingRegion ) -> setCollectShippingRates ( true ); //echo $quote->getCode(); $code = "matrixrate_matrixrate" ; $shippingAddress -> setShippingMethod ( $code ) /*->collectTotals()*/ -> save (); $groups = getGroupedAllShippingRates (); } echo '<form id="co-shipping-method-form" action="<MY_URL>/index.php/checkout/cart/estimateUpdatePost/">' ; $countShip = 1 ; foreach ( $groups as $code => $_rates ): echo "<select name=\" estimate_method\ " onchange=\" this . form . submit () \ " style=\" font - size : 9px ; width : 100 %; \ ">" ; echo "<option style=\" padding : 3px ; \ ">" . "Please Select" . "</option>" ; foreach ( $_rates as $_rate ): $selected = ( $_rate -> getCode () == $quote -> getShippingAddress ()-> getShippingMethod ())? "selected" : "" ; $text = $_rate -> getMethodTitle (). " " ; if( $_rate -> getPrice () != "" ): $_incl = number_format ( Mage :: helper ( 'tax' )-> getShippingPrice ( $_rate -> getPrice (), true ), 2 , "." , "," ); $text .= "£" . $_incl ; endif; echo "<option value=\"".$_rate->getCode()." \ " " . $selected . " style=\" padding : 3px ; \ " >" . $text . "</opton>" ; endforeach; echo "</select>" ; endforeach; echo '</form>' ;
Signature
“First, solve the problem. Then, write the code.”