1

How to add custom amount to shipping & handling in cart on Magento 1.9? Here I did it but shipping charge was added two times to grand total.

Here is my code:

<?php 
class modelname_Model_Sales_Total_Quote_Ccfee extends Mage_Sales_Model_Quote_Address_Total_Abstract
{
    public function __construct()
    {
        $this->setCode('ccfee');
    }

    public function collect(Mage_Sales_Model_Quote_Address $address)
    {
        parent::collect($address);
         $shipping_charge = $this->calculateCustomTax($address);         
           if ($shipping_charge){                           
                $address->setShippingAmount($shipping_charge);
                $address->setBaseShippingAmount($shipping_charge); 
           }                    
            $address->setGrandTotal($address->getGrandTotal() + $address->getShippingAmount());                       
            return $this;
    }
    protected function calculateCustomTax()
    {
              $shipping_charge = 0;
              $cart = Mage::getModel('checkout/cart')->getQuote()->getAllItems();
                foreach ($cart as $item){
                $options = $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct());                    
                $select_ship[] = $options['info_buyRequest']['select_ship'];
                }           
                $ship = $select_ship;

           if($ship != 0)
           {
                $cart = Mage::getModel('checkout/cart')->getQuote()->getAllVisibleItems();
                foreach ($cart as $item)
                {
                    $options = $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct());
                    $select_ship[] = $options['info_buyRequest']['select_ship'];
                }
                $shipping_charge = $select_ship;
            }           

            else{
                    $cart = Mage::getModel('checkout/cart')->getQuote()->getAllVisibleItems();
                    foreach ($cart as $item)
                    {
                      $options = $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct());                  
                      $select_ship[] = $options['info_buyRequest']['select_ship'];
                    }
                 $shipping_charge = max($select_ship); 
               }               
            return  $shipping_charge;

        }
  • Are you looking to change the shipping amount dynamic. Or want to add 1 more charge same like shipping & handling ? – Ashish Jagnani Aug 27 '16 at 5:51
  • How about your current issue? – Khoa TruongDinh Aug 30 '16 at 6:23
0

I'm not sure about your logic, however, basically, we have two types of quote address: shipping and billing. We don't need to calculate for billing address. So, we need to check the type of address.

    parent::collect($address);
    $quote = $address->getQuote();
    $store = Mage::app()->getStore($quote->getStoreId());
    $this->_calculator->reset($address);

    $items = $this->_getAddressItems($address);
    if (!count($items)) {
        return $this;
    }

There is a good example here.

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.