Skip to content

Commit

Permalink
feat: Country queries implemented. (#736)
Browse files Browse the repository at this point in the history
* feat: Country queries implemented.

* chore: WPCS compliance met and late-minute renames.
  • Loading branch information
kidunot89 committed Apr 20, 2023
1 parent 27d9297 commit 80252b0
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 10 deletions.
1 change: 1 addition & 0 deletions includes/class-type-registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public function init( \WPGraphQL\Registry\TypeRegistry $type_registry ) {
Type\WPObject\Shipping_Rate_Type::register();
Type\WPObject\Cart_Error_Types::register();
Type\WPObject\Payment_Token_Types::register();
Type\WPObject\Country_State_Type::register();

// Object fields.
Type\WPObject\Product_Category_Type::register_fields();
Expand Down
1 change: 1 addition & 0 deletions includes/class-wp-graphql-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ private function includes() {
require $include_directory_path . 'type/object/class-tax-rate-type.php';
require $include_directory_path . 'type/object/class-variation-attribute-type.php';
require $include_directory_path . 'type/object/class-payment-token-types.php';
require $include_directory_path . 'type/object/class-country-state-type.php';

// Include input type class files.
require $include_directory_path . 'type/input/class-cart-item-input.php';
Expand Down
45 changes: 45 additions & 0 deletions includes/type/object/class-country-state-type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* WPObject Type - Country_State_Type
*
* Registers CountryState WPObject type
*
* @package WPGraphQL\WooCommerce\Type\WPObject
* @since 0.12.4
*/

namespace WPGraphQL\WooCommerce\Type\WPObject;

/**
* Class Country_State_Type
*/
class Country_State_Type {

/**
* Registers type
*/
public static function register() {
register_graphql_object_type(
'CountryState',
[
'description' => __( 'shipping country state object', 'wp-graphql-woocommerce' ),
'fields' => [
'code' => [
'type' => [ 'non_null' => 'String' ],
'description' => __( 'Country state code', 'wp-graphql-woocommerce' ),
'resolve' => function ( $source ) {
return ! empty( $source['code'] ) ? $source['code'] : null;
},
],
'name' => [
'type' => [ 'non_null' => 'String' ],
'description' => __( 'Country state name', 'wp-graphql-woocommerce' ),
'resolve' => function ( $source ) {
return ! empty( $source['name'] ) ? $source['name'] : null;
},
],
],
]
);
}
}
56 changes: 46 additions & 10 deletions includes/type/object/class-root-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static function register_fields() {
register_graphql_fields(
'RootQuery',
[
'cart' => [
'cart' => [
'type' => 'Cart',
'args' => [
'recalculateTotals' => [
Expand All @@ -49,7 +49,7 @@ public static function register_fields() {
return $cart;
},
],
'cartItem' => [
'cartItem' => [
'type' => 'CartItem',
'args' => [
'key' => [
Expand All @@ -66,7 +66,7 @@ public static function register_fields() {
return $item;
},
],
'cartFee' => [
'cartFee' => [
'type' => 'CartFee',
'args' => [
'id' => [
Expand All @@ -83,7 +83,7 @@ public static function register_fields() {
return $fee;
},
],
'coupon' => [
'coupon' => [
'type' => 'Coupon',
'description' => __( 'A coupon object', 'wp-graphql-woocommerce' ),
'args' => [
Expand Down Expand Up @@ -133,7 +133,7 @@ public static function register_fields() {
return Factory::resolve_crud_object( $coupon_id, $context );
},
],
'customer' => [
'customer' => [
'type' => 'Customer',
'description' => __( 'A customer object', 'wp-graphql-woocommerce' ),
'args' => [
Expand Down Expand Up @@ -173,7 +173,7 @@ public static function register_fields() {
return Factory::resolve_session_customer();
},
],
'order' => [
'order' => [
'type' => 'Order',
'description' => __( 'A order object', 'wp-graphql-woocommerce' ),
'args' => [
Expand Down Expand Up @@ -240,7 +240,7 @@ public static function register_fields() {
return $order;
},
],
'productVariation' => [
'productVariation' => [
'type' => 'ProductVariation',
'description' => __( 'A product variation object', 'wp-graphql-woocommerce' ),
'args' => [
Expand Down Expand Up @@ -283,7 +283,7 @@ public static function register_fields() {
return Factory::resolve_crud_object( $variation_id, $context );
},
],
'refund' => [
'refund' => [
'type' => 'Refund',
'description' => __( 'A refund object', 'wp-graphql-woocommerce' ),
'args' => [
Expand Down Expand Up @@ -349,7 +349,7 @@ public static function register_fields() {
return $refund;
},
],
'shippingMethod' => [
'shippingMethod' => [
'type' => 'ShippingMethod',
'description' => __( 'A shipping method object', 'wp-graphql-woocommerce' ),
'args' => [
Expand Down Expand Up @@ -384,7 +384,7 @@ public static function register_fields() {
return Factory::resolve_shipping_method( $method_id );
},
],
'taxRate' => [
'taxRate' => [
'type' => 'TaxRate',
'description' => __( 'A tax rate object', 'wp-graphql-woocommerce' ),
'args' => [
Expand Down Expand Up @@ -419,6 +419,42 @@ public static function register_fields() {
return Factory::resolve_tax_rate( $rate_id, $context );
},
],
'allowedCountries' => [
'type' => [ 'list_of' => 'CountriesEnum' ],
'description' => __( 'Countries that the store sells to', 'wp-graphql-woocommerce' ),
'resolve' => function() {
$wc_countries = new \WC_Countries();
$countries = $wc_countries->get_allowed_countries();

return array_keys( $countries );
},
],
'allowedCountryStates' => [
'type' => [ 'list_of' => 'CountryState' ],
'args' => [
'country' => [
'type' => [ 'non_null' => 'CountriesEnum' ],
'description' => __( 'Target country', 'wp-graphql-woocommerce' ),
],
],
'description' => __( 'Countries that the store sells to', 'wp-graphql-woocommerce' ),
'resolve' => function( $_, $args ) {
$country = $args['country'];
$wc_countries = new \WC_Countries();
$states = $wc_countries->get_shipping_country_states();

if ( ! empty( $states ) && ! empty( $states[ $country ] ) ) {
$formatted_states = [];
foreach ( $states[ $country ] as $code => $name ) {
$formatted_states[] = compact( 'name', 'code' );
}

return $formatted_states;
}

return [];
},
],
]
);

Expand Down
20 changes: 20 additions & 0 deletions tests/_support/Factory/ShippingZoneFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,26 @@ public function create_object( $args ) {

$object = new \WC_Shipping_Zone();

if ( ! empty( $args['countries'] ) ) {
foreach ( $args['countries'] as $country ) {
$object->add_location( $country, 'country' );
}
}
if ( ! empty( $args['states'] ) ) {
foreach ( $args['states'] as $state ) {
$object->add_location( $state, 'state' );
}
}
if ( ! empty( $args['postcode'] ) ) {
foreach ( $args['postcode'] as $postcode ) {
$object->add_location( $postcode, 'postcode' );
}
}

if ( ! empty( $args['shipping_method'] ) ) {
$object->add_shipping_method( $args['shipping_method'] );
}

foreach ( $args as $key => $value ) {
if ( is_callable( [ $object, "set_{$key}" ] ) ) {
$object->{"set_{$key}"}( $value );
Expand Down
83 changes: 83 additions & 0 deletions tests/wpunit/RootQueriesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

class RootQueriesTest extends \Tests\WPGraphQL\WooCommerce\TestCase\WooGraphQLTestCase {
public function testShippingCountriesQuery() {
// Create shipping zones and shipping rates.
update_option( 'woocommerce_allowed_countries', 'specific' );
update_option( 'woocommerce_specific_allowed_countries', [ 'US', 'CA' ] );

// Create query
$query = '
query {
allowedCountries
}
';

$response = $this->graphql( compact( 'query' ) );
$expected = [
$this->expectedField( 'allowedCountries.#', 'US' ),
$this->expectedField( 'allowedCountries.#', 'CA' ),
$this->not()->expectedField( 'allowedCountries.#', 'GB' ),
];

$this->assertQuerySuccessful( $response, $expected );
}

public function testShippingCountryStatesQuery() {
// Create shipping zones and shipping rates.
update_option( 'woocommerce_allowed_countries', 'specific' );
update_option( 'woocommerce_specific_allowed_countries', [ 'US', 'CA' ] );

// Create query
$query = '
query ($country: CountriesEnum!) {
allowedCountryStates(country: $country) {
name
code
}
}
';

$variables = [ 'country' => 'US' ];
$response = $this->graphql( compact( 'query', 'variables' ) );
$expected = [
$this->expectedObject(
'allowedCountryStates.#',
[
$this->expectedField( 'name', 'Alaska' ),
$this->expectedField( 'code', 'AL' ),
]
),
$this->expectedObject(
'allowedCountryStates.#',
[
$this->not()->expectedField( 'name', 'Ontario' ),
$this->not()->expectedField( 'code', 'ON' ),
]
),
];

$this->assertQuerySuccessful( $response, $expected );

$variables = [ 'country' => 'CA' ];
$response = $this->graphql( compact( 'query', 'variables' ) );
$expected = [
$this->expectedObject(
'allowedCountryStates.#',
[
$this->expectedField( 'name', 'Ontario' ),
$this->expectedField( 'code', 'ON' ),
]
),
$this->expectedObject(
'allowedCountryStates.#',
[
$this->not()->expectedField( 'name', 'Alaska' ),
$this->not()->expectedField( 'code', 'AL' ),
]
),
];

$this->assertQuerySuccessful( $response, $expected );
}
}

0 comments on commit 80252b0

Please sign in to comment.