@@ -88,6 +88,47 @@ public static function get_output_fields() {
8888 return ! empty ( $ methods ) ? $ methods : null ;
8989 },
9090 ),
91+ 'cartErrors ' => array (
92+ 'type ' => array ( 'list_of ' => 'CartError ' ),
93+ 'resolve ' => function ( $ payload ) {
94+ $ errors = array ();
95+ $ all_error_data = array_merge (
96+ $ payload ['invalid_cart_items ' ],
97+ $ payload ['invalid_coupons ' ],
98+ $ payload ['invalid_shipping_methods ' ]
99+ );
100+
101+ foreach ( $ all_error_data as $ error_data ) {
102+ switch ( true ) {
103+ case isset ( $ error_data ['cart_item_data ' ] ):
104+ $ cart_error = $ error_data ['cart_item_data ' ];
105+ $ cart_error ['type ' ] = 'INVALID_CART_ITEM ' ;
106+ break ;
107+ case isset ( $ error_data ['code ' ] ):
108+ $ cart_error = array ( 'code ' => $ error_data ['code ' ] );
109+ $ cart_error ['type ' ] = 'INVALID_COUPON ' ;
110+ break ;
111+ case isset ( $ error_data ['package ' ] ):
112+ $ cart_error = array (
113+ 'package ' => $ error_data ['package ' ],
114+ 'chosen_method ' => $ error_data ['chosen_method ' ],
115+ );
116+ $ cart_error ['type ' ] = 'INVALID_SHIPPING_METHOD ' ;
117+ break ;
118+ }
119+
120+ if ( ! empty ( $ error_data ['reasons ' ] ) ) {
121+ $ cart_error ['reasons ' ] = $ error_data ['reasons ' ];
122+ } elseif ( $ error_data ['reason ' ] ) {
123+ $ cart_error ['reasons ' ] = array ( $ error_data ['reason ' ] );
124+ }
125+
126+ $ errors [] = $ cart_error ;
127+ }
128+
129+ return $ errors ;
130+ },
131+ ),
91132 'cart ' => Cart_Mutation::get_cart_field ( true ),
92133 );
93134 }
@@ -107,8 +148,8 @@ public static function mutate_and_get_payload() {
107148 }
108149
109150 // Validate cart item input.
110- $ added = array ();
111- $ failure = array ();
151+ $ added = array ();
152+ $ invalid_cart_items = array ();
112153 foreach ( $ input ['items ' ] as $ cart_item_data ) {
113154 try {
114155 // Prepare args for "add_to_cart" from input data.
@@ -126,26 +167,26 @@ public static function mutate_and_get_payload() {
126167 // Else capture errors.
127168 $ notices = \WC ()->session ->get ( 'wc_notices ' );
128169 if ( ! empty ( $ notices ['error ' ] ) ) {
129- $ reasons = implode ( ' ' , array_column ( $ notices ['error ' ], 'notice ' ) );
170+ $ reasons = array_column ( $ notices ['error ' ], 'notice ' );
130171 \wc_clear_notices ();
131172
132- $ failure [] = compact ( 'cart_item_data ' , 'reasons ' );
173+ $ invalid_cart_items [] = compact ( 'cart_item_data ' , 'reasons ' );
133174 } else {
134- $ reason = __ ( 'Failed to add cart item. Please check input. ' , 'wp-graphql-woocommerce ' );
135- $ failure [] = compact ( 'cart_item_data ' , 'reason ' );
175+ $ reason = __ ( 'Failed to add cart item. Please check input. ' , 'wp-graphql-woocommerce ' );
176+ $ invalid_cart_items [] = compact ( 'cart_item_data ' , 'reason ' );
136177 }
137178 } catch ( \Exception $ e ) {
138179 // Get thrown error message.
139180 $ reason = $ e ->getMessage ();
140181
141182 // Capture error.
142- $ failure [] = compact ( 'cart_item_data ' , 'reason ' );
183+ $ invalid_cart_items [] = compact ( 'cart_item_data ' , 'reason ' );
143184 }
144185 }
145186
146187 // Log captured errors.
147- if ( ! empty ( $ failure ) ) {
148- graphql_debug ( $ failure , array ( 'type ' => 'INVALID_CART_ITEMS ' ) );
188+ if ( ! empty ( $ invalid_cart_items ) ) {
189+ graphql_debug ( $ invalid_cart_items , array ( 'type ' => 'INVALID_CART_ITEMS ' ) );
149190 }
150191
151192 // Throw error, if no items added.
@@ -155,7 +196,7 @@ public static function mutate_and_get_payload() {
155196
156197 $ applied = array ();
157198 if ( ! empty ( $ input ['coupons ' ] ) ) {
158- $ failure = array ();
199+ $ invalid_coupons = array ();
159200 foreach ( $ input ['coupons ' ] as $ code ) {
160201 $ reason = '' ;
161202 // If validate and successful applied to cart, return payload.
@@ -175,11 +216,11 @@ public static function mutate_and_get_payload() {
175216 $ reason = __ ( 'Failed to apply coupon. Check for an individual-use coupon on cart. ' , 'wp-graphql-woocommerce ' );
176217 }
177218
178- $ failure [] = compact ( 'code ' , 'reason ' );
219+ $ invalid_coupons [] = compact ( 'code ' , 'reason ' );
179220 }
180221
181- if ( ! empty ( $ failure ) ) {
182- graphql_debug ( $ failure , array ( 'type ' => 'INVALID_COUPONS ' ) );
222+ if ( ! empty ( $ invalid_coupons ) ) {
223+ graphql_debug ( $ invalid_coupons , array ( 'type ' => 'INVALID_COUPONS ' ) );
183224 }
184225 }
185226
@@ -191,7 +232,7 @@ public static function mutate_and_get_payload() {
191232 $ chosen_shipping_methods = \WC ()->session ->get ( 'chosen_shipping_methods ' );
192233
193234 // Update current shipping methods.
194- $ failure = array ();
235+ $ invalid_shipping_methods = array ();
195236 foreach ( $ posted_shipping_methods as $ package => $ chosen_method ) {
196237 if ( empty ( $ chosen_method ) ) {
197238 continue ;
@@ -203,22 +244,29 @@ public static function mutate_and_get_payload() {
203244 continue ;
204245 }
205246
206- $ failure [] = compact ( 'package ' , 'chosen_method ' , 'reason ' );
247+ $ invalid_shipping_methods [] = compact ( 'package ' , 'chosen_method ' , 'reason ' );
207248 }
208249
209250 // Set updated shipping methods in session.
210251 \WC ()->session ->set ( 'chosen_shipping_methods ' , $ chosen_shipping_methods );
211252
212- if ( ! empty ( $ failure ) ) {
213- graphql_debug ( $ failure , array ( 'type ' => 'INVALID_SHIPPING_METHODS ' ) );
253+ if ( ! empty ( $ invalid_shipping_methods ) ) {
254+ graphql_debug ( $ invalid_shipping_methods , array ( 'type ' => 'INVALID_SHIPPING_METHODS ' ) );
214255 }
215256 }
216257
217258 // Recalculate totals.
218259 \WC ()->cart ->calculate_totals ();
219260
220261 // Return payload.
221- return compact ( 'added ' , 'applied ' , 'chosen_shipping_methods ' );
262+ return compact (
263+ 'added ' ,
264+ 'invalid_cart_items ' ,
265+ 'applied ' ,
266+ 'invalid_coupons ' ,
267+ 'chosen_shipping_methods ' ,
268+ 'invalid_shipping_methods '
269+ );
222270 };
223271 }
224272}
0 commit comments