105 lines
2.7 KiB
PHP
105 lines
2.7 KiB
PHP
<?php
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit; // Exit if accessed directly
|
|
}
|
|
|
|
/**
|
|
* Implements available wiaas countries
|
|
*
|
|
* Class Wiaas_Countries
|
|
*/
|
|
class Wiaas_Countries {
|
|
|
|
/**
|
|
* Available countries for wiaas
|
|
* @var array
|
|
*/
|
|
private static $available_countries = array(
|
|
'Sweden' => array(
|
|
'name' => 'Sweden',
|
|
'code' => 'se',
|
|
'vat' => 9 ,
|
|
'currency' => 'SEK'
|
|
),
|
|
'Denmark' => array(
|
|
'name' => 'Denmark',
|
|
'code' => 'dk',
|
|
'vat' => 9 ,
|
|
'currency' => 'DKK'
|
|
),
|
|
'Finland' => array(
|
|
'name' => 'Finland',
|
|
'code' => 'fi',
|
|
'vat' => 9 ,
|
|
'currency' => 'EUR'
|
|
),
|
|
);
|
|
|
|
public static function init() {
|
|
|
|
add_action('woocommerce_after_register_taxonomy', array(__CLASS__, 'register_product_countries_taxonomy'));
|
|
}
|
|
|
|
/**
|
|
* Registers product taxonomy for avaiable countries
|
|
*/
|
|
public static function register_product_countries_taxonomy() {
|
|
|
|
$labels = array(
|
|
'name' => _x( 'Country', 'taxonomy general name', 'wiaas' ),
|
|
'singular_name' => _x( 'Country', 'taxonomy singular name', 'wiaas' ),
|
|
'menu_name' => _x( 'Country', 'Admin menu name', 'wiaas' ),
|
|
'search_items' => __( 'Search Countries', 'wiaas' ),
|
|
'all_items' => __( 'All Countries', 'wiaas' ),
|
|
'parent_item' => __( 'Parent Country', 'wiaas' ),
|
|
'parent_item_colon' => __( 'Parent Country:', 'wiaas' ),
|
|
'edit_item' => __( 'Edit Country', 'wiaas' ),
|
|
'update_item' => __( 'Update Country', 'wiaas' ),
|
|
'add_new_item' => __( 'Add New Country', 'wiaas' ),
|
|
'new_item_name' => __( 'New Country Name', 'wiaas' ),
|
|
);
|
|
|
|
$args = array(
|
|
'hierarchical' => false,
|
|
'label' => __( 'Countries', 'wiaas' ),
|
|
'labels' => $labels,
|
|
'show_ui' => true,
|
|
'show_admin_column' => true,
|
|
'query_var' => true,
|
|
'rewrite' => array( 'slug' => 'product_country' ),
|
|
);
|
|
|
|
register_taxonomy( 'product_country', array( 'product' ), $args );
|
|
|
|
foreach (self::$available_countries as $available_country) {
|
|
wp_insert_term($available_country['name'], 'product_country');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Retrieves country for provided package
|
|
* @param $package
|
|
*
|
|
* @return array|null
|
|
*/
|
|
public static function get_package_country($package) {
|
|
return self::get_product_country($package);
|
|
}
|
|
|
|
/**
|
|
* Retrieves country for provided product
|
|
* @param $product
|
|
*
|
|
* @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;
|
|
}
|
|
}
|
|
|
|
Wiaas_Countries::init();
|