Skip to content

Instantly share code, notes, and snippets.

@joshmoto
Created March 29, 2023 15:17
Show Gist options
  • Save joshmoto/fdffedbc4b65a2d2b6b2471c2d89be07 to your computer and use it in GitHub Desktop.
Save joshmoto/fdffedbc4b65a2d2b6b2471c2d89be07 to your computer and use it in GitHub Desktop.
/**
* @author Josh Cranwell <josh.cranwell@gmail.com>
* @copyright The Sweet People
* @version 1.0
* @link https://www.thesweetpeople.com/
* @since 23 March 2023
*/
// the new npm imports
//require('bootstrap4-toggle');
// jQuery on ready
jQuery(function($) {
// all pricing form
const product_pricing = $('.product-pricing-new', document);
// pricing form object
$(product_pricing)
// pricing form country select on change
.on('input','[name="shipping_country"]',function (e) {
// load shipping servicess
load_shipping_services(e.currentTarget);
})
// pricing form service selcect on change
.on('input','[name="shipping_service"]', function (e) {
// load pricing table
load_pricing_table(e.currentTarget);
})
// pricing form currency select on change
.on('input','[name="pricing_currency"]', function (e) {
// get selected currecy value
let currency = e.currentTarget.value;
// set currency cookie
Cookies.set('pricing_currency', currency, { expires: 365 });
// load pricing table
load_pricing_table(e.currentTarget);
})
/**
* @param target
*/
function load_shipping_services(target) {
// get selected country code
let code = target.value;
// get the current pricing form
let product_pricing = $(target).closest('.product-pricing-new');
// disable all shipping services
$('[name="shipping_service"]', product_pricing).prop('disabled',true);
// ajax call
$.ajax({
cache: false,
timeout: 30000,
url: admin_ajax_url,
type: 'GET',
data: {
action: 'load_shipping_services',
country: code
}
}).done(function (data) {
// update shipping services group
$('[data-group="services"]', product_pricing).replaceWith(data);
}).then(function () {
// load pricing table
load_pricing_table(product_pricing);
});
}
/**
* @param target
*/
window.load_pricing_table = function(target) {
// get the current pricing form
let product_pricing = $(target).closest('.product-pricing-new');
// get our shipping form variable
let product_id = $(product_pricing).data('product-id');
let company_id = $(product_pricing).data('company-id');
let country = $('[name="shipping_country"]', product_pricing).val();
let service = $('[name="shipping_service"]:checked', product_pricing).val();
let currency = $('[name="pricing_currency"]', product_pricing).val();
//console.log(product_id, country, service, currency);
// fade out pricing table
$('.pricing-table-new', product_pricing).fadeTo(0,0.5);
// ajax call
$.ajax({
cache: false,
timeout: 30000,
url: admin_ajax_url,
type: 'GET',
data: {
action: 'load_pricing_table',
product_id: product_id,
company_id: company_id,
country: country,
service: service,
currency: currency
}
}).done(function (data) {
// update pricing table and fade in
$('.pricing-table-new', product_pricing).html('<pre>' + JSON.stringify(data,null,4) + '</pre>').fadeTo(0,1);
}).then(function () {
console.log('then');
});
}
})
<?php
use JetBrains\PhpStorm\NoReturn;
/**
* @author Josh Cranwell <josh.cranwell@gmail.com>
* @copyright The Sweet People
* @version 1.0
* @link https://www.thesweetpeople.com/
* @since 23 March 2023
*/
class PricingNew {
public static
string $default_currency = 'gbp';
public static
array|bool $error_logs = false;
public function __construct ()
{
// get default currency
$this->default_currency();
// loadding shipping form service
add_action('wp_ajax_nopriv_load_pricing_table', [ $this, 'ajax_load_pricing_table' ], 20 );
add_action('wp_ajax_load_pricing_table', [ $this, 'ajax_load_pricing_table' ], 20 );
}
/**
* @param mixed $field
* @param string $message
* @return void
*/
public static function error_logger(mixed $field, string $message): void
{
// if field is empty
if(!$field) {
// set error log
self::$error_logs[] = $message;
}
}
/**
* @return void
*/
#[NoReturn] public function ajax_load_pricing_table(): void
{
// set data array
$data = [
'product_id' => $_GET['product_id'] ? (int)$_GET['product_id'] : false,
'company_id' => $_GET['company_id'] ? (int)$_GET['company_id'] : false,
'country' => $_GET['country'] ?? false,
'service' => $_GET['service'] ?? false,
'currency' => $_GET['currency'] ?? false
];
// update data with new data
$data = self::data($data);
// if we have errors
if(isset($data['error']) && is_array($data['error'])) {
// send json error
wp_send_json_error($data['error']);
} else {
// else send json success
wp_send_json_success($data);
}
// death is the beginning
die();
}
/**
* @param $data array
* @return array
*/
public function data(array $data): array
{
// get pricing
$data = self::get_pricing_data($data);
// get unit weight
$data = Shipping::get_unit_weight($data);
// get packaging
$data = Shipping::get_packaging_id($data);
// get parcel data
$data = Shipping::get_parcel_data($data);
// get pallet data
$data = Shipping::get_pallet_data($data);
// get individual shipping
//$data = Shipping::get_individual($data);
// if we have errors return the logs
if(self::$error_logs) {
// return the error logs
return [ 'error' => self::$error_logs ];
}
// return data
return $data;
}
/**
* @param array $price_breaks
* @return array
*/
public static function breaks_floatvals(array $price_breaks): array
{
// loop through price breaks and convert break values to float
foreach ($price_breaks as &$break) {
$break["quantity"] = floatval($break["quantity"]);
$break["unit_price"] = floatval($break["unit_price"]);
}
// return price breaks
return $price_breaks;
}
/**
* @param array $data
* @return array|bool
*/
public static function get_pricing_data(array $data): array|bool
{
// if data country is set and is not empty
if(isset($data['country']) && $data['country']) {
// get the quanity measurement type
$unit_measure = get_field('product_weight_kg',$data['product_id']);
// if we have a measure
$data['unit_measure'] = $unit_measure ? 'kg' : 'qty';
// get the origination promo setting
$promo_origination = get_field('promo_origination_charges','option');
// get the shipping packaging setting
switch (Shipping::$settings['countries'][$data['country']]['pricing'])
{
case 'uk':
// update data with pricing UK code
$data['pricing'] = 'uk';
// get the UK origination
$uk_origination = get_field('product_origination',$data['product_id']);
// if promo origination is on then set origination to 0 else set it to the UK origination
$data['origination'] = $promo_origination ? 0 : (float)$uk_origination;
// get the UK price breaks
$uk_price_breaks = get_field('product_prices',$data['product_id']);
// if we have UK price breaks
if($uk_price_breaks) {
// set the price breaks
$data['price_breaks'] = self::breaks_floatvals($uk_price_breaks);
} else {
// no price breaks found
self::error_logger(false,'No UK price breaks found for <a href="'.get_edit_post_link($data['product_id']).'" target="_blank">product</a>.');
}
break;
case 'eu':
// update data with pricing EU code
$data['pricing'] = 'eu';
// get the EU origination
$eu_origination = get_field('product_eu_origination',$data['product_id']);
// if promo origination is on then set origination to 0 else set it to the EU origination
$data['origination'] = $promo_origination ? 0 : (float)$eu_origination;
// get the EU price breaks
$eu_price_breaks = get_field('product_eu_prices',$data['product_id']);
// if we have EU price breaks
if($eu_price_breaks) {
// set the price breaks
$data['price_breaks'] = self::breaks_floatvals($eu_price_breaks);
} else {
// no price breaks found
self::error_logger(false,'No EU price breaks found for <a href="'.get_edit_post_link($data['product_id']).'" target="_blank">product</a>.');
}
break;
default:
// no regional price code found
self::error_logger(false,'No regional pricing code found in shipping settings.');
}
} else {
// no product id is set in data
PricingNew::error_logger(false,'No shipping country is set in data.');
}
// return data
return $data;
}
/**
* @return void
*/
public static function render_currency_select(): void
{
?>
<div class="form-group ml-auto">
<select class="form-control" style="padding-right:2.25rem;" name="pricing_currency">
<option value="gbp" <?=self::$default_currency==='gbp'?'selected':null?>>(£) GBP <span class="fi fi-gb"></span></option>
<option value="eur" <?=self::$default_currency==='eur'?'selected':null?>>(€) EUR <span class="fi fi-eu"></span></option>
</select>
</div>
<?php
}
/**
* @return void
*/
public function default_currency(): void
{
// check if we have eu shipping cookie
if (isset($_COOKIE['pricing_currency'])) {
// set our currency variable
self::$default_currency = $_COOKIE['pricing_currency'];
}
}
}
new PricingNew();
<?php
use JetBrains\PhpStorm\NoReturn;
/**
* @author Josh Cranwell <josh.cranwell@gmail.com>
* @copyright The Sweet People
* @version 1.0
* @link https://www.thesweetpeople.com/
* @since 23 March 2023
*/
class Shipping {
/**
* @return $
*/
public static
array $countries = [
'AT' => 'Austria',
'BE' => 'Belgium',
'BG' => 'Bulgaria',
'GB-CHA' => 'Channel Islands',
'HR' => 'Croatia',
'CY' => 'Republic of Cyprus',
'CZ' => 'Czech Republic',
'DK' => 'Denmark',
'EE' => 'Estonia',
'FI' => 'Finland',
'FR' => 'France',
'DE' => 'Germany',
'GR' => 'Greece',
'HU' => 'Hungary',
'IE' => 'Ireland',
'IT' => 'Italy',
'GB-IOM' => 'Isle of Man',
'GB-IOW' => 'Isle of Wight',
'LV' => 'Latvia',
'LT' => 'Lithuania',
'LU' => 'Luxembourg',
'MT' => 'Malta',
'NL' => 'Netherlands',
'PL' => 'Poland',
'PT' => 'Portugal',
'RO' => 'Romania',
'SK' => 'Slovakia',
'SI' => 'Slovenia',
'ES' => 'Spain',
'SE' => 'Sweden',
'GB' => 'United Kingdom'
];
public static
array $uk_couriers = [
"Royal Mail",
"DHL Express UK",
"Hermes",
"DPD UK",
"UPS UK",
"TNT UK",
"Parcelforce Worldwide",
"Yodel",
"UK Mail",
"DX"
];
public static
array $eu_couriers = [
"DHL Express",
"UPS",
"FedEx",
"GLS",
"DPD",
"TNT",
"Chronopost",
"La Poste",
"PostNord",
"Bpost"
];
public static
array $services = [
"standard" => "Standard",
"pre-1200" => "Pre 12:00",
"pre-1030" => "Pre 10:30"
];
public static
array $settings = [];
public static
array|bool $enabled = false;
public static
array|bool $individual = false;
#[NoReturn] public function __construct ()
{
// load acf choices shipping settings
add_filter('acf/load_field/key=field_6418de2d5394b', [ $this, 'load_country_choices' ]);
add_filter('acf/load_field/key=field_6418e1805394e', [ $this, 'load_courier_choices' ]);
add_filter('acf/load_field/key=field_6418fbf07a998', [ $this, 'load_courier_choices' ]);
add_filter('acf/load_field/key=field_6418efa64b7c1', [ $this, 'load_service_choices' ]);
add_filter('acf/load_field/key=field_6418fbf07a999', [ $this, 'load_service_choices' ]);
// set our product vars
add_action('wp', [ $this, 'set_product_vars' ]);
// set our settings varible array
$this->set_settings();
// loadding shipping form service
add_action('wp_ajax_nopriv_load_shipping_services', [ $this, 'ajax_load_shipping_services' ], 20 );
add_action('wp_ajax_load_shipping_services', [ $this, 'ajax_load_shipping_services' ], 20 );
}
/**
* @param array $data
* @return array
*/
public static function get_packaging_id(array $data): array
{
// check we have a product id
if(isset($data['product_id']) && $data['product_id']) {
// if product is pre packed
$pre_packed = get_field('product_is_pre_packed',$data['product_id']);
// if product is pre packed
if($pre_packed) {
// get confectionery id
$data['packaging_id'] = get_field('product_confectionery',$data['product_id']);
// if no confectionery packaging id is set the log error
PricingNew::error_logger($data['packaging_id'],'No pre-packed confectionery selected for <a href="'.get_edit_post_link($data['product_id']).'" target="_blank">product</a>.');
} else {
// get packaging id
$data['packaging_id'] = get_field('product_packaging',$data['product_id']);
// if no packaging id is set the log error
PricingNew::error_logger($data['packaging_id'],'No packaging selected for <a href="'.get_edit_post_link($data['product_id']).'" target="_blank">product</a>.');
}
} else {
// no product id is set in data
PricingNew::error_logger(false,'No product id is set in data.');
}
// return data aray
return $data;
}
/**
* @param array $data
* @return array
*/
public static function get_parcel_data(array $data): array
{
// check we have a packaging id
if(isset($data['packaging_id']) && $data['packaging_id']) {
// get parcel data
$packaging = get_term_by('term_taxonomy_id',$data['packaging_id'],'','ARRAY_A');
// check we have taxonomoy set
if(isset($packaging['taxonomy']) && is_string($packaging['taxonomy'])) {
// if we have a product id
if (isset($data['pricing']) && $data['pricing']) {
// get the shipping packaging setting
switch ($data['pricing']) {
case 'uk':
// check if we have a custom shipping
$custom_uk_shipping = get_field('product_custom_shipping',$data['product_id']);
// if we have custom shipping
if($custom_uk_shipping) {
// get parcel box id from product
$data['parcel_id'] = (int)get_field('packaging_shipping',$data['product_id'],false);
// get parcel max items from product
$data['parcel_max_items'] = (int)get_field('packaging_shipping_content',$data['product_id']);
// get custom pallet max parcels
$data['pallet_parcels_max'] = (int)get_field('shipping_pallet_qty',$data['product_id']);
} else {
// get parcel box id
$data['parcel_id'] = (int)get_field('packaging_shipping',$packaging['taxonomy'] . '_' . $packaging['term_id'],false);
// no uk parcel id is found log
PricingNew::error_logger($data['parcel_id'],'No UK parcel found in <a href="'.get_edit_term_link($packaging['term_id'],$packaging['taxonomy']).'" target="_blank">'.$packaging['taxonomy'].'</a>.');
// get parcel max items
$data['parcel_max_items'] = (int)get_field('packaging_shipping_content',$packaging['taxonomy'] . '_' . $packaging['term_id']);
// no uk parcel max items is found log
PricingNew::error_logger($data['parcel_max_items'],'No UK parcel max items is found in <a href="'.get_edit_term_link($packaging['term_id'],$packaging['taxonomy']).'" target="_blank">'.$packaging['taxonomy'].'</a>.');
}
break;
case 'eu':
// check if we have a custom eu shipping
$custom_eu_shipping = get_field('product_custom_eu_shipping',$data['product_id']);
// if we have custom shipping
if($custom_eu_shipping) {
// get parcel box id from product
$data['parcel_id'] = (int)get_field('packaging_eu_shipping',$data['product_id'],false);
// get parcel max items from product
$data['parcel_max_items'] = (int)get_field('packaging_eu_shipping_content',$data['product_id']);
// get custom pallet max parcels
$data['pallet_parcels_max'] = (int)get_field('eu_shipping_pallet_qty',$data['product_id']);
} else {
// get parcel box id
$data['parcel_id'] = (int)get_field('packaging_eu_shipping',$packaging['taxonomy'] . '_' . $packaging['term_id'],false);
// no eu parcel id is found log
PricingNew::error_logger($data['parcel_id'],'No EU parcel found in <a href="'.get_edit_term_link($packaging['term_id'],$packaging['taxonomy']).'" target="_blank">'.$packaging['taxonomy'].'</a>.');
// get parcel content
$data['parcel_max_items'] = (int)get_field('packaging_eu_shipping_content',$packaging['taxonomy'] . '_' . $packaging['term_id']);
// no eu parcel max items is found log
PricingNew::error_logger($data['parcel_max_items'],'No EU parcel max items is found in <a href="'.get_edit_term_link($packaging['term_id'],$packaging['taxonomy']).'" target="_blank">'.$packaging['taxonomy'].'</a>.');
}
break;
}
}
}
}
// return data aray
return $data;
}
public static function get_pallet_data(array $data): array
{
// check we have a packaging id
if(isset($data['parcel_id']) && $data['parcel_id']) {
// check we have outer box
$parcel_outer_parcel = get_field('shipping_outer_box','shipping_' . $data['parcel_id']);
// if we have outer parcel
if($parcel_outer_parcel) {
// set outer parcel id
$data['parcel_outer_id'] = $parcel_outer_parcel;
// if is not set and is false
if(!isset($data['pallet_parcels_max']) || !$data['pallet_parcels_max']) {
// pallet max parcels
$data['pallet_parcels_max'] = (int)get_field('shipping_pallet_qty','shipping_' . $parcel_outer_parcel);
// max outer parcels per pallet error
PricingNew::error_logger($data['pallet_parcels_max'],'Maximum outer boxes per pallet not set in <a href="'.get_edit_term_link($parcel_outer_parcel,'shipping').'" target="_blank">shipping</a>.');
}
// pallet max parcels
$data['pallet_parcels_layer'] = (int)get_field('shipping_pallet_qty_layer','shipping_' . $parcel_outer_parcel);
// pallet outer parcel single layer error
PricingNew::error_logger($data['pallet_parcels_layer'],'Pallet single outer box layer quantity not set in <a href="'.get_edit_term_link($parcel_outer_parcel,'shipping').'" target="_blank">shipping</a>.');
} else {
// if is not set or is false
if(!isset($data['pallet_parcels_max']) || !$data['pallet_parcels_max']) {
// pallet max parcels
$data['pallet_parcels_max'] = (int)get_field('shipping_pallet_qty','shipping_' . $data['parcel_id']);
// max parcels per pallet error
PricingNew::error_logger($data['pallet_parcels_max'],'Maximum boxes per pallet not set in <a href="'.get_edit_term_link($data['parcel_id'],'shipping').'" target="_blank">shipping</a>.');
}
// pallet max parcels
$data['pallet_parcels_layer'] = (int)get_field('shipping_pallet_qty_layer','shipping_' . $data['parcel_id']);
// pallet parcel single layer error
PricingNew::error_logger($data['pallet_parcels_layer'],'Pallet single box layer quantity not set in <a href="'.get_edit_term_link($data['parcel_id'],'shipping').'" target="_blank">shipping</a>.');
}
}
// return data aray
return $data;
}
/**
* @return mixed|void
*/
public static function get_default()
{
// if uk pricing is enabled
if(self::$enabled['uk']) {
// set default services using gb code
return self::$settings['countries']['gb']['services'];
} else {
// get the first country in list
$default_country = reset(self::$settings['countries']);
// set default services using gb code
return $default_country['services'];
}
}
/**
* @param array $data
* @return array
*/
public static function get_unit_weight(array $data): array
{
// add unit weight to data
$data['unit_weight'] = Product::unit_weight($data['product_id']);
// return array
return $data;
}
/**
* @return no-return
*/
public static function render_country_select()
{
// if countries is set and is an array
if (isset(self::$settings['countries']) && is_array(self::$settings['countries'])) { ?>
<div class="form-group mr-md-3">
<select class="form-control" style="padding-right:2.25rem;" name="shipping_country">
<?php foreach (self::$settings['countries'] as $code => $country) { ?>
<?php if (self::$enabled['uk'] && $country['pricing'] === 'uk') { ?>
<option value="<?=$code?>" <?=$code==='gb'?'selected':null?>><?=$country['country']?></option>
<?php } ?>
<?php if (self::$enabled['eu'] && $country['pricing'] === 'eu') { ?>
<option value="<?=$code?>" ><?=$country['country']?></option>
<?php } ?>
<?php } ?>
</select>
</div>
<?php }
}
/**
* @param bool|string $code
* @return no-return
*/
public static function render_service_radio(bool|string $code = false)
{
// if countries is set and is an array
if (isset(self::$settings['countries']) && is_array(self::$settings['countries'])) {
// if we have country code
if ($code) {
// set services using code
$services = self::$settings['countries'][ $code ]['services'];
} else {
// get default
$services = self::get_default();
}
// if we have services
if (is_array($services)) { $uid = rand(); ?>
<div data-group="services" class="form-group">
<?php $i = 0; foreach ($services as $value => $service) { $i++ ?>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="shipping_service" id="service_<?=$value?>_<?=$uid?>" value="<?=$value?>" <?=$i===1?'checked':false?>/>
<label class="form-check-label" for="service_<?=$value?>_<?=$uid?>"><?=$service['service']?></label>
</div>
<?php } ?>
</div>
<?php }
}
}
/**
* @return void
*/
#[NoReturn] public function ajax_load_shipping_services(): void
{
// get the country code
$code = $_GET['country'];
// render service radios for country
self::render_service_radio($code);
// die
die();
}
/**
* @return void
*/
#[NoReturn] public function set_product_vars(): void
{
// global post
global $post;
// check if we are on the right post type and post is set
if(is_singular('product') && isset($post)) {
// check if product shipping has been enabled
$enabled_uk = get_field('product_uk_shipping',$post->ID);
$enabled_eu = get_field('product_eu_shipping',$post->ID);
// update our indivual shipping var
self::$enabled['uk'] = $enabled_uk ?? false;
self::$enabled['eu'] = $enabled_eu ?? false;
// let check for indiviual settings
$individual = get_field('product_individual_shipping',$post->ID);
// if individual shipping
if($individual) {
// individual price fields
$price_uk = get_field('product_individual_shipping_cost',$post->ID);
$price_eu = get_field('product_individual_eu_shipping_cost',$post->ID);
// update our indivual shipping var
self::$individual['uk'] = $price_uk ?? false;
self::$individual['eu'] = $price_eu ?? false;
}
}
}
/**
* @return void
*/
#[NoReturn] public function set_settings(): void
{
// get our shipping options
$globals = get_field('shipping_globals','options');
$countries = get_field('shipping_countries','options');
// if globals is an array
if( is_array($globals) ) {
// for each globals as global
foreach( $globals as $key => $value ) {
// if key string starts with uk
if( str_starts_with($key, 'uk') ) {
// set uk global settings
self::$settings['globals']['uk'][ str_replace('uk_','',$key) ] = $value;
continue;
}
// if key string starts with eu
if( str_starts_with($key, 'eu') ) {
// set eu global settings
self::$settings['globals']['eu'][ str_replace('eu_','',$key) ] = $value;
}
}
}
// if countries is an array
if( is_array($countries) ) {
// for each countries as country
foreach( $countries as $country ) {
// country code
$code = $country['country']['value'];
// set country in settings array
self::$settings['countries'][ $code ]['country'] = $country['country']['label'];
// set pricing in settings array
self::$settings['countries'][ $code ]['pricing'] = $country['pricing']['value'];
// if parcel services is an array
if( is_array($country['services_parcel']) ) {
// for each service's parcel as service parcel
foreach( $country['services_parcel'] as $service_parcel ) {
// parcel service value
$service = $service_parcel['service']['value'];
// set the service name label
self::$settings['countries'][ $code ]['services'][ $service ]['service'] = $service_parcel['service']['label'];
// set parcel service in settings array
self::$settings['countries'][ $code ]['services'][ $service ]['method']['parcel'] = $service_parcel;
// remove service array to label value
unset(self::$settings['countries'][ $code ]['services'][ $service ]['method']['parcel']['service']);
}
}
// if pallet services is an array
if( is_array($country['services_pallet']) ) {
// for each service's pallet as service pallet
foreach( $country['services_pallet'] as $service_pallet ) {
// pallet service value
$service = $service_pallet['service']['value'];
// set pallet service in settings array
self::$settings['countries'][ $code ]['services'][ $service ]['method']['pallet'] = $service_pallet;
// remove service array to label value
unset(self::$settings['countries'][ $code ]['services'][ $service ]['method']['pallet']['service']);
}
}
}
}
// get the countries sub-array
$countries = self::$settings['countries'];
// extract the country sub-element as a separate array for sorting
$country_list = array_column($countries,'country');
// sort the country sub-element array in alphabetical order
array_multisort($country_list,SORT_ASC, SORT_STRING, $countries);
// update the original array with the sorted countries sub-array
self::$settings['countries'] = $countries;
// dump
//dd(self::$settings);
}
/**
* @param $field array
* @return array
*/
public function load_country_choices( array $field ): array
{
// reset choices
$field['choices'] = [];
// select country default null
$field['choices'][''] = 'Select Country...';
// loop through array and add to field 'choices'
if( is_array(self::$countries) ) {
// change keys to lower case
$countries = array_change_key_case(self::$countries);
// for each contries as code => country
foreach( $countries as $code => $country ) {
// add country and code to
$field['choices'][ $code ] = $country;
}
}
// return the field
return $field;
}
/**
* @param $field array
* @return array
*/
public function load_courier_choices( array $field ): array
{
// reset choices
$field['choices'] = [];
// select courier default null
$field['choices'][''] = 'Select Courier...';
// loop through array and add to field 'choices'
if( is_array(self::$uk_couriers) ) {
// for each uk couriers as uk courier
foreach( self::$uk_couriers as $uk_courier ) {
// add uk courier to option group
$field['choices']['UK Couriers'][ $uk_courier ] = $uk_courier;
}
}
// loop through array and add to field 'choices'
if( is_array(self::$eu_couriers) ) {
// for each eu couriers as eu courier
foreach( self::$eu_couriers as $eu_courier ) {
// add eu courier option group
$field['choices']['EU Couriers'][ $eu_courier ] = $eu_courier;
}
}
// return the field
return $field;
}
/**
* @param $field array
* @return array
*/
public function load_service_choices( array $field ): array
{
// reset choices
$field['choices'] = [];
// select services default null
$field['choices'][''] = 'Select Service...';
// loop through array and add to field 'choices'
if( is_array(self::$services) ) {
// for each service as value => label
foreach( self::$services as $value => $label ) {
// add value and label to choices
$field['choices'][ $value ] = $label;
}
}
// return the field
return $field;
}
}
// initiate the shipping class
$shipping = new Shipping();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment