00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 class Mage_Shipping_Model_Carrier_Tablerate
00029 extends Mage_Shipping_Model_Carrier_Abstract
00030 implements Mage_Shipping_Model_Carrier_Interface
00031 {
00032
00033 protected $_code = 'tablerate';
00034 protected $_default_condition_name = 'package_weight';
00035
00036 protected $_conditionNames = array();
00037
00038 public function __construct()
00039 {
00040 parent::__construct();
00041 foreach ($this->getCode('condition_name') as $k=>$v) {
00042 $this->_conditionNames[] = $k;
00043 }
00044 }
00045
00046
00047
00048
00049
00050
00051
00052 public function collectRates(Mage_Shipping_Model_Rate_Request $request)
00053 {
00054 if (!$this->getConfigFlag('active')) {
00055 return false;
00056 }
00057
00058 if (!$request->getConditionName()) {
00059 $request->setConditionName($this->getConfigData('condition_name') ? $this->getConfigData('condition_name') : $this->_default_condition_name);
00060 }
00061
00062 $result = Mage::getModel('shipping/rate_result');
00063 $rate = $this->getRate($request);
00064 if (!empty($rate) && $rate['price'] >= 0) {
00065 $method = Mage::getModel('shipping/rate_result_method');
00066
00067 $method->setCarrier('tablerate');
00068 $method->setCarrierTitle($this->getConfigData('title'));
00069
00070 $method->setMethod('bestway');
00071 $method->setMethodTitle($this->getConfigData('name'));
00072
00073 $shippingPrice = $this->getFinalPriceWithHandlingFee($rate['price']);
00074
00075 $method->setPrice($shippingPrice);
00076 $method->setCost($rate['cost']);
00077
00078 $result->append($method);
00079 }
00080
00081 return $result;
00082 }
00083
00084 public function getRate(Mage_Shipping_Model_Rate_Request $request)
00085 {
00086 return Mage::getResourceModel('shipping/carrier_tablerate')->getRate($request);
00087 }
00088
00089 public function getCode($type, $code='')
00090 {
00091 $codes = array(
00092
00093 'condition_name'=>array(
00094 'package_weight' => Mage::helper('shipping')->__('Weight vs. Destination'),
00095 'package_value' => Mage::helper('shipping')->__('Price vs. Destination'),
00096 'package_qty' => Mage::helper('shipping')->__('# of Items vs. Destination'),
00097 ),
00098
00099 'condition_name_short'=>array(
00100 'package_weight' => Mage::helper('shipping')->__('Weight (and above)'),
00101 'package_value' => Mage::helper('shipping')->__('Order Subtotal (and above)'),
00102 'package_qty' => Mage::helper('shipping')->__('# of Items (and above)'),
00103 ),
00104
00105 );
00106
00107 if (!isset($codes[$type])) {
00108 throw Mage::exception('Mage_Shipping', Mage::helper('shipping')->__('Invalid Table Rate code type: %s', $type));
00109 }
00110
00111 if (''===$code) {
00112 return $codes[$type];
00113 }
00114
00115 if (!isset($codes[$type][$code])) {
00116 throw Mage::exception('Mage_Shipping', Mage::helper('shipping')->__('Invalid Table Rate code for type %s: %s', $type, $code));
00117 }
00118
00119 return $codes[$type][$code];
00120 }
00121
00122
00123
00124
00125
00126
00127 public function getAllowedMethods()
00128 {
00129 return array('bestway'=>$this->getConfigData('name'));
00130 }
00131
00132 }