10 use Magento\Quote\Model\Quote\Address\RateRequestFactory;
89 private $rateRequestFactory;
112 \
Magento\Directory\Model\RegionFactory $regionFactory,
115 RateRequestFactory $rateRequestFactory =
null 117 $this->_scopeConfig = $scopeConfig;
118 $this->_shippingConfig = $shippingConfig;
120 $this->_carrierFactory = $carrierFactory;
121 $this->_rateResultFactory = $rateResultFactory;
122 $this->_shipmentRequestFactory = $shipmentRequestFactory;
123 $this->_regionFactory = $regionFactory;
136 if (empty($this->_result)) {
137 $this->_result = $this->_rateResultFactory->create();
150 $this->_orig =
$data;
186 $this->_scopeConfig->getValue(
188 \
Magento\Store\Model\ScopeInterface::SCOPE_STORE,
192 $this->_scopeConfig->getValue(
194 \
Magento\Store\Model\ScopeInterface::SCOPE_STORE,
198 $this->_scopeConfig->getValue(
200 \
Magento\Store\Model\ScopeInterface::SCOPE_STORE,
204 $this->_scopeConfig->getValue(
206 \
Magento\Store\Model\ScopeInterface::SCOPE_STORE,
212 $limitCarrier =
$request->getLimitCarrier();
213 if (!$limitCarrier) {
214 $carriers = $this->_scopeConfig->getValue(
216 \
Magento\Store\Model\ScopeInterface::SCOPE_STORE,
220 foreach ($carriers as $carrierCode => $carrierConfig) {
224 if (!is_array($limitCarrier)) {
225 $limitCarrier = [$limitCarrier];
227 foreach ($limitCarrier as $carrierCode) {
228 $carrierConfig = $this->_scopeConfig->getValue(
229 'carriers/' . $carrierCode,
230 \
Magento\Store\Model\ScopeInterface::SCOPE_STORE,
233 if (!$carrierConfig) {
255 $carrier = $this->_carrierFactory->createIfActive($carrierCode,
$request->getStoreId());
259 $carrier->setActiveFlag($this->_availabilityConfigField);
269 if (!
$result instanceof \
Magento\Quote\Model\Quote\Address\RateResult\Error) {
270 if ($carrier->getConfigData(
'shipment_requesttype')) {
271 $packages = $this->composePackagesForCarrier($carrier,
$request);
272 if (!empty($packages)) {
274 foreach ($packages as $weight => $packageCount) {
275 $request->setPackageWeight($weight);
280 $result->updateRatePrice($packageCount);
284 if (!empty($sumResults) && count($sumResults) > 1) {
286 foreach ($sumResults as $res) {
291 foreach ($res->getAllRates() as
$method) {
292 foreach (
$result->getAllRates() as $resultMethod) {
293 if (
$method->getMethod() == $resultMethod->getMethod()) {
294 $resultMethod->setPrice(
$method->getPrice() + $resultMethod->getPrice());
311 if ($carrier->getConfigData(
'showmethod') == 0 &&
$result->getError()) {
315 if (method_exists(
$result,
'sortRatesByPrice') && is_callable([
$result,
'sortRatesByPrice'])) {
333 public function composePackagesForCarrier($carrier,
$request)
335 $allItems =
$request->getAllItems();
338 $maxWeight = (double)$carrier->getConfigData(
'max_package_weight');
341 foreach ($allItems as
$item) {
342 if (
$item->getProductType() == \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE
343 &&
$item->getProduct()->getShipmentType()
348 $qty =
$item->getQty();
353 if (
$item->getParentItem()) {
354 if (!
$item->getParentItem()->getProduct()->getShipmentType()) {
357 $qty =
$item->getIsQtyDecimal()
358 ?
$item->getParentItem()->getQty()
359 :
$item->getParentItem()->getQty() *
$item->getQty();
362 $itemWeight =
$item->getWeight();
363 if (
$item->getIsQtyDecimal()
364 &&
$item->getProductType() != \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE
371 $itemWeight = $itemWeight *
$stockItem->getQtyIncrements();
372 $qty = round(
$item->getWeight() / $itemWeight * $qty);
375 $itemWeight = $itemWeight *
$item->getQty();
376 if ($itemWeight > $maxWeight) {
377 $qtyItem = floor($itemWeight / $maxWeight);
378 $decimalItems[] = [
'weight' => $maxWeight,
'qty' => $qtyItem];
379 $weightItem = $this->mathDivision->getExactDivision($itemWeight, $maxWeight);
381 $decimalItems[] = [
'weight' => $weightItem,
'qty' => 1];
383 $checkWeight =
false;
385 $itemWeight = $itemWeight *
$item->getQty();
389 $itemWeight = $itemWeight *
$item->getQty();
393 if ($checkWeight && $maxWeight && $itemWeight > $maxWeight) {
398 && !
$item->getParentItem()
399 &&
$item->getIsQtyDecimal()
400 &&
$item->getProductType() != \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE
405 if (!empty($decimalItems)) {
406 foreach ($decimalItems as $decimalItem) {
407 $fullItems = array_merge(
409 array_fill(0, $decimalItem[
'qty'] * $qty, $decimalItem[
'weight'])
413 $fullItems = array_merge($fullItems, array_fill(0, $qty, $itemWeight));
435 $reverseOrderItems =
$items;
436 arsort($reverseOrderItems);
438 foreach ($reverseOrderItems as $key => $weight) {
439 if (!isset(
$items[$key])) {
443 $sumWeight = $weight;
444 foreach (
$items as $key => $weight) {
445 if ($sumWeight + $weight < $maxWeight) {
447 $sumWeight += $weight;
448 }
elseif ($sumWeight + $weight > $maxWeight) {
449 $pieces[] = (string)(
double)$sumWeight;
453 $pieces[] = (string)(
double)($sumWeight + $weight);
459 if ($sumWeight > 0) {
460 $pieces[] = (string)(
double)$sumWeight;
462 $pieces = array_count_values($pieces);
478 $request = $this->rateRequestFactory->create();
484 $request->setPackageValueWithDiscount(
$address->getBaseSubtotalWithDiscount());
490 $store = $this->_storeManager->getStore();
495 $request->setLimitCarrier($limitCarrier);
510 $this->_availabilityConfigField =
$code;
setCarrierAvailabilityConfigField($code='active')
elseif(isset( $params[ 'redirect_parent']))
collectRates(\Magento\Quote\Model\Quote\Address\RateRequest $request)
__construct(\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Shipping\Model\Config $shippingConfig, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Shipping\Model\CarrierFactory $carrierFactory, \Magento\Shipping\Model\Rate\ResultFactory $rateResultFactory, \Magento\Shipping\Model\Shipment\RequestFactory $shipmentRequestFactory, \Magento\Directory\Model\RegionFactory $regionFactory, \Magento\Framework\Math\Division $mathDivision, \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry, RateRequestFactory $rateRequestFactory=null)
collectCarrierRates($carrierCode, $request)
_makePieces($items, $maxWeight)
const XML_PATH_STORE_COUNTRY_ID
const XML_PATH_STORE_REGION_ID
const XML_PATH_STORE_CITY
$_availabilityConfigField