Friday, 28 November 2014

magento set shipping estimate on cart page and get the shipping rates

<?php
$zipcode = '2000'; // Your zipcode
$country = 'GB'; // Your country Id
$cart = Mage::getSingleton('checkout/cart');
$address = $cart->getQuote()->getShippingAddress();
$address->setCountryId($country)->setPostcode($zipcode)->setCollectShippingrates(true);
$cart->save();

$rates = $address->collectShippingRates() ->getGroupedAllShippingRates();
if($rates) {
?>
<form id="co-shipping-method-form" action="<?php echo $this->getUrl('checkout/cart/estimateUpdatePost') ?>" >
<?php
foreach ($rates as $carrier) {
    foreach ($carrier as $rate) {
//echo '<pre>';   print_r($rate->getData()); echo '</pre>';
if($rate->getCode() == "express_express") {  // Only show your custom shipping module
?>
<input name="estimate_method" type="checkbox" value="<?php echo $rate->getCode(); ?>" id="s_method_express_express" onclick="getShipppingCost();"  <?php if($rate->getCode()===$address->getShippingMethod()) echo ' checked="checked"' ?> class="radio">
             <label for="s_method_express_express"><?php echo $this->escapeHtml($rate->getMethodTitle()) ?>
<span class="price"> <?php echo  Mage::helper('core')->currency($rate->getPrice()); ?></span>
</label>
<?php  
}
    }
}

?>
<script>
function getShipppingCost()
{
var methodid= document.getElementById("s_method_express_express");
if(methodid.checked == true)
{ document.getElementById('co-shipping-method-form').submit(); }
else { methodid.value="freeshipping_freeshipping"; methodid.checked = true; document.getElementById('co-shipping-method-form').submit(); }

}

</script>

</form>
<?php } ?>

No comments:

Post a Comment

Thanks for your comments.