public function getRate(Mage_Shipping_Model_Rate_Request $request) { $read = $this->_getReadAdapter(); $write = $this->_getWriteAdapter(); $select = $read->select()->from($this->getMainTable()); /* //commented out code since we don't want to get state by using zip code if (!$request->getDestCountryId() && !$request->getDestRegionId()) { // assuming that request is coming from shopping cart // for shipping prices pre-estimation... // also probably it will be required to move this part to // Sales/Model/Quote/Address.php ! $selectCountry = $read->select()->from(Mage::getSingleton('core/resource')->getTableName('usa/postcode'), array('country_id', 'region_id')); $selectCountry->where('postcode=?', $request->getDestPostcode()); $selectCountry->limit(1); $countryRegion = $read->fetchRow($selectCountry); $region = $read->quote($countryRegion['region_id']); $country = $read->quote($countryRegion['country_id']); } else { $region = $read->quote($request->getDestRegionId()); $country = $read->quote($request->getDestCountryId()); } */ $region = $read->quote($request->getDestRegionId()); $country = $read->quote($request->getDestCountryId()); $zip = $read->quote($request->getDestPostcode()); $select->where("(dest_zip={$zip})\n OR (dest_region_id={$region} AND dest_zip='')\n OR (dest_country_id={$country} AND dest_region_id='0' AND dest_zip='')\n OR (dest_country_id='0' AND dest_region_id='0' AND dest_zip='')"); if (is_array($request->getConditionName())) { $i = 0; foreach ($request->getConditionName() as $conditionName) { if ($i == 0) { $select->where('condition_name=?', $conditionName); } else { $select->orWhere('condition_name=?', $conditionName); } $select->where('condition_value<=?', $request->getData($conditionName)); $i++; } } else { $select->where('condition_name=?', $request->getConditionName()); $select->where('condition_value<=?', $request->getData($request->getConditionName())); } $select->where('website_id=?', $request->getWebsiteId()); $select->order('condition_value DESC')->limit(1); $row = $read->fetchRow($select); return $row; }
public function getNewRate(Mage_Shipping_Model_Rate_Request $request, $zipRangeSet = 0) { $newdata = array(); $collection = Mage::getResourceModel('matrixrate_shipping/carrier_matrixrate_collection'); $collection->setConditionFilter($request->getConditionName())->setWebsiteFilter($request->getWebsiteId()); $collection->getSelect()->reset(Zend_Db_Select::COLUMNS)->columns(array('website_id', 'zone', 'condition_name', 'condition_from_value', 'condition_to_value', 'shipping_charge')); $collection->getSelect()->join(array('zones' => 'shipping_zones'), "zones.zone=s.zone and zones.delivery_type='standard' and zones.country_code='" . $request->getDestCountryId() . "' AND (condition_from_value<='" . $request->getData($request->getConditionName()) . "') AND (condition_to_value>='" . $request->getData($request->getConditionName()) . "')", array('delivery_type', 'shipping_provider')); //print $collection->getSelect();die; if ($collection->count()) { foreach ($collection->getData() as $data) { $newdata[] = $data; } } return $newdata; }
public function collectRates(Mage_Shipping_Model_Rate_Request $request) { if (!$this->getConfigFlag('active')) { return false; } $freeBoxes = 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()) { $freeBoxes += $item->getQty() * $child->getQty(); } } } elseif ($item->getFreeShipping()) { $freeBoxes += $item->getQty(); } } } $this->setFreeBoxes($freeBoxes); $result = Mage::getModel('shipping/rate_result'); if ($this->getConfigData('type') == 'O') { // per order $shippingPrice = $this->getConfigData('price'); } elseif ($this->getConfigData('type') == 'I') { // per item $shippingPrice = $request->getPackageQty() * $this->getConfigData('price') - $this->getFreeBoxes() * $this->getConfigData('price'); } else { $shippingPrice = false; } if (Mage_Shipping_Model_Carrier_Abstract::HANDLING_TYPE_PERCENT == $this->getConfigData('shipper_type')) { $shippingPrice = $request->getData('base_subtotal_incl_tax') * $shippingPrice / 100; } $shippingPrice = $this->getFinalPriceWithHandlingFee($shippingPrice); if ($shippingPrice !== false) { $method = Mage::getModel('shipping/rate_result_method'); $method->setCarrier('flatrate'); $method->setCarrierTitle($this->getConfigData('title')); $method->setMethod('flatrate'); $method->setMethodTitle($this->getConfigData('name')); if ($request->getFreeShipping() === true || $request->getPackageQty() == $this->getFreeBoxes()) { $shippingPrice = '0.00'; } $method->setPrice($shippingPrice); $method->setCost($shippingPrice); $result->append($method); } return $result; }
public function getRate(Mage_Shipping_Model_Rate_Request $request) { $adapter = $this->_getReadAdapter(); $bind = array(':website_id' => (int) $request->getWebsiteId(), ':country_id' => $request->getDestCountryId(), ':region_id' => (int) $request->getDestRegionId(), ':postcode' => $request->getDestPostcode()); $select = $adapter->select()->from($this->getMainTable())->where('website_id = :website_id')->order(array('dest_country_id DESC', 'dest_region_id DESC', 'dest_zip DESC'))->limit(1); // render destination condition $orWhere = '(' . implode(') OR (', array("dest_country_id = :country_id AND dest_region_id = :region_id AND dest_zip = :postcode", "dest_country_id = :country_id AND dest_region_id = :region_id AND dest_zip = ''", "dest_country_id = :country_id AND dest_region_id = :region_id AND dest_zip = '*'", "dest_country_id = :country_id AND dest_region_id = 0 AND dest_zip = ''", "dest_country_id = :country_id AND dest_region_id = 0 AND dest_zip = :postcode", "dest_country_id = :country_id AND dest_region_id = 0 AND dest_zip = '*'", "dest_country_id = '0' AND dest_region_id = 0 AND dest_zip = '*'")) . ')'; $select->where($orWhere); // render condition by condition name if (is_array($request->getConditionName())) { $orWhere = array(); $i = 0; foreach ($request->getConditionName() as $conditionName) { $bindNameKey = sprintf(':condition_name_%d', $i); $bindValueKey = sprintf(':condition_value_%d', $i); $orWhere[] = "(condition_name = {$bindNameKey} AND condition_value <= {$bindValueKey})"; $bind[$bindNameKey] = $conditionName; $bind[$bindValueKey] = $request->getData($conditionName); $i++; } if ($orWhere) { $select->where(implode(' OR ', $orWhere)); } } else { $bind[':condition_name'] = $request->getConditionName(); $bind[':condition_value'] = $request->getData($request->getConditionName()); $select->where('condition_name = :condition_name'); $select->where('condition_value <= :condition_value'); } $result = $adapter->fetchRow($select, $bind); // normalize destination zip code if ($result && $result['dest_zip'] == '*') { $result['dest_zip'] = ''; } return $result; }
public function collectRates(Mage_Shipping_Model_Rate_Request $request) { if (!Mage::getStoreConfig('carriers/' . $this->_code . '/active')) { return false; } $lei = Mage::helper('urgent')->isLei($request->getData('package_currency')); if (!$lei) { return false; } $helper = Mage::helper('urgent'); if ($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()) { $request->setPackageValue($request->getPackageValue() - $child->getBaseRowTotal()); } } } elseif ($item->getProduct()->isVirtual()) { $request->setPackageValue($request->getPackageValue() - $item->getBaseRowTotal()); } } } $valuare = $request->getPackageValue() * $lei; if ($helper->getAfisare() == 2) { $methodUrgent = $this->_calculateTotal($request, $valuare, true); $result = Mage::getModel('shipping/rate_result'); $result->append($methodUrgent); return $result; } else { $methodUrgent = $this->_calculateTotal($request, $valuare, false); $result = Mage::getModel('shipping/rate_result'); $result->append($methodUrgent); return $result; } }
public function getRate(Mage_Shipping_Model_Rate_Request $request) { $read = $this->_getReadAdapter(); $postcode = $request->getDestPostcode(); $table = $this->getMainTable(); $storeId = $request->getStoreId(); $insuranceStep = (double) Mage::getStoreConfig('carriers/eparcel/insurance_step', $storeId); $insuranceCostPerStep = (double) Mage::getStoreConfig('carriers/eparcel/insurance_cost_per_step', $storeId); $signatureRequired = Mage::getStoreConfigFlag('carriers/eparcel/signature_required', $storeId); if ($signatureRequired) { $signatureCost = (double) Mage::getStoreConfig('carriers/eparcel/signature_cost', $storeId); } else { $signatureCost = 0; } for ($j = 0; $j < 5; $j++) { $select = $read->select()->from($table); // Support for Multi Warehouse Extension. if ($request->getWarehouseId() > 0) { $select->where('stock_id = ?', $request->getWarehouseId()); } switch ($j) { case 0: $select->where($read->quoteInto(" (dest_country_id=? ", $request->getDestCountryId()) . $read->quoteInto(" AND dest_region_id=? ", $request->getDestRegionId()) . $read->quoteInto(" AND dest_zip=?) ", $postcode)); break; case 1: $select->where($read->quoteInto(" (dest_country_id=? ", $request->getDestCountryId()) . $read->quoteInto(" AND dest_region_id=? AND dest_zip='0000') ", $request->getDestRegionId())); break; case 2: $select->where($read->quoteInto(" (dest_country_id=? AND dest_region_id='0' AND dest_zip='0000') ", $request->getDestCountryId())); break; case 3: $select->where($read->quoteInto(" (dest_country_id=? AND dest_region_id='0' ", $request->getDestCountryId()) . $read->quoteInto(" AND dest_zip=?) ", $postcode)); break; case 4: $select->where(" (dest_country_id='0' AND dest_region_id='0' AND dest_zip='0000')"); break; } if (is_array($request->getConditionName())) { $i = 0; foreach ($request->getConditionName() as $conditionName) { if ($i == 0) { $select->where('condition_name=?', $conditionName); } else { $select->orWhere('condition_name=?', $conditionName); } $select->where('condition_from_value<=?', $request->getData($conditionName)); $select->where('condition_to_value>=?', $request->getData($conditionName)); $i++; } } else { $select->where('condition_name=?', $request->getConditionName()); $select->where('condition_from_value<=?', $request->getData($request->getConditionName())); $select->where('condition_to_value>=?', $request->getData($request->getConditionName())); } $select->where('website_id=?', $request->getWebsiteId()); $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) && $j < 5) { // have found a result or found nothing and at end of list! foreach ($row as $data) { try { $price = (double) $data['price']; // add per-Kg cost $conditionValue = (double) $request->getData($request->getConditionName()); $price += (double) $data['price_per_kg'] * $conditionValue; // add signature cost $price += $signatureCost; // add version without insurance $data['price'] = (string) $price; $newdata[] = $data; if (Mage::getStoreConfig('carriers/eparcel/insurance_enable', $storeId)) { // add version with insurance // work out how many insurance 'steps' we have $steps = ceil($request->getPackageValue() / $insuranceStep); // add on number of 'steps' multiplied by the // insurance cost per step $insuranceCost = $insuranceCostPerStep * $steps; $price += $insuranceCost; $data['price'] = (string) $price; $data['delivery_type'] .= " with TransitCover"; $newdata[] = $data; } } catch (Exception $e) { Mage::log($e->getMessage()); } } break; } } return $newdata; }
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; }
/** * Return table rate array or false by rate request * * @param Mage_Shipping_Model_Rate_Request $request * * @return array|false */ public function getRate(Mage_Shipping_Model_Rate_Request $request) { $adapter = $this->_getReadAdapter(); $bind = array(':website_id' => (int) $request->getWebsiteId(), ':country_id' => $request->getDestCountryId(), ':region_id' => $request->getDestRegionId(), ':postcode' => $request->getDestPostcode(), ':weight' => (double) $request->getPackageWeight(), ':price' => (double) $request->getData('zitec_table_price')); $select = $adapter->select()->from($this->getMainTable())->where('website_id=:website_id')->order(array('dest_country_id DESC', 'dest_region_id DESC', 'dest_zip DESC', 'method DESC', 'price_vs_dest DESC', 'weight DESC')); // render destination condition $orWhere = '(' . implode(') OR (', array("dest_country_id = :country_id AND dest_region_id = :region_id AND dest_zip = :postcode", "dest_country_id = :country_id AND dest_region_id = :region_id AND dest_zip = ''", "dest_country_id = :country_id AND dest_region_id = 0 AND dest_zip = ''", "dest_country_id = :country_id AND dest_region_id = 0 AND dest_zip = :postcode", "dest_country_id = '0' AND dest_region_id = 0 AND dest_zip = ''")) . ')'; $select->where($orWhere); $select->where('((weight <= :weight and price_vs_dest = 0) or (weight <= :price and price_vs_dest = 1))'); $rates = $adapter->fetchAll($select, $bind); if (empty($rates)) { $rates = Mage::getModel('zitec_dpd/config_source_service')->getDefaultShipingRates(); } return $rates; }