Skip to content

Commit 6c633be

Browse files
committed
CartError interface implemented and tested.
1 parent 73d6418 commit 6c633be

14 files changed

+425
-45
lines changed

includes/class-type-registry.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public function init( \WPGraphQL\Registry\TypeRegistry $type_registry ) {
4141
\WPGraphQL\WooCommerce\Type\WPEnum\Products_Orderby_Enum::register();
4242
\WPGraphQL\WooCommerce\Type\WPEnum\Orders_Orderby_Enum::register();
4343
\WPGraphQL\WooCommerce\Type\WPEnum\Id_Type_Enums::register();
44+
\WPGraphQL\WooCommerce\Type\WPEnum\Cart_Error_Type::register();
4445

4546
// InputObjects.
4647
\WPGraphQL\WooCommerce\Type\WPInputObject\Cart_Item_Input::register();
@@ -60,6 +61,7 @@ public function init( \WPGraphQL\Registry\TypeRegistry $type_registry ) {
6061
// Interfaces.
6162
\WPGraphQL\WooCommerce\Type\WPInterface\Product::register_interface( $type_registry );
6263
\WPGraphQL\WooCommerce\Type\WPInterface\Product_Attribute::register_interface( $type_registry );
64+
\WPGraphQL\WooCommerce\Type\WPInterface\Cart_Error::register_interface( $type_registry );
6365

6466
// Objects.
6567
\WPGraphQL\WooCommerce\Type\WPObject\Coupon_Type::register();
@@ -81,6 +83,7 @@ public function init( \WPGraphQL\Registry\TypeRegistry $type_registry ) {
8183
\WPGraphQL\WooCommerce\Type\WPObject\Meta_Data_Type::register();
8284
\WPGraphQL\WooCommerce\Type\WPObject\Shipping_Package_Type::register();
8385
\WPGraphQL\WooCommerce\Type\WPObject\Shipping_Rate_Type::register();
86+
\WPGraphQL\WooCommerce\Type\WPObject\Cart_Error_Types::register();
8487

8588
// Object fields.
8689
\WPGraphQL\WooCommerce\Type\WPObject\Product_Category_Type::register_fields();

includes/mutation/class-cart-add-items.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public static function get_input_fields() {
5454
*/
5555
public static function get_output_fields() {
5656
return array(
57-
'added' => array(
57+
'added' => array(
5858
'type' => array( 'list_of' => 'CartItem' ),
5959
'resolve' => function ( $payload ) {
6060
$items = array();
@@ -65,7 +65,27 @@ public static function get_output_fields() {
6565
return $items;
6666
},
6767
),
68-
'cart' => Cart_Mutation::get_cart_field( true ),
68+
'cartErrors' => array(
69+
'type' => array( 'list_of' => 'CartItemError' ),
70+
'resolve' => function ( $payload ) {
71+
$errors = array();
72+
foreach ( $payload['failure'] as $error_data ) {
73+
$cart_error = $error_data['cart_item_data'];
74+
$cart_error['type'] = 'INVALID_CART_ITEM';
75+
76+
if ( ! empty( $error_data['reasons'] ) ) {
77+
$cart_error['reasons'] = $error_data['reasons'];
78+
} elseif ( $error_data['reason'] ) {
79+
$cart_error['reasons'] = array( $error_data['reason'] );
80+
}
81+
82+
$errors[] = $cart_error;
83+
}
84+
85+
return $errors;
86+
},
87+
),
88+
'cart' => Cart_Mutation::get_cart_field( true ),
6989
);
7090
}
7191

@@ -103,7 +123,7 @@ public static function mutate_and_get_payload() {
103123
// Else capture errors.
104124
$notices = \WC()->session->get( 'wc_notices' );
105125
if ( ! empty( $notices['error'] ) ) {
106-
$reasons = implode( ' ', array_column( $notices['error'], 'notice' ) );
126+
$reasons = array_column( $notices['error'], 'notice' );
107127
\wc_clear_notices();
108128

109129
$failure[] = compact( 'cart_item_data', 'reasons' );
@@ -131,7 +151,7 @@ public static function mutate_and_get_payload() {
131151
}
132152

133153
// Return payload.
134-
return compact( 'added' );
154+
return compact( 'added', 'failure' );
135155
};
136156
}
137157
}

includes/mutation/class-cart-fill.php

Lines changed: 66 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* WPEnum Type - CartErrorType
4+
*
5+
* @package WPGraphQL\WooCommerce\Type\WPEnum
6+
* @since 0.7.0
7+
*/
8+
9+
namespace WPGraphQL\WooCommerce\Type\WPEnum;
10+
11+
/**
12+
* Class Cart_Error_Type
13+
*/
14+
class Cart_Error_Type {
15+
/**
16+
* Registers type
17+
*/
18+
public static function register() {
19+
$values = array(
20+
'INVALID_CART_ITEM' => array( 'value' => 'INVALID_CART_ITEM' ),
21+
'INVALID_COUPON' => array( 'value' => 'INVALID_COUPON' ),
22+
'INVALID_SHIPPING_METHOD' => array( 'value' => 'INVALID_SHIPPING_METHOD' ),
23+
);
24+
25+
register_graphql_enum_type(
26+
'CartErrorType',
27+
array(
28+
'description' => __( 'Cart error type enumeration', 'wp-graphql-woocommerce' ),
29+
'values' => $values,
30+
)
31+
);
32+
}
33+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
/**
3+
* WPInterface Type - Cart_Error
4+
*
5+
* @package WPGraphQL\WooCommerce\Type\WPInterface
6+
* @since 0.7.0
7+
*/
8+
9+
namespace WPGraphQL\WooCommerce\Type\WPInterface;
10+
11+
/**
12+
* Class Cart_Error
13+
*/
14+
class Cart_Error {
15+
16+
/**
17+
* Registers the "CartError" interface.
18+
*
19+
* @param \WPGraphQL\Registry\TypeRegistry $type_registry Instance of the WPGraphQL TypeRegistry.
20+
*/
21+
public static function register_interface( &$type_registry ) {
22+
register_graphql_interface_type(
23+
'CartError',
24+
array(
25+
'description' => __( 'An error that occurred when updating the cart', 'wp-graphql-woocommerce' ),
26+
'fields' => self::get_fields(),
27+
'resolveType' => function( array $value ) use ( &$type_registry ) {
28+
switch ( $value['type'] ) {
29+
case 'INVALID_CART_ITEM':
30+
return $type_registry->get_type( 'CartItemError' );
31+
case 'INVALID_COUPON':
32+
return $type_registry->get_type( 'CouponError' );
33+
case 'INVALID_SHIPPING_METHOD':
34+
return $type_registry->get_type( 'ShippingMethodError' );
35+
}
36+
},
37+
)
38+
);
39+
}
40+
41+
/**
42+
* Defines CartError field. All child type must have these fields as well.
43+
*
44+
* @return array
45+
*/
46+
public static function get_fields() {
47+
return array(
48+
'type' => array(
49+
'type' => array( 'non_null' => 'CartErrorType' ),
50+
'description' => __( 'Type of error', 'wp-graphql-woocommerce' ),
51+
'resolve' => function ( array $error ) {
52+
return ! empty( $error['type'] ) ? $error['type'] : null;
53+
},
54+
),
55+
'reasons' => array(
56+
'type' => array( 'list_of' => 'String' ),
57+
'description' => __( 'Reason for error', 'wp-graphql-woocommerce' ),
58+
'resolve' => function ( $error ) {
59+
return ! empty( $error['reasons'] ) ? $error['reasons'] : array( 'Reasons for error unknown, sorry.' );
60+
},
61+
),
62+
);
63+
}
64+
}

includes/type/interface/class-product-attribute.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ public static function register_interface( &$type_registry ) {
3636
}
3737

3838
/**
39-
* Defines product fields. All child type must have these fields as well.
39+
* Defines ProductAttribute fields. All child type must have these fields as well.
40+
*
41+
* @return array
4042
*/
4143
public static function get_fields() {
4244
return array(

includes/type/interface/class-product.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ public static function register_interface( &$type_registry ) {
106106
}
107107

108108
/**
109-
* Defines product fields. All child type must have these fields as well.
109+
* Defines Product fields. All child type must have these fields as well.
110+
*
111+
* @return array
110112
*/
111113
public static function get_fields() {
112114
return array(

0 commit comments

Comments
 (0)