/** * @param RateRequest $request * @return bool|Result|null */ public function collectRates(RateRequest $request) { if (!$this->getConfigFlag('active')) { return false; } $expressAvailable = true; $expressMaxWeight = $this->getConfigData('express_max_weight'); $shippingTotalWeight = 0; $this->_result = $this->_rateResultFactory->create(); 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()) { $shippingTotalWeight += $child->getWeight(); } } } elseif ($item->getFreeShipping()) { $shippingTotalWeight += $item->getWeight(); } } } if ($shippingTotalWeight > $expressMaxWeight) { $expressAvailable = false; } if ($expressAvailable) { $this->_getExpressRate(); } $this->_getStandardRate(); return $this->getResult(); }
/** * @param \Magento\Sales\Model\Quote\Address\RateRequest $request * @return Result|bool */ public function collectRates(\Magento\Sales\Model\Quote\Address\RateRequest $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\Sales\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; }
/** * Prepare items to pieces * * @return array */ protected function _getAllItems() { $allItems = $this->_request->getAllItems(); $fullItems = array(); foreach ($allItems as $item) { if ($item->getProductType() == Type::TYPE_BUNDLE && $item->getProduct()->getShipmentType()) { continue; } $qty = $item->getQty(); $changeQty = true; $checkWeight = true; $decimalItems = array(); if ($item->getParentItem()) { if (!$item->getParentItem()->getProduct()->getShipmentType()) { continue; } if ($item->getIsQtyDecimal()) { $qty = $item->getParentItem()->getQty(); } else { $qty = $item->getParentItem()->getQty() * $item->getQty(); } } $itemWeight = $item->getWeight(); if ($item->getIsQtyDecimal() && $item->getProductType() != Type::TYPE_BUNDLE) { $productId = $item->getProduct()->getId(); $isDecimalDivided = $this->stockItemService->getStockItem($productId)->getIsDecimalDivided(); if ($isDecimalDivided) { if ($this->stockItemService->getEnableQtyIncrements($productId) && $this->stockItemService->getQtyIncrements($productId)) { $itemWeight = $itemWeight * $this->stockItemService->getQtyIncrements($productId); $qty = round($item->getWeight() / $itemWeight * $qty); $changeQty = false; } else { $itemWeight = $this->_getWeight($itemWeight * $item->getQty()); $maxWeight = $this->_getWeight($this->_maxWeight, true); if ($itemWeight > $maxWeight) { $qtyItem = floor($itemWeight / $maxWeight); $decimalItems[] = array('weight' => $maxWeight, 'qty' => $qtyItem); $weightItem = $this->mathDivision->getExactDivision($itemWeight, $maxWeight); if ($weightItem) { $decimalItems[] = array('weight' => $weightItem, 'qty' => 1); } $checkWeight = false; } } } else { $itemWeight = $itemWeight * $item->getQty(); } } if ($checkWeight && $this->_getWeight($itemWeight) > $this->_getWeight($this->_maxWeight, true)) { return array(); } if ($changeQty && !$item->getParentItem() && $item->getIsQtyDecimal() && $item->getProductType() != Type::TYPE_BUNDLE) { $qty = 1; } if (!empty($decimalItems)) { foreach ($decimalItems as $decimalItem) { $fullItems = array_merge($fullItems, array_fill(0, $decimalItem['qty'] * $qty, $decimalItem['weight'])); } } else { $fullItems = array_merge($fullItems, array_fill(0, $qty, $this->_getWeight($itemWeight))); } } sort($fullItems); return $fullItems; }
/** * Compose Packages For Carrier. * Divides order into items and items into parts if it's necessary * * @param \Magento\Shipping\Model\Carrier\AbstractCarrier $carrier * @param \Magento\Sales\Model\Quote\Address\RateRequest $request * @return array [int, float] */ public function composePackagesForCarrier($carrier, $request) { $allItems = $request->getAllItems(); $fullItems = array(); $maxWeight = (double) $carrier->getConfigData('max_package_weight'); /** @var $item \Magento\Sales\Model\Quote\Item */ foreach ($allItems as $item) { if ($item->getProductType() == \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE && $item->getProduct()->getShipmentType()) { continue; } $qty = $item->getQty(); $changeQty = true; $checkWeight = true; $decimalItems = array(); if ($item->getParentItem()) { if (!$item->getParentItem()->getProduct()->getShipmentType()) { continue; } $qty = $item->getIsQtyDecimal() ? $item->getParentItem()->getQty() : $item->getParentItem()->getQty() * $item->getQty(); } $itemWeight = $item->getWeight(); if ($item->getIsQtyDecimal() && $item->getProductType() != \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) { $productId = $item->getProduct()->getId(); if ($this->stockItemService->getStockItem($productId)->getIsDecimalDivided()) { if ($this->stockItemService->getEnableQtyIncrements($productId) && $this->stockItemService->getQtyIncrements($productId)) { $itemWeight = $itemWeight * $this->stockItemService->getQtyIncrements($productId); $qty = round($item->getWeight() / $itemWeight * $qty); $changeQty = false; } else { $itemWeight = $itemWeight * $item->getQty(); if ($itemWeight > $maxWeight) { $qtyItem = floor($itemWeight / $maxWeight); $decimalItems[] = array('weight' => $maxWeight, 'qty' => $qtyItem); $weightItem = $this->mathDivision->getExactDivision($itemWeight, $maxWeight); if ($weightItem) { $decimalItems[] = array('weight' => $weightItem, 'qty' => 1); } $checkWeight = false; } else { $itemWeight = $itemWeight * $item->getQty(); } } } else { $itemWeight = $itemWeight * $item->getQty(); } } if ($checkWeight && $maxWeight && $itemWeight > $maxWeight) { return array(); } if ($changeQty && !$item->getParentItem() && $item->getIsQtyDecimal() && $item->getProductType() != \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) { $qty = 1; } if (!empty($decimalItems)) { foreach ($decimalItems as $decimalItem) { $fullItems = array_merge($fullItems, array_fill(0, $decimalItem['qty'] * $qty, $decimalItem['weight'])); } } else { $fullItems = array_merge($fullItems, array_fill(0, $qty, $itemWeight)); } } sort($fullItems); return $this->_makePieces($fullItems, $maxWeight); }
/** * @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; }
/** * Allows free shipping when all product items have free shipping (promotions etc.) * * @param \Magento\Sales\Model\Quote\Address\RateRequest $request * @return void */ protected function _updateFreeMethodQuote($request) { $freeShipping = false; $items = $request->getAllItems(); $c = count($items); for ($i = 0; $i < $c; $i++) { if ($items[$i]->getProduct() instanceof \Magento\Catalog\Model\Product) { if ($items[$i]->getFreeShipping()) { $freeShipping = true; } else { return; } } } if ($freeShipping) { $request->setFreeShipping(true); } }
/** * Return items for further shipment rate evaluation. We need to pass children of a bundle instead passing the * bundle itself, otherwise we may not get a rate at all (e.g. when total weight of a bundle exceeds max weight * despite each item by itself is not) * * @param RateRequest $request * @return array */ public function getAllItems(RateRequest $request) { $items = array(); if ($request->getAllItems()) { foreach ($request->getAllItems() as $item) { /* @var $item \Magento\Sales\Model\Quote\Item */ if ($item->getProduct()->isVirtual() || $item->getParentItem()) { // Don't process children here - we will process (or already have processed) them below continue; } if ($item->getHasChildren() && $item->isShipSeparately()) { foreach ($item->getChildren() as $child) { if (!$child->getFreeShipping() && !$child->getProduct()->isVirtual()) { $items[] = $child; } } } else { // Ship together - count compound item as one solid $items[] = $item; } } } return $items; }