diff --git a/app/code/Magento/Multishipping/Model/Checkout/Type/Multishipping.php b/app/code/Magento/Multishipping/Model/Checkout/Type/Multishipping.php index b56d7b532df76e618588ade7e7fe563cdca47087..a1d30e3d83e341eb09104b40c937c8a0bd4ea093 100644 --- a/app/code/Magento/Multishipping/Model/Checkout/Type/Multishipping.php +++ b/app/code/Magento/Multishipping/Model/Checkout/Type/Multishipping.php @@ -289,35 +289,8 @@ class Multishipping extends \Magento\Framework\DataObject if ($this->_quoteShippingAddressesItems !== null) { return $this->_quoteShippingAddressesItems; } - $items = []; - $addresses = $this->getQuote()->getAllAddresses(); - foreach ($addresses as $address) { - foreach ($address->getAllItems() as $item) { - if ($item->getParentItemId()) { - continue; - } - if ($item->getProduct()->getIsVirtual()) { - $items[] = $item; - continue; - } - if ($item->getQty() > 1) { - for ($i = 0, $n = $item->getQty(); $i < $n; $i++) { - if ($i == 0) { - $addressItem = $item; - } else { - $addressItem = clone $item; - } - $addressItem->setQty(1)->setCustomerAddressId($address->getCustomerAddressId())->save(); - $items[] = $addressItem; - } - } else { - $item->setCustomerAddressId($address->getCustomerAddressId()); - $items[] = $item; - } - } - } - $this->_quoteShippingAddressesItems = $items; - return $items; + $this->_quoteShippingAddressesItems = $this->getQuote()->getShippingAddressesItems(); + return $this->_quoteShippingAddressesItems; } /** diff --git a/app/code/Magento/Multishipping/Test/Unit/Model/Checkout/Type/MultishippingTest.php b/app/code/Magento/Multishipping/Test/Unit/Model/Checkout/Type/MultishippingTest.php index cb72f0b778a04bc35e2b1c3a754dd5029aa152eb..288cdeba12c69689619fa14923621e28dc4c8ee6 100644 --- a/app/code/Magento/Multishipping/Test/Unit/Model/Checkout/Type/MultishippingTest.php +++ b/app/code/Magento/Multishipping/Test/Unit/Model/Checkout/Type/MultishippingTest.php @@ -260,4 +260,12 @@ class MultishippingTest extends \PHPUnit_Framework_TestCase $this->assertEquals($this->model, $this->model->setQuoteCustomerBillingAddress($addressId)); } + + public function testGetQuoteShippingAddressesItems() + { + $quoteItem = $this->getMock('Magento\Quote\Model\Quote\Address\Item', [], [], '', false); + $this->checkoutSessionMock->expects($this->once())->method('getQuote')->willReturn($this->quoteMock); + $this->quoteMock->expects($this->once())->method('getShippingAddressesItems')->willReturn($quoteItem); + $this->model->getQuoteShippingAddressesItems(); + } } diff --git a/app/code/Magento/Quote/Model/Quote.php b/app/code/Magento/Quote/Model/Quote.php index 29779c1e22ab2d74badb06587ae4b4bd9782333d..0e5880f76a26f582da420637192b6087f25cdee7 100644 --- a/app/code/Magento/Quote/Model/Quote.php +++ b/app/code/Magento/Quote/Model/Quote.php @@ -344,6 +344,13 @@ class Quote extends AbstractExtensibleModel implements \Magento\Quote\Api\Data\C */ protected $shippingAssignmentFactory; + /** + * Quote shipping addresses items cache + * + * @var array + */ + protected $shippingAddressesItems; + /** * @param \Magento\Framework\Model\Context $context * @param \Magento\Framework\Registry $registry @@ -2413,6 +2420,48 @@ class Quote extends AbstractExtensibleModel implements \Magento\Quote\Api\Data\C return $this->_getData(self::KEY_CHECKOUT_METHOD); } + /** + * Get quote items assigned to different quote addresses populated per item qty. + * Based on result array we can display each item separately + * + * @return array + */ + public function getShippingAddressesItems() + { + if ($this->shippingAddressesItems !== null) { + return $this->shippingAddressesItems; + } + $items = []; + $addresses = $this->getAllAddresses(); + foreach ($addresses as $address) { + foreach ($address->getAllItems() as $item) { + if ($item->getParentItemId()) { + continue; + } + if ($item->getProduct()->getIsVirtual()) { + $items[] = $item; + continue; + } + if ($item->getQty() > 1) { + for ($itemIndex = 0, $itemQty = $item->getQty(); $itemIndex < $itemQty; $itemIndex++) { + if ($itemIndex == 0) { + $addressItem = $item; + } else { + $addressItem = clone $item; + } + $addressItem->setQty(1)->setCustomerAddressId($address->getCustomerAddressId())->save(); + $items[] = $addressItem; + } + } else { + $item->setCustomerAddressId($address->getCustomerAddressId()); + $items[] = $item; + } + } + } + $this->shippingAddressesItems = $items; + return $items; + } + /** * Sets the payment method that is used to process the cart. *