Shipping.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Quote\Model\Quote\Address\Total;
  7. use Magento\Framework\Pricing\PriceCurrencyInterface;
  8. use Magento\Quote\Api\Data\AddressInterface;
  9. use Magento\Quote\Model\Quote\Address\FreeShippingInterface;
  10. /**
  11. * Collect totals for shipping.
  12. */
  13. class Shipping extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal
  14. {
  15. /**
  16. * @var PriceCurrencyInterface
  17. */
  18. protected $priceCurrency;
  19. /**
  20. * @var FreeShippingInterface
  21. */
  22. protected $freeShipping;
  23. /**
  24. * @param PriceCurrencyInterface $priceCurrency
  25. * @param FreeShippingInterface $freeShipping
  26. */
  27. public function __construct(
  28. PriceCurrencyInterface $priceCurrency,
  29. FreeShippingInterface $freeShipping
  30. ) {
  31. $this->priceCurrency = $priceCurrency;
  32. $this->freeShipping = $freeShipping;
  33. $this->setCode('shipping');
  34. }
  35. /**
  36. * Collect totals information about shipping
  37. *
  38. * @param \Magento\Quote\Model\Quote $quote
  39. * @param \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment
  40. * @param \Magento\Quote\Model\Quote\Address\Total $total
  41. * @return $this
  42. * @SuppressWarnings(PHPMD.NPathComplexity)
  43. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  44. */
  45. public function collect(
  46. \Magento\Quote\Model\Quote $quote,
  47. \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment,
  48. \Magento\Quote\Model\Quote\Address\Total $total
  49. ) {
  50. parent::collect($quote, $shippingAssignment, $total);
  51. $address = $shippingAssignment->getShipping()->getAddress();
  52. $method = $shippingAssignment->getShipping()->getMethod();
  53. $total->setTotalAmount($this->getCode(), 0);
  54. $total->setBaseTotalAmount($this->getCode(), 0);
  55. if (!count($shippingAssignment->getItems())) {
  56. return $this;
  57. }
  58. $data = $this->getAssignmentWeightData($address, $shippingAssignment->getItems());
  59. $address->setItemQty($data['addressQty']);
  60. $address->setWeight($data['addressWeight']);
  61. $address->setFreeMethodWeight($data['freeMethodWeight']);
  62. $addressFreeShipping = (bool)$address->getFreeShipping();
  63. $isFreeShipping = $this->freeShipping->isFreeShipping($quote, $shippingAssignment->getItems());
  64. $address->setFreeShipping($isFreeShipping);
  65. if (!$addressFreeShipping && $isFreeShipping) {
  66. $data = $this->getAssignmentWeightData($address, $shippingAssignment->getItems());
  67. $address->setItemQty($data['addressQty']);
  68. $address->setWeight($data['addressWeight']);
  69. $address->setFreeMethodWeight($data['freeMethodWeight']);
  70. }
  71. $address->collectShippingRates();
  72. if ($method) {
  73. foreach ($address->getAllShippingRates() as $rate) {
  74. if ($rate->getCode() == $method) {
  75. $store = $quote->getStore();
  76. $amountPrice = $this->priceCurrency->convert(
  77. $rate->getPrice(),
  78. $store
  79. );
  80. $total->setTotalAmount($this->getCode(), $amountPrice);
  81. $total->setBaseTotalAmount($this->getCode(), $rate->getPrice());
  82. $shippingDescription = $rate->getCarrierTitle() . ' - ' . $rate->getMethodTitle();
  83. $address->setShippingDescription(trim($shippingDescription, ' -'));
  84. $total->setBaseShippingAmount($rate->getPrice());
  85. $total->setShippingAmount($amountPrice);
  86. $total->setShippingDescription($address->getShippingDescription());
  87. break;
  88. }
  89. }
  90. }
  91. return $this;
  92. }
  93. /**
  94. * Add shipping totals information to address object
  95. *
  96. * @param \Magento\Quote\Model\Quote $quote
  97. * @param \Magento\Quote\Model\Quote\Address\Total $total
  98. * @return array
  99. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  100. */
  101. public function fetch(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Model\Quote\Address\Total $total)
  102. {
  103. $amount = $total->getShippingAmount();
  104. $shippingDescription = $total->getShippingDescription();
  105. $title = ($shippingDescription)
  106. ? __('Shipping & Handling (%1)', $shippingDescription)
  107. : __('Shipping & Handling');
  108. return [
  109. 'code' => $this->getCode(),
  110. 'title' => $title,
  111. 'value' => $amount
  112. ];
  113. }
  114. /**
  115. * Get Shipping label
  116. *
  117. * @return \Magento\Framework\Phrase
  118. */
  119. public function getLabel()
  120. {
  121. return __('Shipping');
  122. }
  123. /**
  124. * Gets shipping assignments data like items weight, address weight, items quantity.
  125. *
  126. * @param AddressInterface $address
  127. * @param array $items
  128. * @return array
  129. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  130. */
  131. private function getAssignmentWeightData(AddressInterface $address, array $items): array
  132. {
  133. $address->setWeight(0);
  134. $address->setFreeMethodWeight(0);
  135. $addressWeight = $address->getWeight();
  136. $freeMethodWeight = $address->getFreeMethodWeight();
  137. $addressFreeShipping = (bool)$address->getFreeShipping();
  138. $addressQty = 0;
  139. foreach ($items as $item) {
  140. /**
  141. * Skip if this item is virtual
  142. */
  143. if ($item->getProduct()->isVirtual()) {
  144. continue;
  145. }
  146. /**
  147. * Children weight we calculate for parent
  148. */
  149. if ($item->getParentItem()) {
  150. continue;
  151. }
  152. $itemQty = (float)$item->getQty();
  153. $itemWeight = (float)$item->getWeight();
  154. if ($item->getHasChildren() && $item->isShipSeparately()) {
  155. foreach ($item->getChildren() as $child) {
  156. if ($child->getProduct()->isVirtual()) {
  157. continue;
  158. }
  159. $addressQty += $child->getTotalQty();
  160. if (!$item->getProduct()->getWeightType()) {
  161. $itemWeight = (float)$child->getWeight();
  162. $itemQty = (float)$child->getTotalQty();
  163. $addressWeight += ($itemWeight * $itemQty);
  164. $rowWeight = $this->getItemRowWeight(
  165. $addressFreeShipping,
  166. $itemWeight,
  167. $itemQty,
  168. $child->getFreeShipping()
  169. );
  170. $freeMethodWeight += $rowWeight;
  171. $item->setRowWeight($rowWeight);
  172. }
  173. }
  174. if ($item->getProduct()->getWeightType()) {
  175. $addressWeight += ($itemWeight * $itemQty);
  176. $rowWeight = $this->getItemRowWeight(
  177. $addressFreeShipping,
  178. $itemWeight,
  179. $itemQty,
  180. $item->getFreeShipping()
  181. );
  182. $freeMethodWeight += $rowWeight;
  183. $item->setRowWeight($rowWeight);
  184. }
  185. } else {
  186. if (!$item->getProduct()->isVirtual()) {
  187. $addressQty += $itemQty;
  188. }
  189. $addressWeight += ($itemWeight * $itemQty);
  190. $rowWeight = $this->getItemRowWeight(
  191. $addressFreeShipping,
  192. $itemWeight,
  193. $itemQty,
  194. $item->getFreeShipping()
  195. );
  196. $freeMethodWeight += $rowWeight;
  197. $item->setRowWeight($rowWeight);
  198. }
  199. }
  200. return [
  201. 'addressQty' => $addressQty,
  202. 'addressWeight' => $addressWeight,
  203. 'freeMethodWeight' => $freeMethodWeight
  204. ];
  205. }
  206. /**
  207. * Calculates item row weight.
  208. *
  209. * @param bool $addressFreeShipping
  210. * @param float $itemWeight
  211. * @param float $itemQty
  212. * @param bool $freeShipping
  213. * @return float
  214. */
  215. private function getItemRowWeight(
  216. bool $addressFreeShipping,
  217. float $itemWeight,
  218. float $itemQty,
  219. $freeShipping
  220. ): float {
  221. $rowWeight = $itemWeight * $itemQty;
  222. if ($addressFreeShipping || $freeShipping === true) {
  223. $rowWeight = 0;
  224. } elseif (is_numeric($freeShipping)) {
  225. $freeQty = $freeShipping;
  226. if ($itemQty > $freeQty) {
  227. $rowWeight = $itemWeight * ($itemQty - $freeQty);
  228. } else {
  229. $rowWeight = 0;
  230. }
  231. }
  232. return (float)$rowWeight;
  233. }
  234. }