WooCommerce - Show shipping method IDs - List shipping zones, shipping methods and their IDs for use in custom code. https://www.damiencarbery.com/2022/10/show-shipping-method-ids-in-woocommerce/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: WooCommerce - Show shipping method IDs | |
Plugin URI: https://www.damiencarbery.com/2022/10/show-shipping-method-ids-in-woocommerce/ | |
Description: List shipping zones, shipping methods and their IDs for use in custom code. | |
Author: Damien Carbery | |
Version: 0.1 | |
*/ | |
class ShowShippingMethodIDs { | |
// Returns an instance of this class. | |
public static function get_instance() { | |
if ( null == self::$instance ) { | |
self::$instance = new self; | |
} | |
return self::$instance; | |
} | |
// Initialize the plugin variables. | |
public function __construct() { | |
$this->init(); | |
} | |
// Set up WordPress specfic actions. | |
public function init() { | |
// Print the table with Shipping zones, methods and their IDs. | |
add_action( 'woocommerce_settings_shipping', array( $this, 'add_to_shipping_settings' ), 20 ); | |
// Add CSS to style the table. | |
add_action( 'admin_head', array( $this, 'add_shipping_method_ids_css' ) ); | |
} | |
// Return whether on the Shipping zones main page. | |
private function is_shipping_zones_page() { | |
global $current_section; | |
if ( empty( $current_section ) && !isset( $_GET['zone_id'] ) ) { | |
return true; | |
} | |
return false; | |
} | |
public function add_shipping_method_ids_css() { | |
if ( $this->is_shipping_zones_page() ) { | |
?> | |
<style> | |
.shipping_method_ids { border: 1px solid #c3c4c7; } | |
.shipping_method_ids td, .shipping_method_ids th { padding: 5px 10px; background-color: #fff; } | |
.shipping_method_ids td { border-bottom: 2px solid #f9f9f9; } | |
.shipping_method_ids ul { margin: 0; } | |
</style> | |
<?php | |
} | |
} | |
// Print the table with Shipping zones, methods and their IDs. | |
public function add_to_shipping_settings() { | |
if ( $this->is_shipping_zones_page() ) { | |
$data_store = WC_Data_Store::load( 'shipping-zone' ); | |
$raw_zones = $data_store->get_zones(); | |
foreach ( $raw_zones as $raw_zone ) { | |
$zones[] = new WC_Shipping_Zone( $raw_zone ); | |
} | |
?> | |
<h4>Shipping Method IDs</h4> | |
<p>The ID is useful when disabling a method via the '<em>woocommerce_package_rates</em>' filter.</p> | |
<table class="shipping_method_ids"><tr><th>Zone name</th><th>Method name (Type): ID</th></tr> | |
<?php | |
foreach ( $zones as $zone ) { | |
$zone_shipping_methods = $zone->get_shipping_methods(); | |
if ( count( $zone_shipping_methods ) ) { | |
?> | |
<tr><th valign="top"><?php echo $zone->get_zone_name() ?></th> | |
<td> | |
<ul> | |
<?php | |
foreach ( $zone_shipping_methods as $index => $method ) { | |
$method_title = $method->get_method_title(); // e.g. "Flat Rate" | |
$method_user_title = $method->get_title(); // e.g. whatever you renamed "Flat Rate" into | |
$method_rate_id = $method->get_rate_id(); // e.g. "flat_rate:18" | |
printf( '<li>%s (%s): <strong>%s</strong></li>%s', $method_user_title, $method_title, $method_rate_id, "\n" ); | |
} | |
?> | |
</ul> | |
</td></tr> | |
<?php | |
} | |
} | |
?> | |
</table> | |
<?php | |
} | |
} | |
} | |
$ShowShippingMethodIDs = new ShowShippingMethodIDs(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment