77
88namespace Magento \WishlistGraphQl \Model ;
99
10- use Magento \Framework \GraphQl \Exception \GraphQlInputException ;
10+ use Magento \Framework \Exception \AlreadyExistsException ;
11+ use Magento \Framework \Exception \LocalizedException ;
12+ use Magento \Wishlist \Model \Item ;
1113use Magento \Wishlist \Model \ResourceModel \Wishlist as WishlistResourceModel ;
1214use Magento \Wishlist \Model \Wishlist ;
13- use Magento \Wishlist \Model \Wishlist \Data \WishlistOutput ;
1415use Magento \Wishlist \Model \Wishlist \BuyRequest \BuyRequestBuilder ;
15- use Magento \Wishlist \Model \Wishlist \Data \Error ;
16+ use Magento \Wishlist \Model \Wishlist \Data \Error as WishlistError ;
17+ use Magento \Wishlist \Model \Wishlist \Data \WishlistItem as WishlistItemData ;
18+ use Magento \Wishlist \Model \Wishlist \Data \WishlistOutput ;
1619
1720/**
1821 * Update wishlist items helper
1922 */
2023class UpdateWishlistItem
2124{
25+ private const ERROR_UNDEFINED = 'UNDEFINED ' ;
26+
2227 /**
2328 * @var WishlistResourceModel
2429 */
2530 private $ wishlistResource ;
2631
2732 /**
28- * @var array
33+ * @var BuyRequestBuilder
2934 */
30- private $ errors = [] ;
35+ private $ buyRequestBuilder ;
3136
3237 /**
33- * BuyRequestBuilder
34- * @var BuyRequestBuilder $buyRequestBuilder
38+ * @var array
3539 */
36- private $ buyRequestBuilder ;
37-
38- private const ERROR_UNDEFINED = 'UNDEFINED ' ;
40+ private $ errors = [];
3941
4042 /**
4143 * @param WishlistResourceModel $wishlistResource
@@ -50,70 +52,92 @@ public function __construct(
5052 }
5153
5254 /**
53- * Update wishlist Item and set data from request
55+ * Update wishlist item and set data from request
5456 *
55- * @param object $options
57+ * @param WishlistItemData $wishlistItemData
5658 * @param Wishlist $wishlist
5759 *
5860 * @return WishlistOutput
59- * @throws GraphQlInputException
60- * @throws \Magento\Framework\Exception\LocalizedException
61+ * @throws LocalizedException
62+ * @throws AlreadyExistsException
6163 */
62- public function execute (object $ options , Wishlist $ wishlist )
64+ public function execute (WishlistItemData $ wishlistItemData , Wishlist $ wishlist )
6365 {
64- $ itemId = $ options ->getId ();
65- if ($ wishlist ->getItem ($ itemId ) == null ) {
66+ $ wishlistItemId = (int ) $ wishlistItemData ->getId ();
67+ $ wishlistItemToUpdate = $ wishlist ->getItem ($ wishlistItemId );
68+
69+ if (!$ wishlistItemToUpdate ) {
6670 $ this ->addError (
67- __ (
68- 'The wishlist item with ID "%id" does not belong to the wishlist ' ,
69- ['id ' => $ itemId ]
70- )->render ()
71+ __ ('Could not find the wishlist item with ID "%1" ' , $ wishlistItemId )->render ()
7172 );
7273 } else {
73- $ buyRequest = $ this ->buyRequestBuilder ->build ($ options );
74- $ item = $ wishlist ->getItem ((int )$ itemId );
75- $ product = $ item ->getProduct ();
76- $ productId = $ product ->getId ();
77-
78- if ($ productId ) {
79- $ buyRequest ->setData ('action ' , 'updateItem ' );
80- $ product ->setWishlistStoreId ($ item ->getStoreId ());
81- $ cartCandidates = $ product ->getTypeInstance ()->processConfiguration ($ buyRequest , clone $ product );
82-
83- /**
84- * If the product with options existed or not
85- */
86- if (is_string ($ cartCandidates )) {
87- throw new GraphQlInputException (__ ('The product with options does not exist. ' ));
88- }
89-
90- /**
91- * If prepare process return one object
92- */
93- if (!is_array ($ cartCandidates )) {
94- $ cartCandidates = [$ cartCandidates ];
95- }
96-
97- foreach ($ cartCandidates as $ candidate ) {
98- if ($ candidate ->getParentProductId ()) {
99- continue ;
100- }
101- $ candidate ->setWishlistStoreId ($ item ->getStoreId ());
102- $ qty = $ buyRequest ->getData ('qty ' ) ? $ buyRequest ->getData ('qty ' ) : 1 ;
103- $ item ->setOptions ($ candidate ->getCustomOptions ());
104- $ item ->setQty ($ qty );
105- if ($ options ->getDescription ()) {
106- $ item ->setDescription ($ options ->getDescription ());
107- }
108- }
109- $ this ->wishlistResource ->save ($ wishlist );
110- } else {
111- throw new GraphQlInputException (__ ('The product does not exist. ' ));
74+ $ updatedOptions = $ this ->getUpdatedOptions ($ wishlistItemData , $ wishlistItemToUpdate );
75+
76+ $ wishlistItemToUpdate ->setOptions ($ updatedOptions );
77+ $ wishlistItemToUpdate ->setQty ($ wishlistItemData ->getQuantity ());
78+ if ($ wishlistItemData ->getDescription ()) {
79+ $ wishlistItemToUpdate ->setDescription ($ wishlistItemData ->getDescription ());
11280 }
81+
82+ $ this ->wishlistResource ->save ($ wishlist );
11383 }
84+
11485 return $ this ->prepareOutput ($ wishlist );
11586 }
11687
88+ /**
89+ * Build the updated options for the specified wishlist item.
90+ *
91+ * @param WishlistItemData $wishlistItemData
92+ * @param Item $wishlistItemToUpdate
93+ * @return array
94+ * @throws LocalizedException
95+ */
96+ private function getUpdatedOptions (WishlistItemData $ wishlistItemData , Item $ wishlistItemToUpdate )
97+ {
98+ $ wishlistItemId = $ wishlistItemToUpdate ->getId ();
99+ $ wishlistItemProduct = $ wishlistItemToUpdate ->getProduct ();
100+
101+ if (!$ wishlistItemProduct ->getId ()) {
102+ throw new LocalizedException (
103+ __ ('Could not find product for the wishlist item with ID "%1" ' , $ wishlistItemId )
104+ );
105+ }
106+
107+ // Create a buy request with the updated wishlist item data
108+ $ updatedBuyRequest = $ this ->buyRequestBuilder
109+ ->build ($ wishlistItemData )
110+ ->setData ('action ' , 'updateItem ' );
111+
112+ // Get potential products to add to the cart for the product type using the updated buy request
113+ $ wishlistItemProduct ->setWishlistStoreId ($ wishlistItemToUpdate ->getStoreId ());
114+ $ cartCandidates = $ wishlistItemProduct ->getTypeInstance ()->processConfiguration (
115+ $ updatedBuyRequest ,
116+ clone $ wishlistItemProduct
117+ );
118+
119+ if (is_string ($ cartCandidates )) {
120+ throw new LocalizedException (
121+ __ ('Could not prepare product for the wishlist item with ID %1 ' , $ wishlistItemId )
122+ );
123+ }
124+
125+ // Of the cart candidates, find the parent product and get its options
126+ if (!is_array ($ cartCandidates )) {
127+ $ cartCandidates = [$ cartCandidates ];
128+ }
129+ $ updatedOptions = [];
130+ foreach ($ cartCandidates as $ candidate ) {
131+ if ($ candidate ->getParentProductId () === null ) {
132+ $ candidate ->setWishlistStoreId ($ wishlistItemToUpdate ->getStoreId ());
133+ $ updatedOptions = $ candidate ->getCustomOptions ();
134+ break ;
135+ }
136+ }
137+
138+ return $ updatedOptions ;
139+ }
140+
117141 /**
118142 * Add wishlist line item error
119143 *
@@ -124,7 +148,7 @@ public function execute(object $options, Wishlist $wishlist)
124148 */
125149 private function addError (string $ message , string $ code = null ): void
126150 {
127- $ this ->errors [] = new Error (
151+ $ this ->errors [] = new WishlistError (
128152 $ message ,
129153 $ code ?? self ::ERROR_UNDEFINED
130154 );
0 commit comments