Example #1
0
 /**
  * @param \Magento\Sales\Model\Quote\Address\RateRequest $request
  * @return \Magento\Shipping\Model\Rate\Result
  */
 public function collectRates(\Magento\Sales\Model\Quote\Address\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\Sales\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\Sales\Model\Quote\Address\RateResult\Error $error */
         $error = $this->_rateErrorFactory->create(array('data' => array('carrier' => $this->_code, 'carrier_title' => $this->getConfigData('title'), 'error_message' => $this->getConfigData('specificerrmsg'))));
         $result->append($error);
     }
     return $result;
 }