1212use Magento \Wishlist \Model \Wishlist ;
1313use Magento \Wishlist \Model \Wishlist \Data \WishlistOutput ;
1414use Magento \Wishlist \Model \Wishlist \BuyRequest \BuyRequestBuilder ;
15+ use Magento \Wishlist \Model \Wishlist \Data \Error ;
1516
1617/**
1718 * Update wishlist items helper
@@ -34,6 +35,8 @@ class UpdateWishlistItem
3435 */
3536 private $ buyRequestBuilder ;
3637
38+ private const ERROR_UNDEFINED = 'UNDEFINED ' ;
39+
3740 /**
3841 * @param WishlistResourceModel $wishlistResource
3942 * @param BuyRequestBuilder $buyRequestBuilder
@@ -49,61 +52,84 @@ public function __construct(
4952 /**
5053 * Update wishlist Item and set data from request
5154 *
52- * @param int $itemId
5355 * @param object $options
5456 * @param Wishlist $wishlist
5557 *
5658 * @return WishlistOutput
5759 * @throws GraphQlInputException
5860 * @throws \Magento\Framework\Exception\LocalizedException
5961 */
60- public function execute (int $ itemId , object $ options , Wishlist $ wishlist )
62+ public function execute (object $ options , Wishlist $ wishlist )
6163 {
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 ();
7177
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 );
7682
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+ }
8389
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+ }
9096
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+ }
94108 }
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. ' ));
99112 }
100- $ this ->wishlistResource ->save ($ wishlist );
101- } else {
102- throw new GraphQlInputException (__ ('The product does not exist. ' ));
103113 }
104114 return $ this ->prepareOutput ($ wishlist );
105115 }
106116
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+
107133 /**
108134 * Prepare output
109135 *
@@ -119,3 +145,4 @@ private function prepareOutput(Wishlist $wishlist): WishlistOutput
119145 return $ output ;
120146 }
121147}
148+
0 commit comments