Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/filter countries #16

Merged
merged 3 commits into from
Apr 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;

}

}