Monthly Archives: May 2012

Hire a Magento Development India Company

If you are looking to develop, want to customize or optimize your Magento website, you need to hire a Magento specialist company. You need end-to-end Magento development and maintenance services to enjoy full control and flexibility over your Magento website.

Today, most businesses are hiring Magento Development India services for maximum cost efficiency. Ecommerce businesses are building their web store using Magento and enjoying all the essential features they need to efficiently run and manage their web store. Some of these companies have huge amount of experience in ecommerce platform in India. They are well aware about the latest trends in the industry and your key requirements as a web store owner.

This experience along with their expertise in developing and customizing Magento solutions helps them in creating best Magento solutions for their global clients. magento Authority is a similar type of company and has a strong market reputation. You can rely on them for any sort of requirement regarding Magento. Visit here.

Shipping Method Development: Dropdown and Default Setting on Cart Page

Shipping methods are very important while building large Magento ecommerce sites. Here we are discussing how to show a shipping method drop down in the cart totals and how to set a default shipping method in cart page. One of the best methods to perform this is described here.

Screenshot of the module output


(Shipping Dropdown Cart Page)

The currently used module is Excellence_Shipdrop. This module can be explained by following steps:

Adding dropdown to cart pages

Firstly, a dropdown is added in place of the default Magento shopping cart shipping total display. This can be carried out finely in your config.xml files as follows:

<global>
<sales>
<quote>
<totals>
<shipping>
<class>shipdrop/sales_quote_address_total_shipping</class>
<renderer>shipdrop/checkout_total_shipping</renderer>
</shipping>
</totals>
</quote>
</sales>
</global>

Here the value of the <renderer> tag must be noticed. In this module, I have added path to a block class. The <renderer> tag specifies the block class that Magento ecommerce development should use to display the shipping cart totals. Below given code is used inside the block class:

<?php
class Excellence_Shipdrop_Block_Checkout_Total_Shipping extends Mage_Checkout_Block_Total_Default
{
protected $_template = ‘shipdrop/checkout/total/shipping.phtml’;
}

This code is and specifies the path of the template file. The code in shipdrop/checkout/total/shipping.phtml template file is:

<tr>
<th colspan=”2″ style=”<?php echo $this->getTotal()->getStyle() ?>”>
<?php
$block = $this->getLayout()->createBlock(‘checkout/cart_shipping’);
$block->setTemplate(‘shipdrop/checkout/total/shipping/dropdown.phtml’);
echo $block->_toHtml();
?>
</th>
</tr>

While the code in the file shipdrop/checkout/total/shipping/dropdown.phtml is:

<?php if (($_shippingRateGroups = $this->getEstimateRates())): ?>
<form id=”co-shipping-method-form-drop” action=”<?php echo $this->getUrl(‘checkout/cart/estimateUpdatePost’) ?>”>
<select id=’drop_estimate_method’ name=”estimate_method”>
<option value=’-1′>Please Select Shipping Method</option>
<?php
foreach ($_shippingRateGroups as $code => $_rates) {
$str = $this->getCarrierName($code);
foreach ($_rates as $_rate){
$str2 = $str . ‘ – ‘ . $_rate->getMethodTitle();
$_excl = $this->getShippingPrice($_rate->getPrice(), $this->helper(‘tax’)->displayShippingPriceIncludingTax());
$_incl = $this->getShippingPrice($_rate->getPrice(), true);
$str2 .= ‘ ['. $_excl;
if ($this->helper('tax')->displayShippingBothPrices() && $_incl != $_excl) {
$str2 .= ' '. $this->__('Incl. Tax'); echo $_incl;
}
$str2 .= '] ‘;
?>
<option <?php if($_rate->getCode()===$this->getAddressShippingMethod()) echo ‘ selected=”selected”‘ ?> value=”<?php echo $this->htmlEscape($_rate->getCode()) ?>”><?php echo $str2;?></option>
<?php }
} ?>
</select>
</form>
<script type=”text/javascript”>
document.observe(“dom:loaded”, function() {
$(‘drop_estimate_method’).observe(‘change’,function(){
if($(‘drop_estimate_method’).getValue() == -1){
alert(‘Please Select Shipping Method’);
}else{
$(‘co-shipping-method-form-drop’).submit();
}
});
});
</script>
<?php endif; ?>

Drop down is generated by the above code.

We also have to make changes to the Magento shopping cart shipping totals collector class, as by default if shipping amount is zero no totals show up. So we need to make a change in the collector class to show totals even when shipping amount is zero. So the following code must be added to the class which we mentioned in the config.xml file shipdrop/sales_quote_address_total_shipping:

<?php
class Excellence_Shipdrop_Model_Sales_Quote_Address_Total_Shipping extends Mage_Sales_Model_Quote_Address_Total_Shipping{
public function fetch(Mage_Sales_Model_Quote_Address $address)
{
$amount = $address->getShippingAmount();
$title = Mage::helper(‘sales’)->__(‘Shipping & Handling’);
if ($address->getShippingDescription()) {
$title .= ‘ (‘ . $address->getShippingDescription() . ‘)’;
}
$address->addTotal(array(
‘code’ => $this->getCode(),
‘title’ => $title,
‘value’ => $address->getShippingAmount()
));
return $this;
}
}

Now the shipping method dropdown for Magento ecommerce development should show-up in totals. The problem to be fixed now is that, when a product is first added to shopping cart no country is selected by user hence no shipping method will show up. So a code must be added to select the shipping country by default.

Setting shipping country by default

The codes incorporated for this purpose are:

<events>
<controller_action_predispatch_checkout_cart_index >
<observers>
<preIndex>
<class>shipdrop/observer</class>
<method>setShipping</method>
</preIndex>
</observers>
</controller_action_predispatch_checkout_cart_index>
</events>

This event is execute before the indexAction() of CartController is called. The code included in the setShipping() method to select a default country is:

<?php
class Excellence_Shipdrop_Model_Observer {
public function setShipping($evt){
$controller = $evt->getControllerAction();
if(!Mage::getSingleton(‘checkout/type_onepage’)->getQuote()->getShippingAddress()->getCountryId() && Mage::getSingleton(‘checkout/type_onepage’)->getQuote()->getItemsCount()){
$country_id = ‘IN’;
$region_id = false;
$country = Mage::getModel(‘directory/country’)->loadByCode($country_id);
$regions = $country->getRegions();
if(sizeof($regions) > 0){
$region = $regions->getFirstItem();
$region_id = $region->getId();
}
$customerSession=Mage::getSingleton(“customer/session”);
if($customerSession->isLoggedIn()){
$customerAddress=$customerSession->getCustomer()->getDefaultShippingAddress();
if($customerAddress->getId()){
$customerCountry=$customerAddress->getCountryId();
$region_id = $customerAddress->getRegionId();
$region = $customerAddress->getRegion();
$quote = Mage::getSingleton(‘checkout/type_onepage’)->getQuote();
$shipping = $quote->getShippingAddress();
$shipping->setCountryId($customerCountry);
if($region_id){
$shipping->setRegionId($region_id);
}
if($region){
$shipping->setRegion($region);
}
$quote->save();
$controller->getResponse()->setRedirect(Mage::getUrl(‘*/*/*’,array(‘_current’=>true)));
}else{
$quote = Mage::getSingleton(‘checkout/type_onepage’)->getQuote();
$shipping = $quote->getShippingAddress();
$shipping->setCountryId($country_id);
if($region_id){
$shipping->setRegionId($region_id);
}
$quote->save();
$controller->getResponse()->setRedirect(Mage::getUrl(‘*/*/*’,array(‘_current’=>true)));
}
}else{
$quote = Mage::getSingleton(‘checkout/type_onepage’)->getQuote();
$shipping = $quote->getShippingAddress();
$shipping->setCountryId($country_id);
if($region_id){
$shipping->setRegionId($region_id);
}
$quote->save();
$controller->getResponse()->setRedirect(Mage::getUrl(‘*/*/*’,array(‘_current’=>true)));
}
}
}
}

The above code can also be easily extended to set a default shipping method as well. Just add a single line of code before $quote->save()

$quote->setShippingMethod(‘flatrate_flatrate’);
$quote->save();

Conclusion

This post outlined the steps involved in shipping method development. You can now go ahead and use or adapt this for use in your own applications. Hopefully this post will be useful for anyone dealing with shipping rates.