B2B Bulk Discount Matrix Table
If a product belongs to a specific category, this snippet reads the core WooCommerce quantity price breaks and automatically outputs a clean, semantic HTML table on the product single page showing wholesale pricing tiers
This Is Good For Website Having Wholesale Pricing and Want To Showcase The Table With Specific Quantity For Wholesale. Like Its a Display Dynamic Wholesale Price On The Product Page Automatically.
What’s The Use of This Snippet ?
It Bridges The Gap Between Retail and Wholesale Setups Without Needing a Complex Premium B2B Plugins.
Snippet Preview :
https://aetherpress.in/wp-content/uploads/2026/05/ss.png
<?php
/**
* B2B Bulk Discount Matrix Table
* ----------------------------------------
* Outputs a semantic wholesale-pricing-tier table on single product pages
* for products that belong to a configurable category (default: "wholesale").
*
* Drop this into your child theme's functions.php OR save as
* /wp-content/mu-plugins/bulk-discount-matrix.php
*
*/
defined( 'ABSPATH' ) || exit;
/**
* Category slug (or array of slugs) that triggers the matrix.
* Override via the `bdm_target_categories` filter.
*/
function bdm_get_target_categories() {
return apply_filters( 'bdm_target_categories', array( 'wholesale' ) );
}
/**
* Resolve the active tier set for a given product.
*
* @param int $product_id
* @return array
*/
function bdm_get_discount_tiers( $product_id ) {
$default_tiers = array(
array( 'min_qty' => 10, 'max_qty' => 49, 'discount' => 10 ),
array( 'min_qty' => 50, 'max_qty' => 99, 'discount' => 20 ),
array( 'min_qty' => 100, 'max_qty' => 0, 'discount' => 30 ),
);
$product_tiers = get_post_meta( $product_id, '_bulk_discount_tiers', true );
$tiers = ( is_array( $product_tiers ) && ! empty( $product_tiers ) )
? $product_tiers
: $default_tiers;
return apply_filters( 'bdm_discount_tiers', $tiers, $product_id );
}
/**
* Get the base unit price we apply the discount to.
* Falls back gracefully for variable products.
*
* @param WC_Product $product
* @return float
*/
function bdm_get_base_price( $product ) {
$price = (float) $product->get_regular_price();
if ( ! $price && $product->is_type( 'variable' ) ) {
$price = (float) $product->get_variation_regular_price( 'min' );
}
return $price;
}
/**
* Render the bulk discount matrix on the single product page.
*/
function bdm_render_discount_table() {
global $product;
if ( ! $product instanceof WC_Product ) {
return;
}
$product_id = $product->get_id();
$target_cats = bdm_get_target_categories();
if ( ! has_term( $target_cats, 'product_cat', $product_id ) ) {
return;
}
$tiers = bdm_get_discount_tiers( $product_id );
if ( empty( $tiers ) ) {
return;
}
$base_price = bdm_get_base_price( $product );
if ( $base_price <= 0 ) {
return;
}
// Sort tiers by min_qty ascending for clean output.
usort( $tiers, function ( $a, $b ) {
return (int) $a['min_qty'] <=> (int) $b['min_qty'];
} );
?>
<section class="bdm-bulk-discount-matrix" aria-labelledby="bdm-heading-<?php echo esc_attr( $product_id ); ?>">
<h3 id="bdm-heading-<?php echo esc_attr( $product_id ); ?>" class="bdm-title">
<?php esc_html_e( 'Wholesale Pricing Tiers', 'bdm' ); ?>
</h3>
<table class="bdm-table">
<caption class="screen-reader-text">
<?php esc_html_e( 'Volume discount pricing by quantity', 'bdm' ); ?>
</caption>
<thead>
<tr>
<th scope="col"><?php esc_html_e( 'Quantity', 'bdm' ); ?></th>
<th scope="col"><?php esc_html_e( 'Discount', 'bdm' ); ?></th>
<th scope="col"><?php esc_html_e( 'Unit Price', 'bdm' ); ?></th>
<th scope="col"><?php esc_html_e( 'You Save / Unit', 'bdm' ); ?></th>
</tr>
</thead>
<tbody>
<?php foreach ( $tiers as $tier ) :
$min = isset( $tier['min_qty'] ) ? (int) $tier['min_qty'] : 0;
$max = isset( $tier['max_qty'] ) ? (int) $tier['max_qty'] : 0;
$discount = isset( $tier['discount'] ) ? (float) $tier['discount'] : 0;
if ( $min <= 0 || $discount <= 0 ) {
continue;
}
$qty_label = $max > 0
? sprintf( '%d – %d', $min, $max )
: sprintf( /* translators: %d: minimum quantity */ esc_html__( '%d+', 'bdm' ), $min );
$tier_price = $base_price * ( 1 - ( $discount / 100 ) );
$savings = $base_price - $tier_price;
?>
<tr>
<th scope="row" class="bdm-qty"><?php echo wp_kses_post( $qty_label ); ?></th>
<td class="bdm-discount"><?php echo esc_html( rtrim( rtrim( number_format( $discount, 2 ), '0' ), '.' ) ); ?>%</td>
<td class="bdm-price"><?php echo wp_kses_post( wc_price( $tier_price ) ); ?></td>
<td class="bdm-savings"><?php echo wp_kses_post( wc_price( $savings ) ); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<p class="bdm-note">
<small><?php esc_html_e( 'Discounts are applied automatically at checkout based on cart quantity.', 'bdm' ); ?></small>
</p>
</section>
<?php
}
add_action( 'woocommerce_single_product_summary', 'bdm_render_discount_table', 25 );
/**
* Lightweight inline styles.
*/
function bdm_inline_styles() {
if ( ! function_exists( 'is_product' ) || ! is_product() ) {
return;
}
?>
<style id="bdm-inline-css">
.bdm-bulk-discount-matrix {
margin: 1.5rem 0;
padding: 1rem 1.25rem;
border: 1px solid #e5e7eb;
border-radius: 8px;
background: #f9fafb;
}
.bdm-bulk-discount-matrix .bdm-title {
margin: 0 0 .75rem;
font-size: 1.05rem;
font-weight: 700;
letter-spacing: .01em;
}
.bdm-table {
width: 100%;
border-collapse: collapse;
font-size: .95rem;
}
.bdm-table th,
.bdm-table td {
padding: .65rem .85rem;
text-align: left;
border-bottom: 1px solid #e5e7eb;
}
.bdm-table thead th {
background: #111827;
color: #fff;
font-weight: 600;
font-size: .85rem;
text-transform: uppercase;
letter-spacing: .03em;
}
.bdm-table tbody tr:nth-child(even) { background: #ffffff; }
.bdm-table tbody tr:hover { background: #eef2ff; }
.bdm-table tbody th.bdm-qty { font-weight: 600; }
.bdm-table .bdm-discount,
.bdm-table .bdm-price,
.bdm-table .bdm-savings { font-variant-numeric: tabular-nums; }
.bdm-bulk-discount-matrix .bdm-note { margin: .75rem 0 0; color: #6b7280; }
@media (max-width: 480px) {
.bdm-table th,
.bdm-table td { padding: .5rem .55rem; font-size: .9rem; }
}
</style>
<?php
}
add_action( 'wp_head', 'bdm_inline_styles' );