Show countries for delivery process forms. Refactor countries.
This commit is contained in:
@@ -12,28 +12,19 @@ if ( ! defined( 'ABSPATH' ) ) {
|
||||
class Wiaas_Countries {
|
||||
|
||||
/**
|
||||
* Available countries for wiaas
|
||||
* Default available countries for wiaas
|
||||
* @var array
|
||||
*/
|
||||
private static $available_countries = array(
|
||||
'Sweden' => array(
|
||||
'id' => 1,
|
||||
'name' => 'Sweden',
|
||||
'code' => 'se',
|
||||
private static $default_countries = array(
|
||||
'se' => array(
|
||||
'vat' => 9 ,
|
||||
'currency' => 'SEK'
|
||||
),
|
||||
'Denmark' => array(
|
||||
'id' => 2,
|
||||
'name' => 'Denmark',
|
||||
'code' => 'dk',
|
||||
'dk' => array(
|
||||
'vat' => 9 ,
|
||||
'currency' => 'DKK'
|
||||
),
|
||||
'Finland' => array(
|
||||
'id' => 3,
|
||||
'name' => 'Finland',
|
||||
'code' => 'fi',
|
||||
'fi' => array(
|
||||
'vat' => 9 ,
|
||||
'currency' => 'EUR'
|
||||
),
|
||||
@@ -44,31 +35,97 @@ class Wiaas_Countries {
|
||||
add_action('woocommerce_after_register_taxonomy', array(__CLASS__, 'register_product_countries_taxonomy'));
|
||||
}
|
||||
|
||||
public static function get_list_of_countries(){
|
||||
$result = [];
|
||||
foreach(self::$available_countries as $country){
|
||||
array_push($result, array(
|
||||
'country_id' => $country['id'],
|
||||
'country_name' => $country['name']
|
||||
));
|
||||
/**
|
||||
* Retrieve all possible country choices
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_country_choices() {
|
||||
$countries = new WC_Countries();
|
||||
$choices = array();
|
||||
|
||||
foreach ($countries->get_countries() as $code => $name) {
|
||||
$choices[strtolower($code)] = $name;
|
||||
}
|
||||
return $result;
|
||||
|
||||
return $choices;
|
||||
}
|
||||
|
||||
public static function get_country_name_by_id($id){
|
||||
foreach(self::$available_countries as $country){
|
||||
if ($country['id'] == $id) return $country['name'];
|
||||
}
|
||||
return '';
|
||||
/**
|
||||
* Retrieve all possible currency choices
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_currency_choices() {
|
||||
return get_woocommerce_currencies();
|
||||
}
|
||||
|
||||
public static function get_country_code_by_currency($currency) {
|
||||
/**
|
||||
* Retrieve list of available countries setup by administrator
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_available_countries() {
|
||||
$available_countries = [];
|
||||
|
||||
foreach (self::$available_countries as $country) {
|
||||
$available_country_terms = get_terms(array(
|
||||
'taxonomy' => 'product_country',
|
||||
'hide_empty' => false,
|
||||
));
|
||||
|
||||
if ($country['currency'] === $currency) {
|
||||
foreach($available_country_terms as $country_term) {
|
||||
$code = get_term_meta($country_term->term_id, '_wiaas_country_code', true);
|
||||
$currency = get_term_meta($country_term->term_id, '_wiaas_country_currency', true);
|
||||
$vat = get_term_meta($country_term->term_id, '_wiaas_country_vat', true);
|
||||
|
||||
return $country['code'];
|
||||
$available_countries[] = array(
|
||||
'id' => $country_term->term_id,
|
||||
'name' => $country_term->name,
|
||||
'code' => $code,
|
||||
'currency' => $currency,
|
||||
'vat' => $vat
|
||||
);
|
||||
}
|
||||
|
||||
return $available_countries;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve country name by code for available country
|
||||
*
|
||||
* @param string $code
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public static function get_available_country_name_by_code($code) {
|
||||
$available_countries = self::get_available_countries();
|
||||
|
||||
foreach ($available_countries as $available_country) {
|
||||
|
||||
if ($available_country['code'] === $code) {
|
||||
|
||||
return $available_country['name'];
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve country code by currency for available country
|
||||
*
|
||||
* @param string $currency
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public static function get_available_country_code_by_currency($currency) {
|
||||
$available_countries = self::get_available_countries();
|
||||
|
||||
foreach ($available_countries as $available_country) {
|
||||
|
||||
if ($available_country['currency'] === $currency) {
|
||||
|
||||
return $available_country['code'];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,8 +163,27 @@ class Wiaas_Countries {
|
||||
|
||||
register_taxonomy( 'product_country', array( 'product' ), $args );
|
||||
|
||||
foreach (self::$available_countries as $available_country) {
|
||||
wp_insert_term($available_country['name'], 'product_country');
|
||||
$choices = self::get_country_choices();
|
||||
|
||||
foreach (self::$default_countries as $code => $info) {
|
||||
|
||||
$name = $choices[$code];
|
||||
|
||||
if (has_term($name)) {
|
||||
// bail out
|
||||
return;
|
||||
}
|
||||
|
||||
$name = $choices[$code];
|
||||
$result = wp_insert_term($name, 'product_country');
|
||||
|
||||
if (is_wp_error($result)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
update_term_meta($result->term_id, '_wiaas_country_code', $code);
|
||||
update_term_meta($result->term_id, '_wiaas_country_currency', $info['currency']);
|
||||
update_term_meta($result->term_id, '_wiaas_country_vat', $info['vat']);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,10 +204,25 @@ class Wiaas_Countries {
|
||||
* @return array|null
|
||||
*/
|
||||
public static function get_product_country($product) {
|
||||
$product_country = get_the_terms($product->get_id(), 'product_country');
|
||||
return is_array($product_country) && isset($product_country[0]) ?
|
||||
self::$available_countries[$product_country[0]->name] :
|
||||
null;
|
||||
$country_terms = get_the_terms($product->get_id(), 'product_country');
|
||||
|
||||
if (is_wp_error($country_terms) || empty($country_terms) || empty($country_terms[0])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$country_term = $country_terms[0];
|
||||
|
||||
$code = get_term_meta($country_term->term_id, '_wiaas_country_code', true);
|
||||
$currency = get_term_meta($country_term->term_id, '_wiaas_country_currency', true);
|
||||
$vat = get_term_meta($country_term->term_id, '_wiaas_country_vat', true);
|
||||
|
||||
return array(
|
||||
'id' => $country_term->term_id,
|
||||
'name' => $country_term->name,
|
||||
'code' => $code,
|
||||
'currency' => $currency,
|
||||
'vat' => $vat
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user