Last active
October 24, 2022 17:16
-
-
Save damiencarbery/f0bdfdd331e0f58dd0ee84a51a91bdaf to your computer and use it in GitHub Desktop.
Add Shipping Classes column to Products admin page - Add a new column to the WooCommerce Products admin page. https://www.damiencarbery.com/2022/10/add-shipping-classes-column-to-products-admin-page/
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 Shipping Classes column to Products listing | |
Plugin URI: https://www.damiencarbery.com/2022/10/add-shipping-classes-column-to-products-admin-page/ | |
Description: Add a Shipping Class column to the WooCommerce Products listing pages. Requested in WooCommerce Community Facebook group: https://www.facebook.com/groups/advanced.woocommerce/posts/6186336834714017/. Inspired by https://www.conicsolutions.net/tutorials/woocommerce-how-to-add-custom-columns-on-the-products-list-in-dashboard/ | |
Author: Damien Carbery | |
Version: 0.2 | |
*/ | |
class AddShippingClassColumn { | |
// Column key and title. | |
private $column_key; | |
private $column_title; | |
// 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->column_key = 'shipping_class'; | |
$this->column_title = 'Shipping <br/>class'; | |
$this->init(); | |
} | |
// Set up WordPress specfic actions. | |
public function init() { | |
// Add the new column. | |
add_filter( 'manage_product_posts_columns', array( $this, 'set_custom_column' ), 20 ); | |
// Populate the new column with the shipping class name. | |
add_action( 'manage_product_posts_custom_column' , array( $this, 'populate_custom_column' ), 10, 2 ); | |
// Add CSS to ensure new column width is not distored. | |
add_action( 'admin_head', array( $this, 'add_column_css' ) ); | |
} | |
// Add the new column. | |
public function set_custom_column( $columns ) { | |
// Comment this variable out to add column to the end. | |
$insert_after = 'product_cat'; | |
// If $insert_after is set, and found, add the new column after it. | |
if ( isset( $insert_after ) ) { | |
$position = array_search( $insert_after, array_keys( $columns ) ); | |
if ( false !== $position ) { | |
$before = $columns; | |
$after = $columns; | |
array_splice( $before, $position + 1 ); | |
array_splice( $after, 0, $position + 1 ); | |
$before[ $this->column_key ] = $this->column_title; // To avoid wrapping. Add space for 'Screen Options' panel. | |
$columns = array_merge( $before, $after ); | |
} | |
} | |
else { | |
// Otherwise add the new column at the end. | |
$columns[ $this->column_key ] = $this->column_title; | |
} | |
return $columns; | |
} | |
// Populate the new column with the shipping class name. | |
public function populate_custom_column( $column, $post_id ) { | |
// Use switch() to allow for additional columns in the future. | |
switch ( $column ) { | |
case 'shipping_class': | |
$product = wc_get_product( $post_id ); | |
// Copy code from WC_Product::get_shipping_class() but echo term name instead of slug. | |
$class_id = $product->get_shipping_class_id(); | |
if ( $class_id ) { | |
$term = get_term_by( 'id', $class_id, 'product_shipping_class' ); | |
if ( $term && ! is_wp_error( $term ) ) { | |
echo $term->name; | |
break; | |
} | |
} | |
echo '<span class="na">–</span>'; // No shipping class. | |
break; | |
} | |
} | |
// Add CSS to ensure new column width is not distored. | |
public function add_column_css() { | |
$currentScreen = get_current_screen(); | |
if ( isset( $currentScreen ) ) { | |
// base = edit | |
// id = edit-product | |
//error_log( 'get_current_screen: ' . var_export( $currentScreen, true ) ); | |
if ( 'edit-product' == $currentScreen->id ) { | |
?> | |
<style>table.wp-list-table .column-shipping_class { width: 11% !important; }</style> | |
<?php | |
} | |
} | |
} | |
} | |
$AddShippingClassColumn = new AddShippingClassColumn(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment