12
12
use Magento \Wishlist \Model \Wishlist ;
13
13
use Magento \Wishlist \Model \Wishlist \Data \WishlistOutput ;
14
14
use Magento \Wishlist \Model \Wishlist \BuyRequest \BuyRequestBuilder ;
15
+ use Magento \Wishlist \Model \Wishlist \Data \Error ;
15
16
16
17
/**
17
18
* Update wishlist items helper
@@ -34,6 +35,8 @@ class UpdateWishlistItem
34
35
*/
35
36
private $ buyRequestBuilder ;
36
37
38
+ private const ERROR_UNDEFINED = 'UNDEFINED ' ;
39
+
37
40
/**
38
41
* @param WishlistResourceModel $wishlistResource
39
42
* @param BuyRequestBuilder $buyRequestBuilder
@@ -49,61 +52,84 @@ public function __construct(
49
52
/**
50
53
* Update wishlist Item and set data from request
51
54
*
52
- * @param int $itemId
53
55
* @param object $options
54
56
* @param Wishlist $wishlist
55
57
*
56
58
* @return WishlistOutput
57
59
* @throws GraphQlInputException
58
60
* @throws \Magento\Framework\Exception\LocalizedException
59
61
*/
60
- public function execute (int $ itemId , object $ options , Wishlist $ wishlist )
62
+ public function execute (object $ options , Wishlist $ wishlist )
61
63
{
62
- $ buyRequest = $ this ->buyRequestBuilder ->build ($ options );
63
- $ item = $ wishlist ->getItem ((int )$ itemId );
64
-
65
- if (!$ item ) {
66
- throw new GraphQlInputException (__ ('We can \'t specify a wish list item. ' ));
67
- }
68
-
69
- $ product = $ item ->getProduct ();
70
- $ productId = $ product ->getId ();
64
+ $ itemId = $ options ->getId ();
65
+ if ($ wishlist ->getItem ($ itemId ) == null ) {
66
+ $ this ->addError (
67
+ __ (
68
+ 'The wishlist item with ID "%id" does not belong to the wishlist ' ,
69
+ ['id ' => $ itemId ]
70
+ )->render ()
71
+ );
72
+ } else {
73
+ $ buyRequest = $ this ->buyRequestBuilder ->build ($ options );
74
+ $ item = $ wishlist ->getItem ((int )$ itemId );
75
+ $ product = $ item ->getProduct ();
76
+ $ productId = $ product ->getId ();
71
77
72
- if ($ productId ) {
73
- $ buyRequest ->setData ('action ' , 'updateItem ' );
74
- $ product ->setWishlistStoreId ($ item ->getStoreId ());
75
- $ cartCandidates = $ product ->getTypeInstance ()->processConfiguration ($ buyRequest , clone $ product );
78
+ if ($ productId ) {
79
+ $ buyRequest ->setData ('action ' , 'updateItem ' );
80
+ $ product ->setWishlistStoreId ($ item ->getStoreId ());
81
+ $ cartCandidates = $ product ->getTypeInstance ()->processConfiguration ($ buyRequest , clone $ product );
76
82
77
- /**
78
- * If the product with options existed or not
79
- */
80
- if (is_string ($ cartCandidates )) {
81
- throw new GraphQlInputException (__ ('The product with options does not exist. ' ));
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
+ }
83
89
84
- /**
85
- * If prepare process return one object
86
- */
87
- if (!is_array ($ cartCandidates )) {
88
- $ cartCandidates = [$ cartCandidates ];
89
- }
90
+ /**
91
+ * If prepare process return one object
92
+ */
93
+ if (!is_array ($ cartCandidates )) {
94
+ $ cartCandidates = [$ cartCandidates ];
95
+ }
90
96
91
- foreach ($ cartCandidates as $ candidate ) {
92
- if ($ candidate ->getParentProductId ()) {
93
- continue ;
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
+ }
94
108
}
95
- $ candidate ->setWishlistStoreId ($ item ->getStoreId ());
96
- $ qty = $ buyRequest ->getData ('qty ' ) ? $ buyRequest ->getData ('qty ' ) : 1 ;
97
- $ item ->setOptions ($ candidate ->getCustomOptions ());
98
- $ item ->setQty ($ qty );
109
+ $ this ->wishlistResource ->save ($ wishlist );
110
+ } else {
111
+ throw new GraphQlInputException (__ ('The product does not exist. ' ));
99
112
}
100
- $ this ->wishlistResource ->save ($ wishlist );
101
- } else {
102
- throw new GraphQlInputException (__ ('The product does not exist. ' ));
103
113
}
104
114
return $ this ->prepareOutput ($ wishlist );
105
115
}
106
116
117
+ /**
118
+ * Add wishlist line item error
119
+ *
120
+ * @param string $message
121
+ * @param string|null $code
122
+ *
123
+ * @return void
124
+ */
125
+ private function addError (string $ message , string $ code = null ): void
126
+ {
127
+ $ this ->errors [] = new Error (
128
+ $ message ,
129
+ $ code ?? self ::ERROR_UNDEFINED
130
+ );
131
+ }
132
+
107
133
/**
108
134
* Prepare output
109
135
*
@@ -119,3 +145,4 @@ private function prepareOutput(Wishlist $wishlist): WishlistOutput
119
145
return $ output ;
120
146
}
121
147
}
148
+
0 commit comments