Magento – Multiple Tablerate (1)

Someone asked me how to do a multiple tablerate, and actually I found it on magento forum. I am gonna list the code that I modified here:

\app\code\core\Mage\Shipping\Model\Carrier\Tablerate.php to \app\code\core\Mage\Shipping\Model\Carrier\Tablerate3.php or Tablerate(n).php

That is screen shot if you just copy my code to your site that will show on your Magento Admin Panel:
multitablerate

Note: This is my modified version, please read comment. In your case, your file should be similar to \app\code\core\Mage\Shipping\Model\Carrier\Tablerate.php, but except you will add a number after tablerate for Tablerate(n).

<?php 

class Mage_Shipping_Model_Carrier_Tablerate3
    extends Mage_Shipping_Model_Carrier_Abstract
    implements Mage_Shipping_Model_Carrier_Interface
{

    protected $_code = 'tablerate3';
    protected $_default_condition_name = 'package_weight';

    protected $_conditionNames = array();

    public function __construct()
    {
        parent::__construct();
        foreach ($this->getCode('condition_name') as $k=>$v) {
            $this->_conditionNames[] = $k;
        }
    }

    /**
     * Enter description here...
     *
     * @param Mage_Shipping_Model_Rate_Request $data
     * @return Mage_Shipping_Model_Rate_Result
     */
    public function collectRates(Mage_Shipping_Model_Rate_Request $request)
    {
        if (!$this->getConfigFlag('active')) {
            return false;
        }

        if (!$request->getConditionName()) {
            $request->setConditionName($this->getConfigData('condition_name') ? $this->getConfigData('condition_name') : $this->_default_condition_name);
        }

        $result = Mage::getModel('shipping/rate_result');
        $rate = $this->getRate($request);
		
/* My Edited Version. */
	$smallShippingOk = $this->checkSmallPacket($request);
/* My Edited Version */
		
        if (!empty($rate) && $rate['price'] >= 0 
     /*My Edited Version */ && $smallShippingOk /*My Edited Version */) {
            $method = Mage::getModel('shipping/rate_result_method');

            $method->setCarrier('tablerate3');
            $method->setCarrierTitle($this->getConfigData('title'));

            $method->setMethod('bestway');
            $method->setMethodTitle($this->getConfigData('name'));

            $shippingPrice = $this->getFinalPriceWithHandlingFee($rate['price']);

            $method->setPrice($shippingPrice);
            $method->setCost($rate['cost']);

            $result->append($method);
        }

        return $result;
    }

    public function getRate(Mage_Shipping_Model_Rate_Request $request)
    {
        return Mage::getResourceModel('shipping/carrier_tablerate3')->getRate($request);
    }

    public function getCode($type, $code='')
    {
        $codes = array(

            'condition_name'=>array(
                'package_weight' => Mage::helper('shipping')->__('Weight vs. Destination'),
                'package_value'  => Mage::helper('shipping')->__('Price vs. Destination'),
                'package_qty'    => Mage::helper('shipping')->__('# of Items vs. Destination'),
            ),

            'condition_name_short'=>array(
                'package_weight' => Mage::helper('shipping')->__('Weight (and above)'),
                'package_value'  => Mage::helper('shipping')->__('Order Subtotal (and above)'),
                'package_qty'    => Mage::helper('shipping')->__('# of Items (and above)'),
            ),

        );

        if (!isset($codes[$type])) {
            throw Mage::exception('Mage_Shipping', Mage::helper('shipping')->__('Invalid Table Rate code type: %s', $type));
        }

        if (''===$code) {
            return $codes[$type];
        }

        if (!isset($codes[$type][$code])) {
            throw Mage::exception('Mage_Shipping', Mage::helper('shipping')->__('Invalid Table Rate code for type %s: %s', $type, $code));
        }
        return $codes[$type][$code];
    }

    /**
     * Get allowed shipping methods
     *
     * @return array
     */
    public function getAllowedMethods()
    {
        return array('bestway'=>$this->getConfigData('name'));
    }
	
/* My Own Function, original file does not have this function */
	protected function checkSmallPacket($request) {
	
		$items = $request->getAllItems();
		foreach ($items as $item) {
			$_product=$item->getProduct();
			$_product->load($_product->getId());
			if ($_product->getData('small_item') == "0") 
				return false;
		}
		return true;
	}
/* My Own Function, original file does not have this function */
}

Post to Twitter Post to Plurk Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to MySpace Post to Ping.fm Post to Reddit Post to StumbleUpon

Leave a Reply