Example #1
0
 /**
  * @param \Magento\Sales\Model\Quote\Address\RateRequest $request
  * @return void
  */
 protected function _updateFreeMethodQuote($request)
 {
     if ($request->getFreeMethodWeight() == $request->getPackageWeight() || !$request->hasFreeMethodWeight()) {
         return;
     }
     $freeMethod = $this->getConfigData($this->_freeMethod);
     if (!$freeMethod) {
         return;
     }
     $freeRateId = false;
     if (is_object($this->_result)) {
         foreach ($this->_result->getAllRates() as $i => $item) {
             if ($item->getMethod() == $freeMethod) {
                 $freeRateId = $i;
                 break;
             }
         }
     }
     if ($freeRateId === false) {
         return;
     }
     $price = null;
     if ($request->getFreeMethodWeight() > 0) {
         $this->_setFreeMethodRequest($freeMethod);
         $result = $this->_getQuotes();
         if ($result && ($rates = $result->getAllRates()) && count($rates) > 0) {
             if (count($rates) == 1 && $rates[0] instanceof \Magento\Sales\Model\Quote\Address\RateResult\Method) {
                 $price = $rates[0]->getPrice();
             }
             if (count($rates) > 1) {
                 foreach ($rates as $rate) {
                     if ($rate instanceof \Magento\Sales\Model\Quote\Address\RateResult\Method && $rate->getMethod() == $freeMethod) {
                         $price = $rate->getPrice();
                     }
                 }
             }
         }
     } else {
         /**
          * if we can apply free shipping for all order we should force price
          * to $0.00 for shipping with out sending second request to carrier
          */
         $price = 0;
     }
     /**
      * if we did not get our free shipping method in response we must use its old price
      */
     if (!is_null($price)) {
         $this->_result->getRateById($freeRateId)->setPrice($price);
     }
 }