Skip to content

Instantly share code, notes, and snippets.

@abidkakkur11
Last active November 14, 2025 03:46
Show Gist options
  • Select an option

  • Save abidkakkur11/47d33a36398a7487fc418e964bed62bf to your computer and use it in GitHub Desktop.

Select an option

Save abidkakkur11/47d33a36398a7487fc418e964bed62bf to your computer and use it in GitHub Desktop.
Displaying the sale percentage on the Woocommerce admin table
<?php
/**
* Add a custom column for displaying the sale percentage in the WooCommerce Products admin table.
*
* @param array $columns Existing columns in the Products table.
* @return array Modified columns with the Sale Percentage column added.
*/
function add_sale_percentage_column_to_products( $columns ) {
// Add a new column for Sale Percentage
$columns['sale_percentage'] = 'Sale Percentage';
return $columns;
}
add_filter( 'manage_edit-product_columns', 'add_sale_percentage_column_to_products' );
/**
* Display the sale percentage in the custom column for each product.
*
* @param string $column The name of the current column being rendered.
* @param int $post_id The ID of the current product post.
*/
function display_sale_percentage_in_column( $column, $post_id ) {
if ( $column === 'sale_percentage' ) {
// Get the WooCommerce product object
$product = wc_get_product( $post_id );
if ( $product->is_type( 'variable' ) ) {
// For variable products, calculate the average sale percentage across all variations
$variations = $product->get_children(); // Get all variation IDs
$total_percentage = 0;
$count = 0;
foreach ( $variations as $variation_id ) {
$variation = wc_get_product( $variation_id ); // Get the variation product object
$regular_price = $variation->get_regular_price();
$sale_price = $variation->get_sale_price();
if ( $regular_price && $sale_price ) {
// Calculate the sale percentage for the variation
$percentage = round( ( ( $regular_price - $sale_price ) / $regular_price ) * 100 );
$total_percentage += $percentage;
$count++;
}
}
// Display the average sale percentage or a dash if no variations are on sale
echo $count > 0 ? round( $total_percentage / $count ) . '%' : '—';
} elseif ( $product->is_on_sale() ) {
// For simple products, calculate and display the sale percentage
$regular_price = $product->get_regular_price();
$sale_price = $product->get_sale_price();
if ( $regular_price && $sale_price ) {
$percentage = round( ( ( $regular_price - $sale_price ) / $regular_price ) * 100 );
echo $percentage . '%';
} else {
echo '—';
}
} else {
// If the product is not on sale, display a dash
echo '—';
}
}
}
add_action( 'manage_product_posts_custom_column', 'display_sale_percentage_in_column', 10, 2 );
/**
* Make the Sale Percentage column sortable in the WooCommerce Products admin table.
*
* @param array $columns Existing sortable columns.
* @return array Modified sortable columns with the Sale Percentage column added.
*/
function make_sale_percentage_column_sortable( $columns ) {
$columns['sale_percentage'] = 'sale_percentage'; // Add the Sale Percentage column as sortable
return $columns;
}
add_filter( 'manage_edit-product_sortable_columns', 'make_sale_percentage_column_sortable' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment