Magento How to add shipping charge to the cart automatically for logged in customer only

Sometime we needs to apply shipping charge on shopping cart page after adding products to cart directly but generally in Magento the shipping cost only apply when the customer manually run shipping cost estimator or in checkout page after selecting country.

In order to get this working, you need a custom module which hooks into the event ‘sales_quote_save_before’. 

Please consider following assumption to create custom module:

 - Namespace as 'Pg'
 - And Module name as 'Cartshipping'

Step 1: Create a custom module definition xml; in path app/etc/modules, create the file Pg_Cartshipping.xml and add code below.

<config>
<modules>
<Pg_Cartshipping>
<active>true</active>
<codePool>local</codePool>
</Pg_Cartshipping>
</modules>
</config>


Step 2: Now creates the following directory structure

app/code/local/Pg
app/code/local/Pg/Cartshipping
app/code/local/Pg/Cartshipping/etc
app/code/local/Pg/Cartshipping/Model


Step 3: Now under the folder "app/code/local/Pg/Cartshipping/etc", create a file "config.xml"" and add the following content:


<?xml version="1.0"?>
<config>
<modules>
<Pg_Cartshipping>
<version>0.1.0</version>
</Pg_Cartshipping>
</modules>
<global>
<models>
<pg_cartshipping>
<class>Pg_Cartshipping_Model</class>
</pg_cartshipping>
</models>
</global>
<frontend>
<events>
<checkout_cart_save_before>
<observers>
<pg_cartshipping_observer>
<type>singleton</type>
<class>pg_cartshipping/observer</class>
<method>addShippingCost</method>
</pg_cartshipping_observer>
</observers>
</checkout_cart_save_before>
</events>
</frontend>
</config>


Step 4: Now under the folder "app/code/local/Pg/Cartshipping/Model", create a file "Observer.php" and add the following content:

class Pg_Cartshipping_Model_Observer {

private $_shippingCode = 'tablerate_tablerate'; // change your default shipping method code here
private $_country = '';

public function addShippingCost($params = null) {

$customer = Mage::getSingleton('customer/session')->getCustomer();
$customerAddressId = $customer->getDefaultShipping();
if ($customerAddressId)
{
$address = Mage::getModel('customer/address')->load($customerAddressId);
$cust_data = $address->getData();
$this->_country = $cust_data['country_id'];
}
if($this->_country != "")
{
if (Mage::registry('checkout_addShipping')) {
Mage::unregister('checkout_addShipping');
return;
}
Mage::register('checkout_addShipping',true);
 
$cart = Mage::getSingleton('checkout/cart');
$quote = $cart->getQuote();
 
if ($quote->getCouponCode() != '') {
$c = Mage::getResourceModel('salesrule/rule_collection');
$c->getSelect()->where("code=?", $quote->getCouponCode());
foreach ($c->getItems() as $item) { $coupon = $item; }
 
if ($coupon->getSimpleFreeShipping() > 0) {
$quote->getShippingAddress()->setShippingMethod($this->_shippingCode)->save();
return true;
}
}
 
try {
$method = $quote->getShippingAddress()->getShippingMethod();
if ($method) return; // don't overwrite if default set
 
if ($quote->getShippingAddress()->getCountryId() == '') {
$quote->getShippingAddress()->setCountryId($this->_country);
}
 
$quote->getShippingAddress()->setCollectShippingRates(true);
$quote->getShippingAddress()->collectShippingRates();
 
$rates = $quote->getShippingAddress()->getAllShippingRates();
$allowed_rates = array();
foreach ($rates as $rate) {
array_push($allowed_rates,$rate->getCode());
}
 
if (!in_array($this->_shippingCode,$allowed_rates) && count($allowed_rates) > 0) {
$shippingCode = $allowed_rates[0];
}

if (!empty($shippingCode)) {
$address = Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress();
if ($address->getCountryId() == '') $address->setCountryId($this->_country);
if ($address->getCity() == '') $address->setCity('');
if ($address->getPostcode() == '') $address->setPostcode('');
if ($address->getRegionId() == '') $address->setRegionId('');
if ($address->getRegion() == '') $address->setRegion('');
$address->setShippingMethod($this->_shippingCode)->setCollectShippingRates(true);
Mage::getSingleton('checkout/session')->getQuote()->save();
} else {
$address = Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress();
if ($address->getCountryId() == '') $address->setCountryId($this->_country);
if ($address->getCity() == '') $address->setCity('');
if ($address->getPostcode() == '') $address->setPostcode('');
if ($address->getRegionId() == '') $address->setRegionId('');
if ($address->getRegion() == '') $address->setRegion('');
$address->setShippingMethod($this->_shippingCode)->setCollectShippingRates(true);
Mage::getSingleton('checkout/session')->getQuote()->save();
}
 
Mage::getSingleton('checkout/session')->resetCheckout();
 
}
catch (Mage_Core_Exception $e) {
Mage::getSingleton('checkout/session')->addError($e->getMessage());
}
catch (Exception $e) {
Mage::getSingleton('checkout/session')->addException($e, Mage::helper('checkout')->__('Load customer quote error'));
}
}


}

public function getQuote() {
        if (empty($this->_quote)) {
            $this->_quote = Mage::getSingleton('checkout/session')->getQuote();
        }
        return $this->_quote;
    }

}



## Done. Clear Cache ##
Magento How to add shipping charge to the cart automatically for logged in customer only Magento How to add shipping charge to the cart automatically for logged in customer only Reviewed by Web Technology Funda on 3:14:00 AM Rating: 5

No comments

Free! Free!Free! Subscribe to Get Free PHP (Magento) tutorial Update in Your Inbox!!! Hurry Up!!!