@@ -816,6 +816,7 @@ public function getCustomerGroupId()
816816 * @throws \Magento\Framework\Exception\LocalizedException
817817 * @SuppressWarnings(PHPMD.CyclomaticComplexity)
818818 * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
819+ * phpcs:disable Generic.Metrics.NestingLevel
819820 */
820821 public function moveQuoteItem ($ item , $ moveTo , $ qty )
821822 {
@@ -875,11 +876,15 @@ public function moveQuoteItem($item, $moveTo, $qty)
875876 );
876877 }
877878
878- $ cartItem = $ cart ->addProduct ($ product , $ info );
879- if (is_string ($ cartItem )) {
880- throw new \Magento \Framework \Exception \LocalizedException (__ ($ cartItem ));
879+ $ cartItems = $ cart ->getAllVisibleItems ();
880+ $ canBeRestored = (bool )$ this ->restoreTransferredItems ('cart ' , $ cartItems , $ product );
881+ if (!$ canBeRestored ) {
882+ $ cartItem = $ cart ->addProduct ($ product , $ info );
883+ if (is_string ($ cartItem )) {
884+ throw new \Magento \Framework \Exception \LocalizedException (__ ($ cartItem ));
885+ }
886+ $ cartItem ->setPrice ($ item ->getProduct ()->getPrice ());
881887 }
882- $ cartItem ->setPrice ($ item ->getProduct ()->getPrice ());
883888 $ this ->_needCollectCart = true ;
884889 $ removeItem = true ;
885890 }
@@ -922,7 +927,11 @@ public function moveQuoteItem($item, $moveTo, $qty)
922927 )->setStoreId (
923928 $ this ->getSession ()->getStoreId ()
924929 );
925- $ wishlist ->addNewItem ($ item ->getProduct (), $ info );
930+ $ wishlistItems = $ wishlist ->getItemCollection ()->getItems ();
931+ $ canBeRestored = (bool )$ this ->restoreTransferredItems ('wishlist ' , $ wishlistItems , null );
932+ if (!$ canBeRestored ) {
933+ $ wishlist ->addNewItem ($ item ->getProduct (), $ info );
934+ }
926935 $ removeItem = true ;
927936 }
928937 break ;
@@ -969,7 +978,7 @@ public function applySidebarData($data)
969978 if ($ item ) {
970979 $ this ->moveQuoteItem ($ item , 'order ' , $ qty );
971980 $ transferredItems = $ this ->_session ->getTransferredItems () ?? [];
972- $ transferredItems ['cart ' ][] = $ itemId ;
981+ $ transferredItems ['cart ' ][$ itemId ] = $ itemId ;
973982 $ this ->_session ->setTransferredItems ($ transferredItems ) ;
974983 }
975984 }
@@ -985,7 +994,7 @@ public function applySidebarData($data)
985994 if ($ item ->getId ()) {
986995 $ this ->addProduct ($ item ->getProduct (), $ item ->getBuyRequest ()->toArray ());
987996 $ transferredItems = $ this ->_session ->getTransferredItems () ?? [];
988- $ transferredItems ['wishlist ' ][] = $ itemId ;
997+ $ transferredItems ['wishlist ' ][$ itemId ] = $ itemId ;
989998 $ this ->_session ->setTransferredItems ($ transferredItems ) ;
990999 }
9911000 }
@@ -2051,6 +2060,43 @@ private function removeTransferredItems(): void
20512060 }
20522061 }
20532062
2063+ /**
2064+ * Restore items that were transferred from their original sources (cart, wishlist, ...) into ordered items
2065+ *
2066+ * @param string $area
2067+ * @param \Magento\Quote\Model\Quote\Item[]|\Magento\Wishlist\Model\Item[] $items
2068+ * @param \Magento\Catalog\Model\Product|null $product Product
2069+ * @return bool
2070+ */
2071+ private function restoreTransferredItems ($ area , $ items , $ product = null ): bool
2072+ {
2073+ $ transferredItems = $ this ->_session ->getTransferredItems () ?? [];
2074+ switch ($ area ) {
2075+ case 'wishlist ' :
2076+ $ transferredFromWishlist = array_intersect_key ($ items , $ transferredItems ['wishlist ' ]);
2077+ if ($ transferredFromWishlist ) {
2078+ $ wishlistItemId = array_key_first ($ transferredFromWishlist );
2079+ unset($ transferredItems ['wishlist ' ][$ wishlistItemId ]);
2080+ $ this ->_session ->setTransferredItems ($ transferredItems );
2081+ return true ;
2082+ }
2083+ break ;
2084+ case 'cart ' :
2085+ $ cart = $ this ->getCustomerCart ();
2086+ $ cartItem = $ cart ->getItemByProduct ($ product );
2087+ $ transferredFromCart = $ cartItem ? in_array ($ cartItem ->getId (), $ transferredItems ['cart ' ]) : false ;
2088+ if ($ transferredFromCart ) {
2089+ unset($ transferredItems ['cart ' ][$ cartItem ->getItemId ()]);
2090+ $ this ->_session ->setTransferredItems ($ transferredItems );
2091+ return true ;
2092+ }
2093+ break ;
2094+ default :
2095+ break ;
2096+ }
2097+ return false ;
2098+ }
2099+
20542100 /**
20552101 * Validate quote data before order creation
20562102 *
0 commit comments