/**
  * @param RateRequest $request
  * @return \Magento\Shipping\Model\Rate\Result
  */
 public function collectRates(RateRequest $request)
 {
     if (!$this->getConfigFlag('active')) {
         return false;
     }
     /** @var Result $result */
     $result = $this->_rateResultFactory->create();
     $packageValue = $request->getBaseCurrency()->convert($request->getPackageValue(), $request->getPackageCurrency());
     for ($i = 0; $i <= 5; $i++) {
         if ($this->getConfigData('type' . $i) == 'O') {
             // per order
             $shippingPrice = $this->getConfigData('price' . $i);
         } elseif ($this->getConfigData('type' . $i) == 'I') {
             // per item
             $shippingPrice = $request->getPackageQty() * $this->getConfigData('price' . $i) - $this->getFreeBoxes() * $this->getConfigData('price' . $i);
         } else {
             $shippingPrice = $this->getConfigData('price' . $i);
         }
         $shippingName = $this->getConfigData('name' . $i);
         if ($shippingName != "" && ($packageValue >= $this->getConfigData('min_shipping' . $i) && $packageValue <= $this->getConfigData('max_shipping' . $i)) or $shippingName != "" && $this->getConfigData('max_shipping' . $i) == "") {
             /** @var \Magento\Quote\Model\Quote\Address\RateResult\Method $method */
             $method = $this->_rateMethodFactory->create();
             $method->setCarrier('msmultiflat');
             $method->setCarrierTitle($this->getConfigData('title'));
             $method->setMethod($this->getConfigData('name' . $i));
             $method->setMethodTitle($this->getConfigData('name' . $i));
             $method->setMethodDetails($this->getConfigData('details' . $i));
             $method->setMethodDescription($this->getConfigData('details' . $i));
             $method->setPrice($shippingPrice);
             $method->setCost($shippingPrice);
             $result->append($method);
         }
     }
     return $result;
 }
 /**
  * Collect and get rates for storefront
  *
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  * @param RateRequest $request
  * @return DataObject|bool|null
  * @api
  */
 public function collectRates(RateRequest $request)
 {
     /**
      * Make sure that Shipping method is enabled
      */
     if (!$this->isActive()) {
         return false;
     }
     /** @var \Magento\Shipping\Model\Rate\Result $result */
     $result = $this->_rateResultFactory->create();
     $shippingPrice = $this->getConfigData('price');
     $method = $this->_rateMethodFactory->create();
     /**
      * Set carrier's method data
      */
     $method->setCarrier($this->getCarrierCode());
     $method->setCarrierTitle($this->getConfigData('title'));
     /**
      * Displayed as shipping method under Carrier
      */
     $method->setMethod($this->getCarrierCode());
     $method->setMethodTitle($this->getConfigData('name'));
     $method->setPrice($shippingPrice);
     $method->setCost($shippingPrice);
     $result->append($method);
     return $result;
 }
示例#3
0
 /**
  * @param RateRequest $request
  * @return \Magento\Shipping\Model\Rate\Result
  * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  */
 public function collectRates(RateRequest $request)
 {
     if (!$this->getConfigFlag('active')) {
         return false;
     }
     /** @var \Magento\Shipping\Model\Rate\Result $result */
     $result = $this->_rateResultFactory->create();
     $shippingPrice = $this->getConfigData('price');
     $method = $this->_rateMethodFactory->create();
     $method->setCarrier($this->_code);
     $method->setCarrierTitle($this->getConfigData('title'));
     $method->setMethod($this->_code);
     $method->setMethodTitle($this->getConfigData('name'));
     $method->setPrice($shippingPrice);
     $method->setCost($shippingPrice);
     $result->append($method);
     return $result;
 }
示例#4
0
 /**
  * @param \Magento\Framework\Object $request
  * @return \Magento\Shipping\Model\Rate\Result
  * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  */
 public function collectRates(\Magento\Framework\Object $request)
 {
     if (!$this->getConfigFlag('active')) {
         return false;
     }
     /** @var \Magento\Shipping\Model\Rate\Result $result */
     $result = $this->_rateResultFactory->create();
     if (!empty($rate)) {
         /** @var \Magento\Quote\Model\Quote\Address\RateResult\Method $method */
         $method = $this->_rateMethodFactory->create();
         $method->setCarrier('pickup');
         $method->setCarrierTitle($this->getConfigData('title'));
         $method->setMethod('store');
         $method->setMethodTitle(__('Store Pickup'));
         $method->setPrice(0);
         $method->setCost(0);
         $result->append($method);
     }
     return $result;
 }
 /**
  * @param RateRequest $request
  * @return \Magento\Shipping\Model\Rate\Result|bool
  */
 public function collectRates(RateRequest $request)
 {
     if (!$this->getConfigFlag('active')) {
         return false;
     }
     #print_r(get_class_methods($request));
     $info = array('city' => $request->getDestCity(), 'company' => $request->getDestCompany(), 'country_id' => $request->getDestCountryId(), 'firstname' => $request->getDestFirstname(), 'lastname' => $request->getDestLastname(), 'postcode' => $request->getDestPostcode(), 'region' => $request->getDestRegion(), 'region_code' => $request->getDestRegionCode(), 'region_id' => $request->getDestRegionId(), 'street' => $request->getDestStreet(), 'telphone' => $request->getDestTelphone());
     #$this->_logger->addDebug('$info');
     $this->_logger->log(100, print_r($info, true));
     $quoteArr = null;
     foreach ($request->getAllItems() as $item) {
         $quoteArr['item_id'] = $item->getitem_id();
         $quoteArr['quote_id'] = $item->getquote_id();
         $quoteArr['product_id'] = $item->getproduct_id();
         $quoteArr['parent_item_id'] = $item->getparent_item_id();
         $quoteArr['is_virtual'] = $item->getis_virtual();
         $quoteArr['sku'] = $item->getsku();
         $quoteArr['name'] = $item->getname();
         $quoteArr['qty'] = $item->getqty();
         $quoteArr['weight'] = $item->getweight();
         $quoteArr['price'] = $item->getprice();
         $quoteArr['base_price'] = $item->getbase_price();
         $quoteArr['custom_price'] = $item->getcustom_price();
         $quoteArr['discount_percent'] = $item->getdiscount_percent();
         $quoteArr['discount_amount'] = $item->getdiscount_amount();
     }
     #$this->_logger->addDebug('测试');
     $this->_logger->log(100, print_r($quoteArr, true));
     /** @var \Magento\Shipping\Model\Rate\Result $result */
     $result = $this->_rateResultFactory->create();
     $shippingPrice = $this->getConfigData('price');
     $method = $this->_rateMethodFactory->create();
     $method->setCarrier($this->_code);
     $method->setCarrierTitle($this->getConfigData('title'));
     $method->setMethod($this->_code);
     $method->setMethodTitle($this->getConfigData('name'));
     $method->setPrice($shippingPrice);
     $method->setCost($shippingPrice);
     $result->append($method);
     return $result;
 }
 /**
  * FreeShipping Rates Collector
  *
  * @param RateRequest $request
  * @return \Magento\Shipping\Model\Rate\Result|bool
  */
 public function collectRates(RateRequest $request)
 {
     if (!$this->getConfigFlag('active')) {
         return false;
     }
     /** @var \Magento\Shipping\Model\Rate\Result $result */
     $result = $this->_rateResultFactory->create();
     $this->_updateFreeMethodQuote($request);
     if ($request->getFreeShipping() || $request->getBaseSubtotalInclTax() >= $this->getConfigData('free_shipping_subtotal')) {
         /** @var \Magento\Quote\Model\Quote\Address\RateResult\Method $method */
         $method = $this->_rateMethodFactory->create();
         $method->setCarrier('freeshipping');
         $method->setCarrierTitle($this->getConfigData('title'));
         $method->setMethod('freeshipping');
         $method->setMethodTitle($this->getConfigData('name'));
         $method->setPrice('0.00');
         $method->setCost('0.00');
         $result->append($method);
     }
     return $result;
 }
 /**
  * @param int|float $shippingPrice
  * @return \Magento\Quote\Model\Quote\Address\RateResult\Method
  */
 private function createResultMethod($shippingPrice)
 {
     /** @var \Magento\Quote\Model\Quote\Address\RateResult\Method $method */
     $method = $this->_rateMethodFactory->create();
     $method->setCarrier('flatrate');
     $method->setCarrierTitle($this->getConfigData('title'));
     $method->setMethod('flatrate');
     $method->setMethodTitle($this->getConfigData('name'));
     $method->setPrice($shippingPrice);
     $method->setCost($shippingPrice);
     return $method;
 }
示例#8
0
 /**
  * @param \Magento\Framework\Object $request
  * @return Result|bool
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function collectRates(\Magento\Framework\Object $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);
     /** @var Result $result */
     $result = $this->_rateResultFactory->create();
     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;
     }
     $shippingPrice = $this->getFinalPriceWithHandlingFee($shippingPrice);
     if ($shippingPrice !== false) {
         /** @var \Magento\Quote\Model\Quote\Address\RateResult\Method $method */
         $method = $this->_rateMethodFactory->create();
         $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;
 }
 /**
  * @param string $methodId
  * @param \stdClass $method
  * @return \Magento\Quote\Model\Quote\Address\RateResult\Method
  */
 protected function createMethod($methodId, $method)
 {
     /** @var \Magento\Quote\Model\Quote\Address\RateResult\Method $rate */
     $rate = $this->rateMethodFactory->create();
     $rate->setCarrier($this->_code);
     $rate->setCarrierTitle($this->getConfigData('title'));
     $rate->setMethod($methodId);
     $title = isset($method->title) ? $method->title : 'N/A';
     $rate->setMethodTitle($title);
     $description = isset($method->description) ? $method->description : null;
     $rate->setMethodDescription($description);
     $rate->setCost($method->price);
     $rate->setPrice($method->price);
     return $rate;
 }
示例#10
0
 /**
  * Build Rate based on location data
  *
  * @param string $locationId Shipping method(location) identifier
  * @param array $location Location info
  * @return \Magento\Quote\Model\Quote\Address\RateResult\Method
  */
 protected function buildRateForLocation($locationId, array $location)
 {
     /** @var \Magento\Quote\Model\Quote\Address\RateResult\Method $rateResultMethod */
     $rateResultMethod = $this->rateMethodFactory->create();
     $rateResultMethod->setCarrier($this->getCarrierCode());
     $carrierTitle = $this->getConfigData('title');
     $rateResultMethod->setCarrierTitle($carrierTitle);
     /**
      * Displayed as shipping method under Carrier(In-Store Pickup)
      */
     $methodTitle = sprintf('%s, %s, %s, %s (%s)', $location['street'], $location['city'], $location['country_id'], $location['postcode'], $location['message']);
     $rateResultMethod->setMethodTitle($methodTitle);
     $rateResultMethod->setMethod($locationId);
     $rateResultMethod->setPrice(0);
     $rateResultMethod->setCost(0);
     return $rateResultMethod;
 }
示例#11
0
 /**
  *
  * @param RateRequest $request
  * @return boolean
  */
 public function collectRates(RateRequest $request)
 {
     if (!$this->isActive()) {
         return false;
     }
     $config = $this->getConfig($request);
     // $stopToFirstMatch = (boolean)$this->getConfigData('stop_to_first_match');
     /** @var \Magento\Shipping\Model\Rate\Result $result */
     $result = $this->rateFactory->create();
     if (!isset($config) || !is_array($config)) {
         $this->_logger->debug("OWEBIA SHIPPING : Invalid config");
         return false;
     }
     foreach ($config as $methodId => $method) {
         if (isset($method->error)) {
             $this->appendError($result, $methodId, $method, $method->error);
         } elseif (!isset($method->price)) {
             $this->appendError($result, $methodId, $method, "Invalid price: null");
         } elseif ($method->price === false) {
             $this->appendError($result, $methodId, $method, "Invalid price: false");
         } elseif (isset($method->enabled) && !$method->enabled) {
             // $this->appendError($result, $methodId, $method, "Method disabled");
         } elseif (isset($method->debug)) {
             $this->appendError($result, $methodId, $method, $method->debug);
         } else {
             /** @var \Magento\Quote\Model\Quote\Address\RateResult\Method $rate */
             $rate = $this->rateMethodFactory->create();
             $rate->setCarrier($this->_code);
             $rate->setCarrierTitle($this->getConfigData('title'));
             $rate->setMethod($methodId);
             $title = isset($method->title) ? $method->title : 'N/A';
             $rate->setMethodTitle($title);
             $description = isset($method->description) ? $method->description : null;
             $rate->setMethodDescription($description);
             $rate->setCost($method->price);
             $rate->setPrice($method->price);
             $result->append($rate);
             // if ($stopToFirstMatch) break;
         }
     }
     return $result;
 }
 /**
  * @param $methodId
  *
  * @return \Magento\Quote\Model\Quote\Address\RateResult\Error|\Magento\Quote\Model\Quote\Address\RateResult\Method
  */
 protected function _getRate($methodId)
 {
     if ($methodId == self::INVALID_METHOD) {
         return $this->_getErrorRate();
     }
     /** @var \Magento\Quote\Model\Quote\Address\RateResult\Method $method */
     $rate = $this->_rateMethodFactory->create();
     $dataMethod = $this->getDataMethod($methodId);
     $rate->setCarrier($this->_code);
     $estimatedDate = $this->_getEstimatedDate($dataMethod->getEstimatedDeliveryTime());
     $rate->setCarrierTitle($this->getConfigData('title'));
     $rate->setMethod($methodId);
     $rate->setMethodTitle($dataMethod->getName() . ' ' . __('(estimated date %1)', $estimatedDate));
     if (!empty($this->_request) && $this->_request->getFreeShipping()) {
         $rate->setPrice(0.0);
     } else {
         $rate->setPrice($dataMethod->getCost());
     }
     $rate->setCost($dataMethod->getListCost());
     return $rate;
 }
示例#13
0
 /**
  * @param RateRequest $request
  * @return \Magento\Shipping\Model\Rate\Result
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 public function collectRates(RateRequest $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()) {
                         $request->setPackageValue($request->getPackageValue() - $child->getBaseRowTotal());
                     }
                 }
             } elseif ($item->getProduct()->isVirtual()) {
                 $request->setPackageValue($request->getPackageValue() - $item->getBaseRowTotal());
             }
         }
     }
     // Free shipping by qty
     $freeQty = 0;
     if ($request->getAllItems()) {
         $freePackageValue = 0;
         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()) {
                         $freeShipping = is_numeric($child->getFreeShipping()) ? $child->getFreeShipping() : 0;
                         $freeQty += $item->getQty() * ($child->getQty() - $freeShipping);
                     }
                 }
             } elseif ($item->getFreeShipping()) {
                 $freeShipping = is_numeric($item->getFreeShipping()) ? $item->getFreeShipping() : 0;
                 $freeQty += $item->getQty() - $freeShipping;
                 $freePackageValue += $item->getBaseRowTotal();
             }
         }
         $oldValue = $request->getPackageValue();
         $request->setPackageValue($oldValue - $freePackageValue);
     }
     if (!$request->getConditionName()) {
         $conditionName = $this->getConfigData('condition_name');
         $request->setConditionName($conditionName ? $conditionName : $this->_defaultConditionName);
     }
     // Package weight and qty free shipping
     $oldWeight = $request->getPackageWeight();
     $oldQty = $request->getPackageQty();
     $request->setPackageWeight($request->getFreeMethodWeight());
     $request->setPackageQty($oldQty - $freeQty);
     /** @var \Magento\Shipping\Model\Rate\Result $result */
     $result = $this->_rateResultFactory->create();
     $rate = $this->getRate($request);
     $request->setPackageWeight($oldWeight);
     $request->setPackageQty($oldQty);
     if (!empty($rate) && $rate['price'] >= 0) {
         /** @var \Magento\Quote\Model\Quote\Address\RateResult\Method $method */
         $method = $this->_resultMethodFactory->create();
         $method->setCarrier('tablerate');
         $method->setCarrierTitle($this->getConfigData('title'));
         $method->setMethod('bestway');
         $method->setMethodTitle($this->getConfigData('name'));
         if ($request->getFreeShipping() === true || $request->getPackageQty() == $freeQty) {
             $shippingPrice = 0;
         } else {
             $shippingPrice = $this->getFinalPriceWithHandlingFee($rate['price']);
         }
         $method->setPrice($shippingPrice);
         $method->setCost($rate['cost']);
         $result->append($method);
     } else {
         /** @var \Magento\Quote\Model\Quote\Address\RateResult\Error $error */
         $error = $this->_rateErrorFactory->create(['data' => ['carrier' => $this->_code, 'carrier_title' => $this->getConfigData('title'), 'error_message' => $this->getConfigData('specificerrmsg')]]);
         $result->append($error);
     }
     return $result;
 }
示例#14
0
 /**
  * @param $ratesToAdd
  * @return \Magento\Shipping\Model\Rate\Result
  */
 public function createMergedRate($ratesToAdd)
 {
     $result = $this->rateFactory->create();
     foreach ($ratesToAdd as $rateToAdd) {
         $method = $this->rateMethodFactory->create();
         $method->setPrice((double) $rateToAdd['price']);
         $method->setCost((double) $rateToAdd['price']);
         $method->setCarrier($this->_code);
         $method->setCarrierTitle($rateToAdd['mergedTitle']);
         $method->setMethod($rateToAdd['title']);
         $method->setMethodTitle($rateToAdd['title']);
         $method->setMethodDescription($rateToAdd['mergedDescription']);
         $method->setCarrierType(__('multiple_shipments'));
         $result->append($method);
     }
     return $result;
 }