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_Sales_Model_Quote_Address_Total_Shipping extends Mage_Sales_Model_Quote_Address_Total_Abstract
00029 {
00030 public function collect(Mage_Sales_Model_Quote_Address $address)
00031 {
00032 $oldWeight = $address->getWeight();
00033 $address->setWeight(0);
00034 $address->setShippingAmount(0);
00035 $address->setBaseShippingAmount(0);
00036 $address->setFreeMethodWeight(0);
00037
00038 $items = $address->getAllItems();
00039 if (!count($items)) {
00040 return $this;
00041 }
00042
00043 $method = $address->getShippingMethod();
00044 $freeAddress = $address->getFreeShipping();
00045
00046 $addressWeight = $address->getWeight();
00047 $freeMethodWeight = $address->getFreeMethodWeight();
00048
00049 $addressQty = 0;
00050
00051 foreach ($items as $item) {
00052
00053
00054
00055
00056 if ($item->getProduct()->isVirtual()) {
00057 continue;
00058 }
00059
00060
00061
00062 if ($item->getParentItem()) {
00063 continue;
00064 }
00065
00066 if ($item->getHasChildren() && $item->isShipSeparately()) {
00067 foreach ($item->getChildren() as $child) {
00068 if ($child->getProduct()->isVirtual()) {
00069 continue;
00070 }
00071 $addressQty += $item->getQty()*$child->getQty();
00072
00073 if (!$item->getProduct()->getWeightType()) {
00074 $itemWeight = $child->getWeight();
00075 $itemQty = $item->getQty()*$child->getQty();
00076 $rowWeight = $itemWeight*$itemQty;
00077 $addressWeight += $rowWeight;
00078 if ($freeAddress || $child->getFreeShipping()===true) {
00079 $rowWeight = 0;
00080 } elseif (is_numeric($child->getFreeShipping())) {
00081 $freeQty = $child->getFreeShipping();
00082 if ($itemQty>$freeQty) {
00083 $rowWeight = $itemWeight*($itemQty-$freeQty);
00084 }
00085 else {
00086 $rowWeight = 0;
00087 }
00088 }
00089 $freeMethodWeight += $rowWeight;
00090 $item->setRowWeight($rowWeight);
00091 }
00092 }
00093 if ($item->getProduct()->getWeightType()) {
00094 $itemWeight = $item->getWeight();
00095 $rowWeight = $itemWeight*$item->getQty();
00096 $addressWeight+= $rowWeight;
00097 if ($freeAddress || $item->getFreeShipping()===true) {
00098 $rowWeight = 0;
00099 } elseif (is_numeric($item->getFreeShipping())) {
00100 $freeQty = $item->getFreeShipping();
00101 if ($item->getQty()>$freeQty) {
00102 $rowWeight = $itemWeight*($item->getQty()-$freeQty);
00103 }
00104 else {
00105 $rowWeight = 0;
00106 }
00107 }
00108 $freeMethodWeight+= $rowWeight;
00109 $item->setRowWeight($rowWeight);
00110 }
00111 }
00112 else {
00113 if (!$item->getProduct()->isVirtual()) {
00114 $addressQty += $item->getQty();
00115 }
00116 $itemWeight = $item->getWeight();
00117 $rowWeight = $itemWeight*$item->getQty();
00118 $addressWeight+= $rowWeight;
00119 if ($freeAddress || $item->getFreeShipping()===true) {
00120 $rowWeight = 0;
00121 } elseif (is_numeric($item->getFreeShipping())) {
00122 $freeQty = $item->getFreeShipping();
00123 if ($item->getQty()>$freeQty) {
00124 $rowWeight = $itemWeight*($item->getQty()-$freeQty);
00125 }
00126 else {
00127 $rowWeight = 0;
00128 }
00129 }
00130 $freeMethodWeight+= $rowWeight;
00131 $item->setRowWeight($rowWeight);
00132 }
00133 }
00134
00135 if (isset($addressQty)) {
00136 $address->setItemQty($addressQty);
00137 }
00138
00139 $address->setWeight($addressWeight);
00140 $address->setFreeMethodWeight($freeMethodWeight);
00141
00142 $address->collectShippingRates();
00143
00144 $address->setShippingAmount(0);
00145 $address->setBaseShippingAmount(0);
00146
00147 $method = $address->getShippingMethod();
00148
00149 if ($method) {
00150 foreach ($address->getAllShippingRates() as $rate) {
00151 if ($rate->getCode()==$method) {
00152 $amountPrice = $address->getQuote()->getStore()->convertPrice($rate->getPrice(), false);
00153 $address->setShippingAmount($amountPrice);
00154 $address->setBaseShippingAmount($rate->getPrice());
00155 $address->setShippingDescription($rate->getCarrierTitle().' - '.$rate->getMethodTitle());
00156 break;
00157 }
00158 }
00159 }
00160
00161 $address->setGrandTotal($address->getGrandTotal() + $address->getShippingAmount());
00162 $address->setBaseGrandTotal($address->getBaseGrandTotal() + $address->getBaseShippingAmount());
00163 return $this;
00164 }
00165
00166 public function fetch(Mage_Sales_Model_Quote_Address $address)
00167 {
00168 $amount = $address->getShippingAmount();
00169 if ($amount!=0 || $address->getShippingDescription()) {
00170 $address->addTotal(array(
00171 'code'=>$this->getCode(),
00172 'title'=>Mage::helper('sales')->__('Shipping & Handling').' ('.$address->getShippingDescription().')',
00173 'value'=>$address->getShippingAmount()
00174 ));
00175 }
00176 return $this;
00177 }
00178 }