Tuesday, May 10, 2011

Add additional shipping cost based on weight - Magento module

Description:
In this shipping module i added additional cost to the shipping cost based on cart weight. For example if the shipping cost is 5$ and cart weight is less than 18 pounds means i added additional 7$ to the shipping cost. So the total shipping cost would be 12$. You can also use this code for only particular product or for particular shipping methods.

app/etc/modules/Fixed_Shipping.xml


  
      
          true
          local
      
  

app/code/local/Fixed/Shipping/etc/config.xml

    
        
            0.7.4
        
    




Fixed_Shipping_Model_Rate





app/code/local/Fixed/Shipping/Model/Rate.php
class Fixed_Shipping_Model_Rate extends Mage_Sales_Model_Quote_Address_Rate

{

   public function importShippingRate(Mage_Shipping_Model_Rate_Result_Abstract $rate)

    {

        if ($rate instanceof Mage_Shipping_Model_Rate_Result_Error) {

            $this

                ->setCode($rate->getCarrier().'_error')

                ->setCarrier($rate->getCarrier())

                ->setCarrierTitle($rate->getCarrierTitle())

                ->setErrorMessage($rate->getErrorMessage())

            ;

        } elseif ($rate instanceof Mage_Shipping_Model_Rate_Result_Method) {

   $items = Mage::getSingleton('checkout/cart')->getQuote()->getItemsCollection()->getItems();

   $product = Mage::getModel('catalog/product');

   $total_weight=0; $additional_price = 0;

   foreach ($items as $item) 

   { 

    $product->load($product->getIdBySku($item->getSku()));

    $qty = $item->getQty();

    $weight = $item->getWeight();

    if($product->isConfigurable()){

     $total_weight+=($weight*($qty-1));

    } else {

     $total_weight+=($weight*$qty);

    }

   }
$var1 = Mage::getModel('core/variable')->loadByCode('ship_cond_1') ->getData(html_value);

//Shipping method codes -> flatrate, FEDEXGROUND, GROUNDHOMEDELIVERY, GND, 3DS, 2DA, 1DP, 1DA, 1DM

   if($total_weight <= 18)

   $additional_price = 7;

   else if($total_weight > 18 && $total_weight <= 43 )

   $additional_price = 10;

   else if($total_weight > 43 && $total_weight <= 61 )

   $additional_price = 14;

   else if($total_weight > 61 && $total_weight <= 80 )

   $additional_price = 16;

   else if($total_weight > 80 && $total_weight <= 1000 )

   $additional_price = 22;

   

   $price =$additional_price + $rate->getPrice();

            $this

                ->setCode($rate->getCarrier().'_'.$rate->getMethod())

                ->setCarrier($rate->getCarrier())

                ->setCarrierTitle($rate->getCarrierTitle())

                ->setMethod($rate->getMethod())

                ->setMethodTitle($rate->getMethodTitle())

                ->setMethodDescription($rate->getMethodDescription())

                ->setPrice($price)

            ;

        }

Mage::log($rate->getMethod());

        return $this;

    }


}

No comments:

Post a Comment