Show shipping class IDs in WooCommerce - List shipping classes and their IDs for use in custom code. https://www.damiencarbery.com/2022/12/show-shipping-class-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: Show shipping class IDs in WooCommerce | |
Plugin URI: https://www.damiencarbery.com/2022/12/show-shipping-class-ids-in-woocommerce/ | |
Description: List shipping classes and their IDs for use in custom code. | |
Author: Damien Carbery | |
Version: 0.1 | |
*/ | |
class ShowShippingClassIDs { | |
// 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 classes 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 classes page. | |
private function is_shipping_classes_page() { | |
global $current_section; | |
if ( !empty( $current_section ) && 'classes' == $current_section ) { | |
return true; | |
} | |
return false; | |
} | |
public function add_shipping_method_ids_css() { | |
if ( $this->is_shipping_classes_page() ) { | |
?> | |
<style> | |
.shipping_classes_ids { border: 1px solid #c3c4c7; } | |
.shipping_classes_ids td, .shipping_classes_ids th { padding: 5px 10px; background-color: #fff; } | |
.shipping_classes_ids td { border-bottom: 2px solid #f9f9f9; } | |
.shipping_classes_ids ul { margin: 0; } | |
</style> | |
<?php | |
} | |
} | |
// Print the table with Shipping classes and their IDs. | |
public function add_to_shipping_settings() { | |
if ( $this->is_shipping_classes_page() ) { | |
?> | |
<h4>Shipping classes IDs</h4> | |
<table class="shipping_classes_ids"><tr><th>Class name</th><th>ID</th></tr> | |
<?php | |
// Retrieve all the shipping classes. | |
$terms = get_terms( array( 'taxonomy' => 'product_shipping_class' ) ); | |
foreach ( $terms as $term ) { | |
printf( '<tr><td>%s</td><td><strong>%s</strong></td></tr>%s', $term->name, $term->term_id, "\n"); | |
} | |
?> | |
</table> | |
<?php | |
} | |
} | |
} | |
$ShowShippingClassIDs = new ShowShippingClassIDs(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment