@@ -816,6 +816,7 @@ public function getCustomerGroupId()
816
816
* @throws \Magento\Framework\Exception\LocalizedException
817
817
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
818
818
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
819
+ * phpcs:disable Generic.Metrics.NestingLevel
819
820
*/
820
821
public function moveQuoteItem ($ item , $ moveTo , $ qty )
821
822
{
@@ -875,11 +876,15 @@ public function moveQuoteItem($item, $moveTo, $qty)
875
876
);
876
877
}
877
878
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 ());
881
887
}
882
- $ cartItem ->setPrice ($ item ->getProduct ()->getPrice ());
883
888
$ this ->_needCollectCart = true ;
884
889
$ removeItem = true ;
885
890
}
@@ -922,7 +927,11 @@ public function moveQuoteItem($item, $moveTo, $qty)
922
927
)->setStoreId (
923
928
$ this ->getSession ()->getStoreId ()
924
929
);
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
+ }
926
935
$ removeItem = true ;
927
936
}
928
937
break ;
@@ -969,7 +978,7 @@ public function applySidebarData($data)
969
978
if ($ item ) {
970
979
$ this ->moveQuoteItem ($ item , 'order ' , $ qty );
971
980
$ transferredItems = $ this ->_session ->getTransferredItems () ?? [];
972
- $ transferredItems ['cart ' ][] = $ itemId ;
981
+ $ transferredItems ['cart ' ][$ itemId ] = $ itemId ;
973
982
$ this ->_session ->setTransferredItems ($ transferredItems ) ;
974
983
}
975
984
}
@@ -985,7 +994,7 @@ public function applySidebarData($data)
985
994
if ($ item ->getId ()) {
986
995
$ this ->addProduct ($ item ->getProduct (), $ item ->getBuyRequest ()->toArray ());
987
996
$ transferredItems = $ this ->_session ->getTransferredItems () ?? [];
988
- $ transferredItems ['wishlist ' ][] = $ itemId ;
997
+ $ transferredItems ['wishlist ' ][$ itemId ] = $ itemId ;
989
998
$ this ->_session ->setTransferredItems ($ transferredItems ) ;
990
999
}
991
1000
}
@@ -2051,6 +2060,43 @@ private function removeTransferredItems(): void
2051
2060
}
2052
2061
}
2053
2062
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
+
2054
2100
/**
2055
2101
* Validate quote data before order creation
2056
2102
*
0 commit comments