Skip to content

Instantly share code, notes, and snippets.

@damiencarbery
Created December 3, 2022 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save damiencarbery/b213d1bfb3b0a818de99a63ffd52908e to your computer and use it in GitHub Desktop.
Save damiencarbery/b213d1bfb3b0a818de99a63ffd52908e to your computer and use it in GitHub Desktop.
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/
<?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