Exemplo n.º 1
0
 public function testCollectRatesErrorMessage()
 {
     $this->scope->expects($this->once())->method('isSetFlag')->willReturn(false);
     $this->error->expects($this->once())->method('setCarrier')->with('fedex');
     $this->error->expects($this->once())->method('setCarrierTitle');
     $this->error->expects($this->once())->method('setErrorMessage');
     $request = new RateRequest();
     $request->setPackageWeight(1);
     $this->assertSame($this->error, $this->_model->collectRates($request));
 }
Exemplo n.º 2
0
 public function testCollectRatesFail()
 {
     $this->scope->expects($this->once())->method('isSetFlag')->willReturn(true);
     $request = new RateRequest();
     $request->setPackageWeight(1);
     $this->assertSame($this->rate, $this->model->collectRates($request));
 }
Exemplo n.º 3
0
 public function testCollectRatesFail()
 {
     $this->scope->expects($this->once())->method('isSetFlag')->willReturn(true);
     $this->scope->expects($this->atLeastOnce())->method('getValue')->willReturnMap([['carriers/usps/userid' => 123], ['carriers/usps/container' => 11]]);
     $request = new RateRequest();
     $request->setPackageWeight(1);
     $this->assertNotEmpty($this->carrier->collectRates($request));
 }
Exemplo n.º 4
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;
 }
Exemplo n.º 5
0
 /**
  * Enter description here...
  *
  * @param Mage_Shipping_Model_Rate_Request $data
  * @return Mage_Shipping_Model_Rate_Result
  */
 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() || $item->getProductType() == 'downloadable') {
                         $request->setPackageValue($request->getPackageValue() - $child->getBaseRowTotal());
                     }
                 }
             } elseif ($item->getProduct()->isVirtual() || $item->getProductType() == 'downloadable') {
                 $request->setPackageValue($request->getPackageValue() - $item->getBaseRowTotal());
             }
         }
     }
     // Free shipping by qty
     $freeQty = 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()) {
                         $freeQty += $item->getQty() * ($child->getQty() - (is_numeric($child->getFreeShipping()) ? $child->getFreeShipping() : 0));
                     }
                 }
             } elseif ($item->getFreeShipping()) {
                 $freeQty += $item->getQty() - (is_numeric($item->getFreeShipping()) ? $item->getFreeShipping() : 0);
             }
         }
     }
     if (!$request->getMRConditionName()) {
         if ($this->areaState->getAreaCode() == 'adminhtml') {
             $request->setMRConditionName(['notempty', 'admin', $this->getConfigData('condition_name') ? $this->getConfigData('condition_name') : $this->_default_condition_name]);
         } else {
             $request->setMRConditionName($this->getConfigData('condition_name') ? $this->getConfigData('condition_name') : $this->_default_condition_name);
         }
     }
     // Package weight and qty free shipping
     $oldWeight = $request->getPackageWeight();
     $oldQty = $request->getPackageQty();
     if ($this->getConfigData('allow_free_shipping_promotions') && !$this->getConfigData('include_free_ship_items')) {
         $request->setPackageWeight($request->getFreeMethodWeight());
         $request->setPackageQty($oldQty - $freeQty);
     }
     $result = $this->_rateResultFactory->create();
     $ratearray = $this->getRate($request);
     $request->setPackageWeight($oldWeight);
     $request->setPackageQty($oldQty);
     $freeShipping = false;
     if (is_numeric($this->getConfigData('free_shipping_threshold')) && $this->getConfigData('free_shipping_threshold') > 0 && $request->getPackageValue() > $this->getConfigData('free_shipping_threshold')) {
         $freeShipping = true;
     }
     if ($this->getConfigData('allow_free_shipping_promotions') && ($request->getFreeShipping() === true || $request->getPackageQty() == $this->getFreeBoxes())) {
         $freeShipping = true;
     }
     if ($freeShipping) {
         $method = $this->_resultMethodFactory->create();
         $method->setCarrier('matrixrate');
         $method->setCarrierTitle($this->getConfigData('title'));
         $method->setMethod('matrixrate_free');
         $method->setPrice('0.00');
         $method->setMethodTitle($this->getConfigData('free_method_text'));
         $result->append($method);
         if ($this->getConfigData('show_only_free')) {
             return $result;
         }
     }
     foreach ($ratearray as $rate) {
         if (!empty($rate) && $rate['price'] >= 0) {
             $method = $this->_resultMethodFactory->create();
             $method->setCarrier('matrixrate');
             $method->setCarrierTitle($this->getConfigData('title'));
             $method->setMethod('matrixrate_' . $rate['pk']);
             $method->setMethodTitle(__($rate['delivery_type']));
             $shippingPrice = $this->getFinalPriceWithHandlingFee($rate['price']);
             $method->setCost($rate['cost']);
             $method->setDeliveryType($rate['delivery_type']);
             $method->setPrice($shippingPrice);
             $result->append($method);
         }
     }
     return $result;
 }