public function testCollect() { $this->shippingAssignment->expects($this->exactly(3))->method('getShipping')->willReturn($this->shipping); $this->shipping->expects($this->exactly(2))->method('getAddress')->willReturn($this->address); $this->shipping->expects($this->once())->method('getMethod')->willReturn('flatrate'); $this->shippingAssignment->expects($this->atLeastOnce())->method('getItems')->willReturn([$this->cartItem]); $this->freeShipping->expects($this->once())->method('isFreeShipping')->with($this->quote, [$this->cartItem])->willReturn(true); $this->address->expects($this->once())->method('setFreeShipping')->with(true); $this->total->expects($this->atLeastOnce())->method('setTotalAmount'); $this->total->expects($this->atLeastOnce())->method('setBaseTotalAmount'); $this->cartItem->expects($this->atLeastOnce())->method('getProduct')->willReturnSelf(); $this->cartItem->expects($this->atLeastOnce())->method('isVirtual')->willReturn(false); $this->cartItem->expects($this->once())->method('getParentItem')->willReturn(false); $this->cartItem->expects($this->once())->method('getHasChildren')->willReturn(false); $this->cartItem->expects($this->once())->method('getWeight')->willReturn(2); $this->cartItem->expects($this->atLeastOnce())->method('getQty')->willReturn(2); $this->address->expects($this->once())->method('getFreeShipping')->willReturn(true); $this->cartItem->expects($this->once())->method('setRowWeight')->with(0); $this->address->expects($this->once())->method('setItemQty')->with(2); $this->address->expects($this->atLeastOnce())->method('setWeight'); $this->address->expects($this->atLeastOnce())->method('setFreeMethodWeight'); $this->address->expects($this->once())->method('collectShippingRates'); $this->address->expects($this->once())->method('getAllShippingRates')->willReturn([$this->rate]); $this->rate->expects($this->once())->method('getCode')->willReturn('flatrate'); $this->quote->expects($this->once())->method('getStore')->willReturn($this->store); $this->rate->expects($this->atLeastOnce())->method('getPrice')->willReturn(5); $this->priceCurrency->expects($this->once())->method('convert')->with(5, $this->store)->willReturn(5); $this->total->expects($this->once())->method('setShippingAmount')->with(5); $this->rate->expects($this->once())->method('getCarrierTitle')->willReturn('Carrier title'); $this->rate->expects($this->once())->method('getMethodTitle')->willReturn('Method title'); $this->address->expects($this->once())->method('setShippingDescription')->with('Carrier title - Method title'); $this->address->expects($this->once())->method('getShippingDescription')->willReturn('Carrier title - Method title'); $this->total->expects($this->once())->method('setShippingDescription')->with('Carrier title - Method title'); $this->shippingModel->collect($this->quote, $this->shippingAssignment, $this->total); }
/** * Collect totals information about shipping * * @param \Magento\Quote\Model\Quote $quote * @param \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment * @param \Magento\Quote\Model\Quote\Address\Total $total * @return $this * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function collect(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment, \Magento\Quote\Model\Quote\Address\Total $total) { parent::collect($quote, $shippingAssignment, $total); $address = $shippingAssignment->getShipping()->getAddress(); $method = $shippingAssignment->getShipping()->getMethod(); $address->setWeight(0); $address->setFreeMethodWeight(0); $addressWeight = $address->getWeight(); $freeMethodWeight = $address->getFreeMethodWeight(); $address->setFreeShipping($this->freeShipping->isFreeShipping($quote, $shippingAssignment->getItems())); $total->setTotalAmount($this->getCode(), 0); $total->setBaseTotalAmount($this->getCode(), 0); if (!count($shippingAssignment->getItems())) { return $this; } $addressQty = 0; foreach ($shippingAssignment->getItems() as $item) { /** * Skip if this item is virtual */ if ($item->getProduct()->isVirtual()) { continue; } /** * Children weight we calculate for parent */ if ($item->getParentItem()) { continue; } if ($item->getHasChildren() && $item->isShipSeparately()) { foreach ($item->getChildren() as $child) { if ($child->getProduct()->isVirtual()) { continue; } $addressQty += $child->getTotalQty(); if (!$item->getProduct()->getWeightType()) { $itemWeight = $child->getWeight(); $itemQty = $child->getTotalQty(); $rowWeight = $itemWeight * $itemQty; $addressWeight += $rowWeight; if ($address->getFreeShipping() || $child->getFreeShipping() === true) { $rowWeight = 0; } elseif (is_numeric($child->getFreeShipping())) { $freeQty = $child->getFreeShipping(); if ($itemQty > $freeQty) { $rowWeight = $itemWeight * ($itemQty - $freeQty); } else { $rowWeight = 0; } } $freeMethodWeight += $rowWeight; $item->setRowWeight($rowWeight); } } if ($item->getProduct()->getWeightType()) { $itemWeight = $item->getWeight(); $rowWeight = $itemWeight * $item->getQty(); $addressWeight += $rowWeight; if ($address->getFreeShipping() || $item->getFreeShipping() === true) { $rowWeight = 0; } elseif (is_numeric($item->getFreeShipping())) { $freeQty = $item->getFreeShipping(); if ($item->getQty() > $freeQty) { $rowWeight = $itemWeight * ($item->getQty() - $freeQty); } else { $rowWeight = 0; } } $freeMethodWeight += $rowWeight; $item->setRowWeight($rowWeight); } } else { if (!$item->getProduct()->isVirtual()) { $addressQty += $item->getQty(); } $itemWeight = $item->getWeight(); $rowWeight = $itemWeight * $item->getQty(); $addressWeight += $rowWeight; if ($address->getFreeShipping() || $item->getFreeShipping() === true) { $rowWeight = 0; } elseif (is_numeric($item->getFreeShipping())) { $freeQty = $item->getFreeShipping(); if ($item->getQty() > $freeQty) { $rowWeight = $itemWeight * ($item->getQty() - $freeQty); } else { $rowWeight = 0; } } $freeMethodWeight += $rowWeight; $item->setRowWeight($rowWeight); } } if (isset($addressQty)) { $address->setItemQty($addressQty); } $address->setWeight($addressWeight); $address->setFreeMethodWeight($freeMethodWeight); $address->collectShippingRates(); if ($method) { foreach ($address->getAllShippingRates() as $rate) { if ($rate->getCode() == $method) { $store = $quote->getStore(); $amountPrice = $this->priceCurrency->convert($rate->getPrice(), $store); $total->setTotalAmount($this->getCode(), $amountPrice); $total->setBaseTotalAmount($this->getCode(), $rate->getPrice()); $shippingDescription = $rate->getCarrierTitle() . ' - ' . $rate->getMethodTitle(); $address->setShippingDescription(trim($shippingDescription, ' -')); break; } } } return $this; }