7
7
8
8
namespace Magento \WishlistGraphQl \Model ;
9
9
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 ;
11
13
use Magento \Wishlist \Model \ResourceModel \Wishlist as WishlistResourceModel ;
12
14
use Magento \Wishlist \Model \Wishlist ;
13
- use Magento \Wishlist \Model \Wishlist \Data \WishlistOutput ;
14
15
use 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 ;
16
19
17
20
/**
18
21
* Update wishlist items helper
19
22
*/
20
23
class UpdateWishlistItem
21
24
{
25
+ private const ERROR_UNDEFINED = 'UNDEFINED ' ;
26
+
22
27
/**
23
28
* @var WishlistResourceModel
24
29
*/
25
30
private $ wishlistResource ;
26
31
27
32
/**
28
- * @var array
33
+ * @var BuyRequestBuilder
29
34
*/
30
- private $ errors = [] ;
35
+ private $ buyRequestBuilder ;
31
36
32
37
/**
33
- * BuyRequestBuilder
34
- * @var BuyRequestBuilder $buyRequestBuilder
38
+ * @var array
35
39
*/
36
- private $ buyRequestBuilder ;
37
-
38
- private const ERROR_UNDEFINED = 'UNDEFINED ' ;
40
+ private $ errors = [];
39
41
40
42
/**
41
43
* @param WishlistResourceModel $wishlistResource
@@ -50,70 +52,92 @@ public function __construct(
50
52
}
51
53
52
54
/**
53
- * Update wishlist Item and set data from request
55
+ * Update wishlist item and set data from request
54
56
*
55
- * @param object $options
57
+ * @param WishlistItemData $wishlistItemData
56
58
* @param Wishlist $wishlist
57
59
*
58
60
* @return WishlistOutput
59
- * @throws GraphQlInputException
60
- * @throws \Magento\Framework\Exception\LocalizedException
61
+ * @throws LocalizedException
62
+ * @throws AlreadyExistsException
61
63
*/
62
- public function execute (object $ options , Wishlist $ wishlist )
64
+ public function execute (WishlistItemData $ wishlistItemData , Wishlist $ wishlist )
63
65
{
64
- $ itemId = $ options ->getId ();
65
- if ($ wishlist ->getItem ($ itemId ) == null ) {
66
+ $ wishlistItemId = (int ) $ wishlistItemData ->getId ();
67
+ $ wishlistItemToUpdate = $ wishlist ->getItem ($ wishlistItemId );
68
+
69
+ if (!$ wishlistItemToUpdate ) {
66
70
$ 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 ()
71
72
);
72
73
} 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 ());
112
80
}
81
+
82
+ $ this ->wishlistResource ->save ($ wishlist );
113
83
}
84
+
114
85
return $ this ->prepareOutput ($ wishlist );
115
86
}
116
87
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
+
117
141
/**
118
142
* Add wishlist line item error
119
143
*
@@ -124,7 +148,7 @@ public function execute(object $options, Wishlist $wishlist)
124
148
*/
125
149
private function addError (string $ message , string $ code = null ): void
126
150
{
127
- $ this ->errors [] = new Error (
151
+ $ this ->errors [] = new WishlistError (
128
152
$ message ,
129
153
$ code ?? self ::ERROR_UNDEFINED
130
154
);
0 commit comments