![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/Ecombricks/InventoryQuote/Model/Quote/Address/Total/Shipping/ |
<?php /** * Copyright © eComBricks. All rights reserved. * See LICENSE.txt for license details. */ namespace Ecombricks\InventoryQuote\Model\Quote\Address\Total\Shipping; /** * Quote address total shipping source trait */ trait SourceTrait { /** * Get item row weight * * @param bool $addressFreeShipping * @param float $itemWeight * @param float $itemQty * @param $freeShipping * @return float */ protected function getItemRowWeight($addressFreeShipping, $itemWeight, $itemQty, $freeShipping) { $rowWeight = $itemWeight * $itemQty; if ($addressFreeShipping || $freeShipping === true) { $rowWeight = 0; } elseif (is_numeric($freeShipping)) { $freeQty = $freeShipping; if ($itemQty > $freeQty) { $rowWeight = $itemWeight * ($itemQty - $freeQty); } else { $rowWeight = 0; } } return (float) $rowWeight; } /** * Get item weight data * * @param \Magento\Quote\Api\Data\CartItemInterface $item * @param bool $addressFreeShipping * @return array */ protected function getItemWeightData($item, $addressFreeShipping) { $itemWeight = (float) $item->getWeight(); $itemQty = (float) $item->getTotalQty(); $weight = ($itemWeight * $itemQty); $rowWeight = $this->getItemRowWeight($addressFreeShipping, $itemWeight, $itemQty, $item->getFreeShipping()); $item->setRowWeight($rowWeight); return ['weight' => $weight, 'rowWeight' => $rowWeight]; } /** * Get assignment weight data * * @param \Magento\Quote\Api\Data\AddressInterface $address * @param array $items * @return array */ protected function getAssignmentWeightData(\Magento\Quote\Api\Data\AddressInterface $address, array $items) { $address->setWeight(0); $address->setFreeMethodWeight(0); $addressWeight = $address->getWeight(); $freeMethodWeight = $address->getFreeMethodWeight(); $addressFreeShipping = (bool) $address->getFreeShipping(); $addressQty = 0; foreach ($items as $item) { $product = $item->getProduct(); $isVirtual = $product->isVirtual(); if ($isVirtual || $item->getParentItem()) { continue; } $itemQty = (float) $item->getQty(); if ($item->getHasChildren() && $item->isShipSeparately()) { $weightType = $product->getWeightType(); foreach ($item->getChildren() as $childItem) { if ($childItem->getProduct()->isVirtual()) { continue; } $addressQty += $childItem->getTotalQty(); if ($weightType) { continue; } $itemWeightData = $this->getItemWeightData($childItem, $addressFreeShipping); $addressWeight += $itemWeightData['weight']; $freeMethodWeight += $itemWeightData['rowWeight']; } if (!$weightType) { continue; } $itemWeightData = $this->getItemWeightData($item, $addressFreeShipping); $addressWeight += $itemWeightData['weight']; $freeMethodWeight += $itemWeightData['rowWeight']; } else { $addressQty += $itemQty; $itemWeightData = $this->getItemWeightData($item, $addressFreeShipping); $addressWeight += $itemWeightData['weight']; $freeMethodWeight += $itemWeightData['rowWeight']; } } return [ 'addressQty' => $addressQty, 'addressWeight' => $addressWeight, 'freeMethodWeight' => $freeMethodWeight, ]; } /** * Collect * * @param \Magento\Quote\Model\Quote $quote * @param \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment * @param \Magento\Quote\Model\Quote\Address\Total $total * @return $this */ public function inventoryCollect( \Magento\Quote\Model\Quote $quote, \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment, \Magento\Quote\Model\Quote\Address\Total $total ) { parent::collect($quote, $shippingAssignment, $total); $shipping = $shippingAssignment->getShipping(); $address = $shipping->getAddress(); $code = $this->getCode(); $total->setTotalAmount($code, 0); $total->setBaseTotalAmount($code, 0); $items = $shippingAssignment->getItems(); if (!count($items)) { return $this; } $data = $this->getAssignmentWeightData($address, $items); $address->setItemQty($data['addressQty']); $address->setWeight($data['addressWeight']); $address->setFreeMethodWeight($data['freeMethodWeight']); $addressFreeShipping = (bool) $address->getFreeShipping(); $freeShipping = $this->freeShipping->isFreeShipping($quote, $items); $address->setFreeShipping($freeShipping); if (!$addressFreeShipping && $freeShipping) { $data = $this->getAssignmentWeightData($address, $items); $address->setItemQty($data['addressQty']); $address->setWeight($data['addressWeight']); $address->setFreeMethodWeight($data['freeMethodWeight']); } $address->collectShippingRates(); $shippingMethods = $shipping->getMethod(); if (empty($shippingMethods)) { return $this; } $store = $quote->getStore(); $basePrice = 0; $price = 0; $shippingRates = $address->getShippingRateByCode($shippingMethods); foreach ($shippingRates as $shippingRate) { $shippingRatePrice = $shippingRate->getPrice(); $basePrice += $shippingRatePrice; $price += $this->priceCurrency->convert($shippingRatePrice, $store); } $total->setBaseTotalAmount($code, $basePrice); $total->setTotalAmount($code, $price); $total->setBaseShippingAmount($basePrice); $total->setShippingAmount($price); $address->generateShippingDescription($shippingMethods); $total->setShippingDescription($address->getShippingDescription()); return $this; } }