Skip to content

Instantly share code, notes, and snippets.

@passatgt
Created November 14, 2019 23:55
Show Gist options
  • Save passatgt/ea6c0c8a12b6c143db8acc7747300cf8 to your computer and use it in GitHub Desktop.
Save passatgt/ea6c0c8a12b6c143db8acc7747300cf8 to your computer and use it in GitHub Desktop.
Get an array of all available active shipping methods from all zones
<?php
function get_shipping_methods() {
$active_methods = array();
$custom_zones = WC_Shipping_Zones::get_zones();
$worldwide_zone = new WC_Shipping_Zone( 0 );
$worldwide_methods = $worldwide_zone->get_shipping_methods();
foreach ( $custom_zones as $zone ) {
$shipping_methods = $zone['shipping_methods'];
foreach ($shipping_methods as $shipping_method) {
if ( isset( $shipping_method->enabled ) && 'yes' === $shipping_method->enabled ) {
$method_title = $shipping_method->method_title;
$active_methods[$shipping_method->id.':'.$shipping_method->instance_id] = $method_title.' ('.$zone['zone_name'].')';
}
}
}
foreach ($worldwide_methods as $shipping_method_id => $shipping_method) {
if ( isset( $shipping_method->enabled ) && 'yes' === $shipping_method->enabled ) {
$method_title = $shipping_method->method_title;
$active_methods[$shipping_method_id.':'.$shipping_method->instance_id] = $method_title.' (Worldwide)';
}
}
return $active_methods;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment