From f386135c2880032c0562935db4a2da80fb5f8219 Mon Sep 17 00:00:00 2001 From: Imran Date: Sun, 25 Apr 2021 22:06:26 +0530 Subject: [PATCH 1/3] Get all billing and shipping countries --- composer.json | 2 +- headless-cms.php | 31 +++++++++++- .../schema/class-register-countries.php | 50 ++++++++++++++++--- 3 files changed, 75 insertions(+), 8 deletions(-) diff --git a/composer.json b/composer.json index 2d50824..b0ee423 100644 --- a/composer.json +++ b/composer.json @@ -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" diff --git a/headless-cms.php b/headless-cms.php index c833310..3806645 100644 --- a/headless-cms.php +++ b/headless-cms.php @@ -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 @@ -34,3 +34,32 @@ function headless_cms_features_plugin_loader() { } headless_cms_features_plugin_loader(); + + + +//function hello() { +// +// if ( !class_exists('WooCommerce') ) { +// return []; +// } +// +// $countries_with_country_codes = WC()->countries; +// $all_countries = WC()->countries->get_states(); +// $countries_with_states = []; +// +// if ( empty( $all_countries ) && !is_array( $all_countries ) ) { +// return []; +// } +// +// foreach ( $all_countries as $country_code => $states ) { +// if ( ! empty( $states ) ) { +// $countries_with_states[$country_code] = $country_code; +// } +// } +// +// echo '
';
+//	print_r($countries_with_country_codes);
+//	wp_die();
+//}
+//
+//add_action('init', 'hello' );
diff --git a/inc/classes/schema/class-register-countries.php b/inc/classes/schema/class-register-countries.php
index 12851c1..5e3e26a 100644
--- a/inc/classes/schema/class-register-countries.php
+++ b/inc/classes/schema/class-register-countries.php
@@ -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(
@@ -59,7 +60,14 @@ 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
@@ -67,7 +75,8 @@ function register_countries_fields() {
 					 * 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 ),
 					];
 
 				},
@@ -75,4 +84,33 @@ function register_countries_fields() {
 		);
 	}
 
+	/**
+	 * 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;
+
+	}
+
 }

From dc9f460ef6a0d3ad6a1d375ff8e8dcc5b2beb2ab Mon Sep 17 00:00:00 2001
From: Imran 
Date: Sun, 25 Apr 2021 22:07:03 +0530
Subject: [PATCH 2/3] Update readme

---
 README.md | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/README.md b/README.md
index 8b67d5e..3eec7f9 100644
--- a/README.md
+++ b/README.md
@@ -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

From 8bd1324f2bc8c680c628e7e876708a5cf30a459a Mon Sep 17 00:00:00 2001
From: Imran 
Date: Sun, 25 Apr 2021 22:08:28 +0530
Subject: [PATCH 3/3] Update readme

---
 headless-cms.php | 29 -----------------------------
 1 file changed, 29 deletions(-)

diff --git a/headless-cms.php b/headless-cms.php
index 3806645..7ae9275 100644
--- a/headless-cms.php
+++ b/headless-cms.php
@@ -34,32 +34,3 @@ function headless_cms_features_plugin_loader() {
 }
 
 headless_cms_features_plugin_loader();
-
-
-
-//function hello() {
-//
-//	if ( !class_exists('WooCommerce') ) {
-//		return [];
-//	}
-//
-//	$countries_with_country_codes = WC()->countries;
-//	$all_countries = WC()->countries->get_states();
-//	$countries_with_states = [];
-//
-//	if ( empty( $all_countries ) && !is_array( $all_countries ) ) {
-//		return [];
-//	}
-//
-//	foreach ( $all_countries as $country_code => $states ) {
-//		if ( ! empty( $states ) ) {
-//			$countries_with_states[$country_code] = $country_code;
-//		}
-//	}
-//
-//	echo '
';
-//	print_r($countries_with_country_codes);
-//	wp_die();
-//}
-//
-//add_action('init', 'hello' );