public function collectRates(RateRequest $request) { if (!$this->getConfigFlag('active')) { return false; } //Do some filtering of items in cart if ($request->getAllItems()) { foreach ($request->getAllItems() as $item) { //$item->getQty(); //$item->getFreeShipping() //$item->isShipSeparately() //$item->getHasChildren() //$item->getProduct()->isVirtual() //... } } //After filtering, start forming final price //Final price does not have to be fixed like below $shippingPrice = $this->getConfigData('price'); $result = $this->_rateResultFactory->create(); $method = $this->_rateMethodFactory->create(); $method->setCarrier('shipbox'); $method->setCarrierTitle($this->getConfigData('title')); $method->setMethod('shipbox'); $method->setMethodTitle($this->getConfigData('name')); $method->setPrice($shippingPrice); $method->setCost($shippingPrice); $result->append($method); return $result; }
/** * @param RateRequest $request * @return Result|bool * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function collectRates(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\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 RateRequest $request * @return int */ private function getFreeBoxesCount(RateRequest $request) { $freeBoxes = 0; if ($request->getAllItems()) { foreach ($request->getAllItems() as $item) { if ($item->getProduct()->isVirtual() || $item->getParentItem()) { continue; } if ($item->getHasChildren() && $item->isShipSeparately()) { $freeBoxes += $this->getFreeBoxesCountFromChildren($item); } elseif ($item->getFreeShipping()) { $freeBoxes += $item->getQty(); } } } return $freeBoxes; }
/** * Allows free shipping when all product items have free shipping (promotions etc.) * * @param \Magento\Quote\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); } }
/** * @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; }
/** * @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; }
/** * 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 * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @api */ public function getAllItems(RateRequest $request) { $items = []; if ($request->getAllItems()) { foreach ($request->getAllItems() as $item) { /* @var $item \Magento\Quote\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; }
/** * Prepare items to pieces * * @return array * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ protected function _getAllItems() { $allItems = $this->_request->getAllItems(); $fullItems = []; foreach ($allItems as $item) { if ($item->getProductType() == Type::TYPE_BUNDLE && $item->getProduct()->getShipmentType()) { continue; } $qty = $item->getQty(); $changeQty = true; $checkWeight = true; $decimalItems = []; 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(); $stockItemDo = $this->stockRegistry->getStockItem($productId, $item->getStore()->getWebsiteId()); $isDecimalDivided = $stockItemDo->getIsDecimalDivided(); if ($isDecimalDivided) { if ($stockItemDo->getEnableQtyIncrements() && $stockItemDo->getQtyIncrements()) { $itemWeight = $itemWeight * $stockItemDo->getQtyIncrements(); $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[] = ['weight' => $maxWeight, 'qty' => $qtyItem]; $weightItem = $this->mathDivision->getExactDivision($itemWeight, $maxWeight); if ($weightItem) { $decimalItems[] = ['weight' => $weightItem, 'qty' => 1]; } $checkWeight = false; } } } else { $itemWeight = $itemWeight * $item->getQty(); } } if ($checkWeight && $this->_getWeight($itemWeight) > $this->_getWeight($this->_maxWeight, true)) { return []; } 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\Quote\Model\Quote\Address\RateRequest $request * @return array [int, float] * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function composePackagesForCarrier($carrier, $request) { $allItems = $request->getAllItems(); $fullItems = []; $maxWeight = (double) $carrier->getConfigData('max_package_weight'); /** @var $item \Magento\Quote\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 = []; 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(); $stockItem = $this->stockRegistry->getStockItem($productId, $item->getStore()->getWebsiteId()); if ($stockItem->getIsDecimalDivided()) { if ($stockItem->getEnableQtyIncrements() && $stockItem->getQtyIncrements()) { $itemWeight = $itemWeight * $stockItem->getQtyIncrements(); $qty = round($item->getWeight() / $itemWeight * $qty); $changeQty = false; } else { $itemWeight = $itemWeight * $item->getQty(); if ($itemWeight > $maxWeight) { $qtyItem = floor($itemWeight / $maxWeight); $decimalItems[] = ['weight' => $maxWeight, 'qty' => $qtyItem]; $weightItem = $this->mathDivision->getExactDivision($itemWeight, $maxWeight); if ($weightItem) { $decimalItems[] = ['weight' => $weightItem, 'qty' => 1]; } $checkWeight = false; } else { $itemWeight = $itemWeight * $item->getQty(); } } } else { $itemWeight = $itemWeight * $item->getQty(); } } if ($checkWeight && $maxWeight && $itemWeight > $maxWeight) { return []; } 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); }
/** * Prepare and set request to this instance * * @param \Magento\Quote\Model\Quote\Address\RateRequest $request * @return $this */ public function setRequest(\Magento\Quote\Model\Quote\Address\RateRequest $request) { if (is_array($request->getAllItems())) { $item = current($request->getAllItems()); if ($item instanceof QuoteItem) { $request->setQuote($item->getQuote()); $this->quote = $item->getQuote(); } } //SHQ16-1261 - further detail as values not on shipping address if (!$this->quote) { $this->quote = $this->shipperDataHelper->getQuote(); } $shippingAddress = $this->quote->getShippingAddress(); $key = $this->shipperDataHelper->getAddressKey($shippingAddress); $existing = $this->checkoutSession->getShipAddressValidation(); $validate = true; if (is_array($existing)) { if (isset($existing['key']) && $existing['key'] == $key) { $validate = false; } } else { $validate = $this->shipperRateHelper->shouldValidateAddress($shippingAddress->getValidationStatus(), $shippingAddress->getDestinationType()); } $request->setValidateAddress($validate); $request->setSelectedOptions($this->getSelectedOptions($shippingAddress)); $isCheckout = $this->shipperDataHelper->isCheckout($this->quote); $cartType = !is_null($isCheckout) && $isCheckout != 1 ? "CART" : "STD"; if ($this->quote->getIsMultiShipping()) { $cartType = 'MAC'; } $request->setCartType($cartType); $this->eventManager->dispatch('shipperhq_carrier_set_request', ['request' => $request]); $this->shipperRequest = $this->shipperMapper->getShipperTranslation($request); $this->rawRequest = $request; return $this; }
/** * @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->soap_init(); $rates = $this->get_services(); print_r($rates); die; // $rates = $this->getAllowedMethods(); foreach ($rates as $key => $rated) { $rate = $this->_rateMethodFactory->create(); $rate->setCarrier($this->_code); $rate->setCarrierTitle($this->getConfigData('title')); $rate->setMethodTitle($rated); $rate->setPrice(1.23); $rate->setCost(0); $this->_result->append($rate); // return $this; } return $this->getResult(); }
/** * 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; }