public function getNewRate(Mage_Shipping_Model_Rate_Request $request, $zipRangeSet = 0) { $read = $this->_getReadAdapter(); $write = $this->_getWriteAdapter(); $postcode = $request->getDestPostcode(); $table = Mage::getSingleton('core/resource')->getTableName('matrixrate_shipping/matrixrate'); if ($zipRangeSet && is_numeric($postcode)) { # Want to search for postcodes within a range $zipSearchString = ' AND ' . $postcode . ' BETWEEN dest_zip AND dest_zip_to )'; } else { $zipSearchString = $read->quoteInto(" AND ? LIKE dest_zip )", $postcode); } for ($j = 0; $j < 10; $j++) { $select = $read->select()->from($table); switch ($j) { case 0: $select->where($read->quoteInto(" (dest_country_id=? ", $request->getDestCountryId()) . $read->quoteInto(" AND dest_region_id=? ", $request->getDestRegionId()) . $read->quoteInto(" AND STRCMP(LOWER(dest_city),LOWER(?)) = 0 ", $request->getDestCity()) . $zipSearchString); break; case 1: $select->where($read->quoteInto(" (dest_country_id=? ", $request->getDestCountryId()) . $read->quoteInto(" AND dest_region_id=? AND dest_city=''", $request->getDestRegionId()) . $zipSearchString); break; case 2: $select->where($read->quoteInto(" (dest_country_id=? ", $request->getDestCountryId()) . $read->quoteInto(" AND dest_region_id=? ", $request->getDestRegionId()) . $read->quoteInto(" AND STRCMP(LOWER(dest_city),LOWER(?)) = 0 AND dest_zip='')", $request->getDestCity())); break; case 3: $select->where($read->quoteInto(" (dest_country_id=? ", $request->getDestCountryId()) . $read->quoteInto(" AND STRCMP(LOWER(dest_city),LOWER(?)) = 0 AND dest_region_id='0'", $request->getDestCity()) . $zipSearchString); break; case 4: $select->where($read->quoteInto(" (dest_country_id=? ", $request->getDestCountryId()) . $read->quoteInto(" AND STRCMP(LOWER(dest_city),LOWER(?)) = 0 AND dest_region_id='0' AND dest_zip='') ", $request->getDestCity())); break; case 5: $select->where($read->quoteInto(" (dest_country_id=? AND dest_region_id='0' AND dest_city='' ", $request->getDestCountryId()) . $zipSearchString); break; case 6: $select->where($read->quoteInto(" (dest_country_id=? ", $request->getDestCountryId()) . $read->quoteInto(" AND dest_region_id=? AND dest_city='' AND dest_zip='') ", $request->getDestRegionId())); break; case 7: $select->where($read->quoteInto(" (dest_country_id=? AND dest_region_id='0' AND dest_city='' AND dest_zip='') ", $request->getDestCountryId())); break; case 8: $select->where(" (dest_country_id='0' AND dest_region_id='0'" . $zipSearchString); break; case 9: $select->where(" (dest_country_id='0' AND dest_region_id='0' AND dest_zip='')"); break; } if (is_array($request->getMRConditionName())) { $i = 0; foreach ($request->getMRConditionName() as $conditionName) { if ($i == 0) { $select->where('condition_name=?', $conditionName); } else { $select->orWhere('condition_name=?', $conditionName); } $select->where('condition_from_value<=?', $request->getData($conditionName)); $i++; } } else { $select->where('condition_name=?', $request->getMRConditionName()); $select->where('condition_from_value<=?', $request->getData($request->getMRConditionName())); $select->where('condition_to_value>=?', $request->getData($request->getMRConditionName())); } $select->where('website_id=?', $request->getWebsiteId()); if ($filter = $request->getData('filter')) { $select->where('filter IN (?)', $filter); } $select->order('dest_country_id DESC'); $select->order('dest_region_id DESC'); $select->order('dest_zip DESC'); $select->order('condition_from_value DESC'); /* pdo has an issue. we cannot use bind */ $newdata = array(); $row = $read->fetchAll($select); if (!empty($row)) { // have found a result or found nothing and at end of list! foreach ($row as $data) { $newdata[] = $data; } break; } } return $newdata; }
/** * 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; } // exclude Virtual products price from Package value if pre-configured if (!$this->getConfigFlag('include_virtual_price') && $request->getAllItems()) { foreach ($request->getAllItems() as $item) { if ($item->getParentItem()) { continue; } if ($item->getHasChildren() && $item->isShipSeparately()) { foreach ($item->getChildren() as $child) { if ($child->getProduct()->isVirtual() || $item->getProductType() == 'downloadable') { $request->setPackageValue($request->getPackageValue() - $child->getBaseRowTotal()); } } } elseif ($item->getProduct()->isVirtual() || $item->getProductType() == 'downloadable') { $request->setPackageValue($request->getPackageValue() - $item->getBaseRowTotal()); } } } // Free shipping by qty $freeQty = 0; if ($request->getAllItems()) { foreach ($request->getAllItems() as $item) { if ($item->getProduct()->isVirtual() || $item->getParentItem()) { continue; } if ($item->getHasChildren() && $item->isShipSeparately()) { foreach ($item->getChildren() as $child) { if ($child->getFreeShipping() && !$child->getProduct()->isVirtual()) { $freeQty += $item->getQty() * ($child->getQty() - (is_numeric($child->getFreeShipping()) ? $child->getFreeShipping() : 0)); } } } elseif ($item->getFreeShipping()) { $freeQty += $item->getQty() - (is_numeric($item->getFreeShipping()) ? $item->getFreeShipping() : 0); } } } if (!$request->getMRConditionName()) { $request->setMRConditionName($this->getConfigData('condition_name') ? $this->getConfigData('condition_name') : $this->_default_condition_name); } // Package weight and qty free shipping $oldWeight = $request->getPackageWeight(); $oldQty = $request->getPackageQty(); if ($this->getConfigData('allow_free_shipping_promotions') && !$this->getConfigData('include_free_ship_items')) { $request->setPackageWeight($request->getFreeMethodWeight()); $request->setPackageQty($oldQty - $freeQty); } $result = Mage::getModel('shipping/rate_result'); $ratearray = $this->getRate($request); $freeShipping = false; if (is_numeric($this->getConfigData('free_shipping_threshold')) && $this->getConfigData('free_shipping_threshold') > 0 && $request->getPackageValue() > $this->getConfigData('free_shipping_threshold')) { $freeShipping = true; } if ($this->getConfigData('allow_free_shipping_promotions') && ($request->getFreeShipping() === true || $request->getPackageQty() == $this->getFreeBoxes())) { $freeShipping = true; } if ($freeShipping) { $method = Mage::getModel('shipping/rate_result_method'); $method->setCarrier('matrixrate'); $method->setCarrierTitle($this->getConfigData('title')); $method->setMethod('matrixrate_free'); $method->setPrice('0.00'); $method->setMethodTitle($this->getConfigData('free_method_text')); $result->append($method); if ($this->getConfigData('show_only_free')) { return $result; } } foreach ($ratearray as $rate) { if (!empty($rate) && $rate['price'] >= 0) { $method = Mage::getModel('shipping/rate_result_method'); $method->setCarrier('matrixrate'); $method->setCarrierTitle($this->getConfigData('title')); $method->setMethod('matrixrate_' . $rate['pk']); $method->setMethodTitle(Mage::helper('matrixrate')->__($rate['delivery_type'])); $shippingPrice = $this->getFinalPriceWithHandlingFee($rate['price']); $method->setCost($rate['cost']); $method->setDeliveryType($rate['delivery_type']); $method->setPrice($shippingPrice); $result->append($method); } } return $result; }