magentofedexupsusps

Is there a way in magento to set shipping method based on items in order?


Is there a setting(s) in magento where if the order contains certain items, a shipping method will be selected by default?


Solution

  • There is no setting as far as I am familiar with Magento, but you can do it via extending the Mage_Sales_Model_Quote_Address method: requestShippingRates.

    In your etc/config.xml file:

    
    <global>
        <models>
            <modulename>
                <class>Package_Modulename_Model</class>
            </modulename>
            <sales>
                <rewrite>
                    <quote_address>Package_Modulename_Model_Sales_Quote_Address</quote_address>
                </rewrite>
            </sales>
        </models>
    </global>
    

    In your Package_Modulename_Model_Sales_Quote_Address class, copy the method:

    public function requestShippingRates(Mage_Sales_Model_Quote_Item_Abstract $item = null)
    

    and inside, look for the following code:

    
    $found = false;
    if ($result) {
        $shippingRates = $result->getAllRates();
        //Add your code here:
        
        //Get all of the cart items.
        $_cartItems = $request->getAllItems();
        
        //Check if the cart contains items with specific shipping method:
        //Note that you need to implement the _getItemsShippingMethod yourself.
        $_shippingMethod = $this->_getItemsShippingMethod($_cartItems);
        
        foreach ($shippingRates as $shippingRate) {
            //Skip all other methods.
            if ($shippingRate->getCarrier() != $_shippingMethod) {
                continue;
            }