Skip to content

Commit 80252b0

Browse files
authored
feat: Country queries implemented. (#736)
* feat: Country queries implemented. * chore: WPCS compliance met and late-minute renames.
1 parent 27d9297 commit 80252b0

File tree

6 files changed

+196
-10
lines changed

6 files changed

+196
-10
lines changed

includes/class-type-registry.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public function init( \WPGraphQL\Registry\TypeRegistry $type_registry ) {
8888
Type\WPObject\Shipping_Rate_Type::register();
8989
Type\WPObject\Cart_Error_Types::register();
9090
Type\WPObject\Payment_Token_Types::register();
91+
Type\WPObject\Country_State_Type::register();
9192

9293
// Object fields.
9394
Type\WPObject\Product_Category_Type::register_fields();

includes/class-wp-graphql-woocommerce.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ private function includes() {
258258
require $include_directory_path . 'type/object/class-tax-rate-type.php';
259259
require $include_directory_path . 'type/object/class-variation-attribute-type.php';
260260
require $include_directory_path . 'type/object/class-payment-token-types.php';
261+
require $include_directory_path . 'type/object/class-country-state-type.php';
261262

262263
// Include input type class files.
263264
require $include_directory_path . 'type/input/class-cart-item-input.php';
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* WPObject Type - Country_State_Type
4+
*
5+
* Registers CountryState WPObject type
6+
*
7+
* @package WPGraphQL\WooCommerce\Type\WPObject
8+
* @since 0.12.4
9+
*/
10+
11+
namespace WPGraphQL\WooCommerce\Type\WPObject;
12+
13+
/**
14+
* Class Country_State_Type
15+
*/
16+
class Country_State_Type {
17+
18+
/**
19+
* Registers type
20+
*/
21+
public static function register() {
22+
register_graphql_object_type(
23+
'CountryState',
24+
[
25+
'description' => __( 'shipping country state object', 'wp-graphql-woocommerce' ),
26+
'fields' => [
27+
'code' => [
28+
'type' => [ 'non_null' => 'String' ],
29+
'description' => __( 'Country state code', 'wp-graphql-woocommerce' ),
30+
'resolve' => function ( $source ) {
31+
return ! empty( $source['code'] ) ? $source['code'] : null;
32+
},
33+
],
34+
'name' => [
35+
'type' => [ 'non_null' => 'String' ],
36+
'description' => __( 'Country state name', 'wp-graphql-woocommerce' ),
37+
'resolve' => function ( $source ) {
38+
return ! empty( $source['name'] ) ? $source['name'] : null;
39+
},
40+
],
41+
],
42+
]
43+
);
44+
}
45+
}

includes/type/object/class-root-query.php

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static function register_fields() {
2626
register_graphql_fields(
2727
'RootQuery',
2828
[
29-
'cart' => [
29+
'cart' => [
3030
'type' => 'Cart',
3131
'args' => [
3232
'recalculateTotals' => [
@@ -49,7 +49,7 @@ public static function register_fields() {
4949
return $cart;
5050
},
5151
],
52-
'cartItem' => [
52+
'cartItem' => [
5353
'type' => 'CartItem',
5454
'args' => [
5555
'key' => [
@@ -66,7 +66,7 @@ public static function register_fields() {
6666
return $item;
6767
},
6868
],
69-
'cartFee' => [
69+
'cartFee' => [
7070
'type' => 'CartFee',
7171
'args' => [
7272
'id' => [
@@ -83,7 +83,7 @@ public static function register_fields() {
8383
return $fee;
8484
},
8585
],
86-
'coupon' => [
86+
'coupon' => [
8787
'type' => 'Coupon',
8888
'description' => __( 'A coupon object', 'wp-graphql-woocommerce' ),
8989
'args' => [
@@ -133,7 +133,7 @@ public static function register_fields() {
133133
return Factory::resolve_crud_object( $coupon_id, $context );
134134
},
135135
],
136-
'customer' => [
136+
'customer' => [
137137
'type' => 'Customer',
138138
'description' => __( 'A customer object', 'wp-graphql-woocommerce' ),
139139
'args' => [
@@ -173,7 +173,7 @@ public static function register_fields() {
173173
return Factory::resolve_session_customer();
174174
},
175175
],
176-
'order' => [
176+
'order' => [
177177
'type' => 'Order',
178178
'description' => __( 'A order object', 'wp-graphql-woocommerce' ),
179179
'args' => [
@@ -240,7 +240,7 @@ public static function register_fields() {
240240
return $order;
241241
},
242242
],
243-
'productVariation' => [
243+
'productVariation' => [
244244
'type' => 'ProductVariation',
245245
'description' => __( 'A product variation object', 'wp-graphql-woocommerce' ),
246246
'args' => [
@@ -283,7 +283,7 @@ public static function register_fields() {
283283
return Factory::resolve_crud_object( $variation_id, $context );
284284
},
285285
],
286-
'refund' => [
286+
'refund' => [
287287
'type' => 'Refund',
288288
'description' => __( 'A refund object', 'wp-graphql-woocommerce' ),
289289
'args' => [
@@ -349,7 +349,7 @@ public static function register_fields() {
349349
return $refund;
350350
},
351351
],
352-
'shippingMethod' => [
352+
'shippingMethod' => [
353353
'type' => 'ShippingMethod',
354354
'description' => __( 'A shipping method object', 'wp-graphql-woocommerce' ),
355355
'args' => [
@@ -384,7 +384,7 @@ public static function register_fields() {
384384
return Factory::resolve_shipping_method( $method_id );
385385
},
386386
],
387-
'taxRate' => [
387+
'taxRate' => [
388388
'type' => 'TaxRate',
389389
'description' => __( 'A tax rate object', 'wp-graphql-woocommerce' ),
390390
'args' => [
@@ -419,6 +419,42 @@ public static function register_fields() {
419419
return Factory::resolve_tax_rate( $rate_id, $context );
420420
},
421421
],
422+
'allowedCountries' => [
423+
'type' => [ 'list_of' => 'CountriesEnum' ],
424+
'description' => __( 'Countries that the store sells to', 'wp-graphql-woocommerce' ),
425+
'resolve' => function() {
426+
$wc_countries = new \WC_Countries();
427+
$countries = $wc_countries->get_allowed_countries();
428+
429+
return array_keys( $countries );
430+
},
431+
],
432+
'allowedCountryStates' => [
433+
'type' => [ 'list_of' => 'CountryState' ],
434+
'args' => [
435+
'country' => [
436+
'type' => [ 'non_null' => 'CountriesEnum' ],
437+
'description' => __( 'Target country', 'wp-graphql-woocommerce' ),
438+
],
439+
],
440+
'description' => __( 'Countries that the store sells to', 'wp-graphql-woocommerce' ),
441+
'resolve' => function( $_, $args ) {
442+
$country = $args['country'];
443+
$wc_countries = new \WC_Countries();
444+
$states = $wc_countries->get_shipping_country_states();
445+
446+
if ( ! empty( $states ) && ! empty( $states[ $country ] ) ) {
447+
$formatted_states = [];
448+
foreach ( $states[ $country ] as $code => $name ) {
449+
$formatted_states[] = compact( 'name', 'code' );
450+
}
451+
452+
return $formatted_states;
453+
}
454+
455+
return [];
456+
},
457+
],
422458
]
423459
);
424460

tests/_support/Factory/ShippingZoneFactory.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,26 @@ public function create_object( $args ) {
3030

3131
$object = new \WC_Shipping_Zone();
3232

33+
if ( ! empty( $args['countries'] ) ) {
34+
foreach ( $args['countries'] as $country ) {
35+
$object->add_location( $country, 'country' );
36+
}
37+
}
38+
if ( ! empty( $args['states'] ) ) {
39+
foreach ( $args['states'] as $state ) {
40+
$object->add_location( $state, 'state' );
41+
}
42+
}
43+
if ( ! empty( $args['postcode'] ) ) {
44+
foreach ( $args['postcode'] as $postcode ) {
45+
$object->add_location( $postcode, 'postcode' );
46+
}
47+
}
48+
49+
if ( ! empty( $args['shipping_method'] ) ) {
50+
$object->add_shipping_method( $args['shipping_method'] );
51+
}
52+
3353
foreach ( $args as $key => $value ) {
3454
if ( is_callable( [ $object, "set_{$key}" ] ) ) {
3555
$object->{"set_{$key}"}( $value );

tests/wpunit/RootQueriesTest.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
class RootQueriesTest extends \Tests\WPGraphQL\WooCommerce\TestCase\WooGraphQLTestCase {
4+
public function testShippingCountriesQuery() {
5+
// Create shipping zones and shipping rates.
6+
update_option( 'woocommerce_allowed_countries', 'specific' );
7+
update_option( 'woocommerce_specific_allowed_countries', [ 'US', 'CA' ] );
8+
9+
// Create query
10+
$query = '
11+
query {
12+
allowedCountries
13+
}
14+
';
15+
16+
$response = $this->graphql( compact( 'query' ) );
17+
$expected = [
18+
$this->expectedField( 'allowedCountries.#', 'US' ),
19+
$this->expectedField( 'allowedCountries.#', 'CA' ),
20+
$this->not()->expectedField( 'allowedCountries.#', 'GB' ),
21+
];
22+
23+
$this->assertQuerySuccessful( $response, $expected );
24+
}
25+
26+
public function testShippingCountryStatesQuery() {
27+
// Create shipping zones and shipping rates.
28+
update_option( 'woocommerce_allowed_countries', 'specific' );
29+
update_option( 'woocommerce_specific_allowed_countries', [ 'US', 'CA' ] );
30+
31+
// Create query
32+
$query = '
33+
query ($country: CountriesEnum!) {
34+
allowedCountryStates(country: $country) {
35+
name
36+
code
37+
}
38+
}
39+
';
40+
41+
$variables = [ 'country' => 'US' ];
42+
$response = $this->graphql( compact( 'query', 'variables' ) );
43+
$expected = [
44+
$this->expectedObject(
45+
'allowedCountryStates.#',
46+
[
47+
$this->expectedField( 'name', 'Alaska' ),
48+
$this->expectedField( 'code', 'AL' ),
49+
]
50+
),
51+
$this->expectedObject(
52+
'allowedCountryStates.#',
53+
[
54+
$this->not()->expectedField( 'name', 'Ontario' ),
55+
$this->not()->expectedField( 'code', 'ON' ),
56+
]
57+
),
58+
];
59+
60+
$this->assertQuerySuccessful( $response, $expected );
61+
62+
$variables = [ 'country' => 'CA' ];
63+
$response = $this->graphql( compact( 'query', 'variables' ) );
64+
$expected = [
65+
$this->expectedObject(
66+
'allowedCountryStates.#',
67+
[
68+
$this->expectedField( 'name', 'Ontario' ),
69+
$this->expectedField( 'code', 'ON' ),
70+
]
71+
),
72+
$this->expectedObject(
73+
'allowedCountryStates.#',
74+
[
75+
$this->not()->expectedField( 'name', 'Alaska' ),
76+
$this->not()->expectedField( 'code', 'AL' ),
77+
]
78+
),
79+
];
80+
81+
$this->assertQuerySuccessful( $response, $expected );
82+
}
83+
}

0 commit comments

Comments
 (0)