0

I have added a new shipping method module for my magento store. This module allows the user to set shipping rates based on shipping address country.
I have added an observer on customer shipping address save after, the code is below. In config.xml I added new event.

<events>
             <checkout_controller_onepage_save_shipping_address_after>
                <observers>
                    <zones>
                        <type>model</type>
                        <class>zones/observer</class>
                        <method>getShippingMethods</method>
                    </zones>
                </observers>
            </checkout_controller_onepage_save_shipping_address_after>
        </events>

After that I have dispatched the event in OnepageController > SaveShipping method like,

Mage::dispatchEvent('checkout_controller_onepage_save_shipping_address_after', array('page' => $model, 'request' => $this->getRequest()));

This calls the observer when I save my shipping address. But I have no idea how to change the shipping method charge as per the shipping address country.
I have used this thread http://www.opencart60s.com/magento/changing-or-set-shipping-price-on-fly-from-event-or-observer-in-magento.html-13407.html to change the shipping cost. But without success.
I need to change my shipping flat rate as per the country.
I have already created the module which will store the country and shipping rate details. I can get the shipping rate using that module. Now, I just have to update the flat rate value with the custom shipping rate value. Please help me setting my flat rate as per the country code. Thanks..

1

I found the solution from this link here : http://www.magentocommerce.com/boards/viewthread/316685/
In my observer I added below code to change my shipping rates as per country selection in shipping address.

class Pnk_Zones_Model_Observer
{
    public function getShippingMethods($observer)
    {
        $session = Mage::getSingleton('checkout/session');
        $quote=Mage::getSingleton('checkout/session')->getQuote();
        $quoteid=$quote->getId(); 
        if($quoteid) {                    
        try{
                $address=$quote->getShippingAddress();
                if($address->getAddressType()=='shipping'){
            //    echo '<pre>'; print_r($events->getQuoteAddress()->getData()); exit;
                //$price=40;
                $countryId = $address->getCountry();
                //Code to get Shipping rate from my zone module //
                $shippingRateCollection = Mage::getModel('zones/zones')->getCollection();
                $shippingRateCollection->addFieldToSelect('*');
                $shippingRateCollection->getSelect()->where("find_in_set('".$countryId."', `countries`)");


                foreach ($shippingRateCollection as $shipping){
                    $price = $shipping->getShippingRate();
                }
//              echo "Price = ".$price;
                // Find if our shipping has been included.
                $rates = $address->collectShippingRates()
                         ->getGroupedAllShippingRates();

                foreach ($rates as $carrier) {
                    foreach ($carrier as $rate) {
                        // Check with your custom shipping method code
                if($rate->getCode() == 'zones'){
                            $rate->setPrice($price);
                            $rate->save();
               }
                    }
                }
                    $this->collectTotals($quote,$price);
                }            
                $quote->collectTotals();
            } catch (Exception $e) {            
                Mage::logException($e);
                $response['error'] = $e->getMessage();
            }
        }
    }   

    public function collectTotals($quote,$price){
        $quoteid=$quote->getId(); 
        $shippingcode='freeshipping_freeshipping';
        if($quoteid) {                    
                try{
                $quote->setSubtotal(0);
                $quote->setBaseSubtotal(0);
                $quote->setSubtotalWithDiscount(0);
                $quote->setBaseSubtotalWithDiscount(0);
                $quote->setGrandTotal(0);
                $quote->setBaseGrandTotal(0);

                $quote->getShippingAddress()->setShippingMethod($shippingcode)/* ->collectTotals() */->save();
                $quote->save();
                foreach ($quote->getAllAddresses() as $address) {
                    $address->setSubtotal(0);
                    $address->setBaseSubtotal(0);

                    $address->setGrandTotal(0);
                    $address->setBaseGrandTotal(0);

                    $address->collectTotals();

                    $quote->setSubtotal((float) $quote->getSubtotal() + $address->getSubtotal());
                    $quote->setBaseSubtotal((float) $quote->getBaseSubtotal() + $address->getBaseSubtotal());

                    $quote->setSubtotalWithDiscount(
                        (float) $quote->getSubtotalWithDiscount() + $address->getSubtotalWithDiscount()
                    );
                    $quote->setBaseSubtotalWithDiscount(
                        (float) $quote->getBaseSubtotalWithDiscount() + $address->getBaseSubtotalWithDiscount()
                    );

                    $quote->setGrandTotal((float) $quote->getGrandTotal() + $address->getGrandTotal());
                    $quote->setBaseGrandTotal((float) $quote->getBaseGrandTotal() + $address->getBaseGrandTotal());

                    $address->setShippingAmount($price);
                    $address->setBaseShippingAmount($price);
                    $address->save();
                }

                    $response['message'] = 'Succcess';
                } catch (Exception $e) {            
                        Mage::logException($e);
                        $response['error'] = $e->getMessage();
                 }
        }
    }

}
1

Thebod built a module which does this for you, might help: https://github.com/thebod/Thebod_Shippingrates

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.