/** * Return table rate array or false by rate request * * @param \Magento\Sales\Model\Quote\Address\RateRequest $request * @return array|bool */ public function getRate(\Magento\Sales\Model\Quote\Address\RateRequest $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 = '0' AND dest_region_id = :region_id AND dest_zip = '*'", "dest_country_id = '0' AND dest_region_id = 0 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 = '*'")) . ')'; $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; }
/** * Prepare and set request to this instance * * @param RateRequest $request * @return $this */ public function setRequest(RateRequest $request) { $this->_request = $request; $r = new \Magento\Framework\Object(); if ($request->getLimitMethod()) { $r->setService($request->getLimitMethod()); } if ($request->getFedexAccount()) { $account = $request->getFedexAccount(); } else { $account = $this->getConfigData('account'); } $r->setAccount($account); if ($request->getFedexDropoff()) { $dropoff = $request->getFedexDropoff(); } else { $dropoff = $this->getConfigData('dropoff'); } $r->setDropoffType($dropoff); if ($request->getFedexPackaging()) { $packaging = $request->getFedexPackaging(); } else { $packaging = $this->getConfigData('packaging'); } $r->setPackaging($packaging); if ($request->getOrigCountry()) { $origCountry = $request->getOrigCountry(); } else { $origCountry = $this->_scopeConfig->getValue(\Magento\Sales\Model\Order\Shipment::XML_PATH_STORE_COUNTRY_ID, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $request->getStoreId()); } $r->setOrigCountry($this->_countryFactory->create()->load($origCountry)->getIso2Code()); if ($request->getOrigPostcode()) { $r->setOrigPostal($request->getOrigPostcode()); } else { $r->setOrigPostal($this->_scopeConfig->getValue(\Magento\Sales\Model\Order\Shipment::XML_PATH_STORE_ZIP, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $request->getStoreId())); } if ($request->getDestCountryId()) { $destCountry = $request->getDestCountryId(); } else { $destCountry = self::USA_COUNTRY_ID; } $r->setDestCountry($this->_countryFactory->create()->load($destCountry)->getIso2Code()); if ($request->getDestPostcode()) { $r->setDestPostal($request->getDestPostcode()); } else { } $weight = $this->getTotalNumOfBoxes($request->getPackageWeight()); $r->setWeight($weight); if ($request->getFreeMethodWeight() != $request->getPackageWeight()) { $r->setFreeMethodWeight($request->getFreeMethodWeight()); } $r->setValue($request->getPackagePhysicalValue()); $r->setValueWithDiscount($request->getPackageValueWithDiscount()); $r->setMeterNumber($this->getConfigData('meter_number')); $r->setKey($this->getConfigData('key')); $r->setPassword($this->getConfigData('password')); $r->setIsReturn($request->getIsReturn()); $r->setBaseSubtotalInclTax($request->getBaseSubtotalInclTax()); $this->setRawRequest($r); return $this; }
/** * @param \Magento\Sales\Model\Quote\Address\RateRequest $request * @return $this|bool|false|\Magento\Framework\Model\AbstractModel */ public function checkAvailableShipCountries(\Magento\Sales\Model\Quote\Address\RateRequest $request) { $speCountriesAllow = $this->getConfigData('sallowspecific'); /* * for specific countries, the flag will be 1 */ if ($speCountriesAllow && $speCountriesAllow == 1) { $showMethod = $this->getConfigData('showmethod'); $availableCountries = array(); if ($this->getConfigData('specificcountry')) { $availableCountries = explode(',', $this->getConfigData('specificcountry')); } if ($availableCountries && in_array($request->getDestCountryId(), $availableCountries)) { return $this; } elseif ($showMethod && (!$availableCountries || $availableCountries && !in_array($request->getDestCountryId(), $availableCountries))) { /** @var Error $error */ $error = $this->_rateErrorFactory->create(); $error->setCarrier($this->_code); $error->setCarrierTitle($this->getConfigData('title')); $errorMsg = $this->getConfigData('specificerrmsg'); $error->setErrorMessage($errorMsg ? $errorMsg : __('Sorry, but we can\'t deliver to the destination country with this shipping module.')); return $error; } else { /* * The admin set not to show the shipping module if the delivery country is not within specific countries */ return false; } } return $this; }
/** * Prepare and set request to this instance * * @param \Magento\Sales\Model\Quote\Address\RateRequest $request * @return $this */ public function setRequest(\Magento\Sales\Model\Quote\Address\RateRequest $request) { $this->_request = $request; $r = new \Magento\Framework\Object(); if ($request->getLimitMethod()) { $r->setService($request->getLimitMethod()); } else { $r->setService('ALL'); } if ($request->getUspsUserid()) { $userId = $request->getUspsUserid(); } else { $userId = $this->getConfigData('userid'); } $r->setUserId($userId); if ($request->getUspsContainer()) { $container = $request->getUspsContainer(); } else { $container = $this->getConfigData('container'); } $r->setContainer($container); if ($request->getUspsSize()) { $size = $request->getUspsSize(); } else { $size = $this->getConfigData('size'); } $r->setSize($size); if ($request->getGirth()) { $girth = $request->getGirth(); } else { $girth = $this->getConfigData('girth'); } $r->setGirth($girth); if ($request->getHeight()) { $height = $request->getHeight(); } else { $height = $this->getConfigData('height'); } $r->setHeight($height); if ($request->getLength()) { $length = $request->getLength(); } else { $length = $this->getConfigData('length'); } $r->setLength($length); if ($request->getWidth()) { $width = $request->getWidth(); } else { $width = $this->getConfigData('width'); } $r->setWidth($width); if ($request->getUspsMachinable()) { $machinable = $request->getUspsMachinable(); } else { $machinable = $this->getConfigData('machinable'); } $r->setMachinable($machinable); if ($request->getOrigPostcode()) { $r->setOrigPostal($request->getOrigPostcode()); } else { $r->setOrigPostal($this->_scopeConfig->getValue(\Magento\Sales\Model\Order\Shipment::XML_PATH_STORE_ZIP, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $request->getStoreId())); } if ($request->getOrigCountryId()) { $r->setOrigCountryId($request->getOrigCountryId()); } else { $r->setOrigCountryId($this->_scopeConfig->getValue(\Magento\Sales\Model\Order\Shipment::XML_PATH_STORE_COUNTRY_ID, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $request->getStoreId())); } if ($request->getDestCountryId()) { $destCountry = $request->getDestCountryId(); } else { $destCountry = self::USA_COUNTRY_ID; } $r->setDestCountryId($destCountry); if (!$this->_isUSCountry($destCountry)) { $r->setDestCountryName($this->_getCountryName($destCountry)); } if ($request->getDestPostcode()) { $r->setDestPostal($request->getDestPostcode()); } $weight = $this->getTotalNumOfBoxes($request->getPackageWeight()); $r->setWeightPounds(floor($weight)); $r->setWeightOunces(round(($weight - floor($weight)) * self::OUNCES_POUND, 1)); if ($request->getFreeMethodWeight() != $request->getPackageWeight()) { $r->setFreeMethodWeight($request->getFreeMethodWeight()); } $r->setValue($request->getPackageValue()); $r->setValueWithDiscount($request->getPackageValueWithDiscount()); $r->setBaseSubtotalInclTax($request->getBaseSubtotalInclTax()); $this->setRawRequest($r); return $this; }
/** * Prepare and set request to this instance * * @param RateRequest $request * @return $this */ public function setRequest(RateRequest $request) { $this->_request = $request; $rowRequest = new \Magento\Framework\Object(); if ($request->getLimitMethod()) { $rowRequest->setAction($this->configHelper->getCode('action', 'single')); $rowRequest->setProduct($request->getLimitMethod()); } else { $rowRequest->setAction($this->configHelper->getCode('action', 'all')); $rowRequest->setProduct('GND' . $this->getConfigData('dest_type')); } if ($request->getUpsPickup()) { $pickup = $request->getUpsPickup(); } else { $pickup = $this->getConfigData('pickup'); } $rowRequest->setPickup($this->configHelper->getCode('pickup', $pickup)); if ($request->getUpsContainer()) { $container = $request->getUpsContainer(); } else { $container = $this->getConfigData('container'); } $rowRequest->setContainer($this->configHelper->getCode('container', $container)); if ($request->getUpsDestType()) { $destType = $request->getUpsDestType(); } else { $destType = $this->getConfigData('dest_type'); } $rowRequest->setDestType($this->configHelper->getCode('dest_type', $destType)); if ($request->getOrigCountry()) { $origCountry = $request->getOrigCountry(); } else { $origCountry = $this->_scopeConfig->getValue(\Magento\Sales\Model\Order\Shipment::XML_PATH_STORE_COUNTRY_ID, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $request->getStoreId()); } $rowRequest->setOrigCountry($this->_countryFactory->create()->load($origCountry)->getIso2Code()); if ($request->getOrigRegionCode()) { $origRegionCode = $request->getOrigRegionCode(); } else { $origRegionCode = $this->_scopeConfig->getValue(\Magento\Sales\Model\Order\Shipment::XML_PATH_STORE_REGION_ID, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $request->getStoreId()); } if (is_numeric($origRegionCode)) { $origRegionCode = $this->_regionFactory->create()->load($origRegionCode)->getCode(); } $rowRequest->setOrigRegionCode($origRegionCode); if ($request->getOrigPostcode()) { $rowRequest->setOrigPostal($request->getOrigPostcode()); } else { $rowRequest->setOrigPostal($this->_scopeConfig->getValue(\Magento\Sales\Model\Order\Shipment::XML_PATH_STORE_ZIP, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $request->getStoreId())); } if ($request->getOrigCity()) { $rowRequest->setOrigCity($request->getOrigCity()); } else { $rowRequest->setOrigCity($this->_scopeConfig->getValue(\Magento\Sales\Model\Order\Shipment::XML_PATH_STORE_CITY, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $request->getStoreId())); } if ($request->getDestCountryId()) { $destCountry = $request->getDestCountryId(); } else { $destCountry = self::USA_COUNTRY_ID; } //for UPS, puero rico state for US will assume as puerto rico country if ($destCountry == self::USA_COUNTRY_ID && ($request->getDestPostcode() == '00912' || $request->getDestRegionCode() == self::PUERTORICO_COUNTRY_ID)) { $destCountry = self::PUERTORICO_COUNTRY_ID; } // For UPS, Guam state of the USA will be represented by Guam country if ($destCountry == self::USA_COUNTRY_ID && $request->getDestRegionCode() == self::GUAM_REGION_CODE) { $destCountry = self::GUAM_COUNTRY_ID; } $rowRequest->setDestCountry($this->_countryFactory->create()->load($destCountry)->getIso2Code()); $rowRequest->setDestRegionCode($request->getDestRegionCode()); if ($request->getDestPostcode()) { $rowRequest->setDestPostal($request->getDestPostcode()); } else { } $weight = $this->getTotalNumOfBoxes($request->getPackageWeight()); $weight = $this->_getCorrectWeight($weight); $rowRequest->setWeight($weight); if ($request->getFreeMethodWeight() != $request->getPackageWeight()) { $rowRequest->setFreeMethodWeight($request->getFreeMethodWeight()); } $rowRequest->setValue($request->getPackageValue()); $rowRequest->setValueWithDiscount($request->getPackageValueWithDiscount()); if ($request->getUpsUnitMeasure()) { $unit = $request->getUpsUnitMeasure(); } else { $unit = $this->getConfigData('unit_of_measure'); } $rowRequest->setUnitMeasure($unit); $rowRequest->setIsReturn($request->getIsReturn()); $rowRequest->setBaseSubtotalInclTax($request->getBaseSubtotalInclTax()); $this->_rawRequest = $rowRequest; return $this; }