Skip to content

Commit

Permalink
Merge pull request #16 from imranhsayed/feature/filter-countries
Browse files Browse the repository at this point in the history
Feature/filter countries
  • Loading branch information
imranhsayed authored Apr 25, 2021
2 parents 43bab0f + 8bd1324 commit c044368
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 9 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ For Preview to work , you also need the [wp-graphql-jwt-authentication](https://
```javascript
{
wooCountries {
countries
billingCountries
shippingCountries
}
wooStates(countryCode: "in") {
states
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "A WordPress plugin that adds features to use WordPress as a headless CMS with any front-end environment using REST API",
"type": "wordpress-plugin",
"license": "GPL-3.0+",
"version": "1.4.0",
"version": "1.5.0",
"keywords": [
"wordpress",
"plugin"
Expand Down
2 changes: 1 addition & 1 deletion headless-cms.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Author URI: https://codeytek.com
* License: GPL2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Version: 1.4.0
* Version: 1.5.0
* Text Domain: headless-cms
*
* @package headless-cms
Expand Down
50 changes: 44 additions & 6 deletions inc/classes/schema/class-register-countries.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,14 @@ protected function setup_hooks() {
/**
* Register field.
*/
function register_countries_fields() {
public function register_countries_fields() {

register_graphql_object_type( 'WooCountries', [
'description' => __( 'Countries Type', 'headless-cms' ),
'fields' => [
'countries' => [ 'type' => 'String' ],
]
'fields' => [
'billingCountries' => [ 'type' => 'String' ],
'shippingCountries' => [ 'type' => 'String' ],
],
] );

register_graphql_field(
Expand All @@ -59,20 +60,57 @@ function register_countries_fields() {
'type' => 'WooCountries',
'resolve' => function () {

$countries = class_exists('WooCommerce') ? WC()->countries : [];
// All countries with states for billing.
$all_countries = class_exists( 'WooCommerce' ) ? WC()->countries : [];
$all_countries = ! empty( $all_countries->countries ) ? $all_countries->countries : [];
$billing_countries_with_states = $this->get_countries_having_states( $all_countries );

// All countries with states for shipping.
$shipping_countries = class_exists( 'WooCommerce' ) ? WC()->countries->get_shipping_countries() : [];;
$shipping_countries_with_states = $this->get_countries_having_states( $shipping_countries );

/**
* Here you need to return data that matches the shape of the "WooCountries" type. You could get
* the data from the WP Database, an external API, or static values.
* For example in this case we are getting it from WordPress database.
*/
return [
'countries' => wp_json_encode($countries),
'billingCountries' => wp_json_encode( $billing_countries_with_states ),
'shippingCountries' => wp_json_encode( $shipping_countries_with_states ),
];

},
]
);
}

/**
* Filters countries that have states.
*
* Excludes the one's that don't have states.
*/
public function get_countries_having_states( $all_countries ) {

$countries_with_states = [];

if ( ! class_exists( 'WooCommerce' ) || empty( $all_countries ) || !is_array($all_countries) ) {
return $countries_with_states;
}

$all_countries_with_states = WC()->countries->get_allowed_countries();

if ( empty( $all_countries_with_states ) && !is_array( $all_countries_with_states ) ) {
return $countries_with_states;
}

foreach ( $all_countries_with_states as $country_code => $states ) {
if ( ! empty( $states ) ) {
$countries_with_states[$country_code] = $all_countries[$country_code];
}
}

return $countries_with_states;

}

}

0 comments on commit c044368

Please sign in to comment.