In my case, I need to adding shipping cost of Webshopapps Matrix Rates to cart automatically. If you meet the same requirement , you can follow these steps to make it done:
1/Create a xml file to declare new module: app/etc/modules/Inbusiness_Cart.xml
<?xml version="1.0"?> <config> <modules> <Inbusiness_Cart> <active>true</active> <codePool>local</codePool> </Inbusiness_Cart> </modules> </config>
2/Create app/code/local/Inbusiness/Cart/etc/config.xml
<?xml version="1.0"?> <config> <modules> <Inbusiness_Cart> <version>1.1.1</version> </Inbusiness_Cart> </modules> <global> <models> <Inbusiness_Cart> <class>Inbusiness_Cart_Model</class> </Inbusiness_Cart> </models> </global> <frontend> <events> <checkout_cart_save_before> <observers> <inbusiness_set_shipping_observer> <type>singleton</type> <class>Inbusiness_Cart/observer</class> <method>setShipping</method> </inbusiness_set_shipping_observer> </observers> </checkout_cart_save_before> </events> </frontend> </config>
3/ Create app/code/local/Inbusiness/Cart/Model/Observer.php
<?php class Inbusiness_Cart_Model_Observer { private $_shippingCode = 'matrixrate'; private $_country = 'NO'; public function setShipping($observer) { /*if (Mage::registry('checkout_addShipping')) { Mage::unregister('checkout_addShipping'); return; } Mage::register('checkout_addShipping',true);*/ $event = $observer->getEvent(); $cart = $event->getCart(); $rates = $cart->getQuote()->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]; $cart->getQuote()->getShippingAddress()->setShippingMethod($shippingCode)->save(); } return; } } ?>
Resource:
http://excellencemagentoblog.com/magento-shipping-method-dropdown-default-shipping-method-on-cart-page
http://www.danneh.org/2010/08/adding-shipping-costs-to-the-cart-automatically-in-magento/
http://www.nicksays.co.uk/magento-events-cheat-sheet-1-8/
Mage::dispatchEvent(‘checkout_cart_save_before’, array(‘cart’=>$this));
flamefox
July 29
Hi! Here’s also a free ready solution for this https://amasty.com/shipping-table-rates.html. The mod helps calculate shipping cost according to default address. the cost is displayed in the shopping cart.
Samer Hakim
July 29
Hi… this only seems to work if the customer is logged in. Can we also make it appear even if the customer is not logged in?
thienphucvx
July 29
Try to set default shipping address following this link : http://excellencemagentoblog.com/blog/2012/02/07/magento-shipping-method-dropdown-default-shipping-method-on-cart-page/
at section “So this event is execute before the indexAction() of CartController is called. In the setShipping() method we will put in this code to select a default country”
Samer Hakim
July 29
ok that’s way too complicated….???!?!?!
and doesn’t that add a drop down menu?
—
Regards,
Samer Hakim
BPharm AACPA
GDipAppFin (Finsia)
*Pharmacy S.O.S”60 Minute Locum Service Or It’s FREE!”*Phone: 1300 505 247 Fax: 1300 204 656Email: sos@pharmacysos.com.au Web: http://www.pharmacysos.com.au * Conditions Apply *
Samer Hakim
July 29
i don’t have a folder with a directory like that?
—
Regards,
Samer Hakim
BPharm AACPA
GDipAppFin (Finsia)
*Pharmacy S.O.S”60 Minute Locum Service Or It’s FREE!”*Phone: 1300 505 247 Fax: 1300 204 656Email: sos@pharmacysos.com.au Web: http://www.pharmacysos.com.au * Conditions Apply *
Scraping Space
July 29
This is worked like a charm