From 651bf2cae4ccf01b95292d413b7b895760ae594f Mon Sep 17 00:00:00 2001
From: Iryna Lagno <ilagno@ebay.com>
Date: Wed, 28 Oct 2015 11:42:52 +0200
Subject: [PATCH] MAGETWO-39862: Can't disable Multishipping module

---
 .../Model/Checkout/Type/Multishipping.php     | 31 +-----------
 .../Model/Checkout/Type/MultishippingTest.php |  8 +++
 app/code/Magento/Quote/Model/Quote.php        | 49 +++++++++++++++++++
 3 files changed, 59 insertions(+), 29 deletions(-)

diff --git a/app/code/Magento/Multishipping/Model/Checkout/Type/Multishipping.php b/app/code/Magento/Multishipping/Model/Checkout/Type/Multishipping.php
index b56d7b532df..a1d30e3d83e 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 cb72f0b778a..288cdeba12c 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 29779c1e22a..0e5880f76a2 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.
      *
-- 
GitLab