My requirement is when the shopping cart page is loaded i need to set my custom shipping rate and show it on the cart page.I have country name and shipping price in my hand.The shipping method is matrix rate.I have tried the estimate shipping method box functionality on the page load but it didn't work.
In order to set the default shipping at cart page, you can observe the event - sales_quote_collect_totals_before
Define your event-observer in app/code/local/MagePsycho/Autoshipping/etc/config.xml
<sales_quote_collect_totals_before>
<observers>
<magepsycho_autoshipping_sales_quote_collect_totals_before>
<type>model</type>
<class>magepsycho_autoshipping/observer</class>
<method>salesQuoteCollectTotalsBefore</method>
</magepsycho_autoshipping_sales_quote_collect_totals_before>
</observers>
</sales_quote_collect_totals_before>
And your Observer Model should look like:
<?php
/**
* @category MagePsycho
* @package MagePsycho_Autoshipping
* @author Raj KB <magepsycho@gmail.com>
* @website http://www.magepsycho.com
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
class MagePsycho_Autoshipping_Model_Observer
{
function salesQuoteCollectTotalsBefore(Varien_Event_Observer $observer)
{
$quote = $observer->getEvent()->getQuote();
if (!$quote->getId()) return;
$shippingMethod = $quote->getShippingAddress()->getShippingMethod();
if ($shippingMethod) return;
$shippingAddress = $quote->getShippingAddress();
$country = 'US'; // Some country code
$postcode = '1234'; // Some postcode
$regionId = '1234'; // Some region id
$method = 'your-custom-shipping-method-code'; // shipping method code
$shippingAddress
->setCountryId($country)
->setRegionId($regionId)
->setPostcode($postcode)
->setShippingMethod($method)
->setCollectShippingRates(true)
;
$shippingAddress->save();
$quote->save();
}
}