Untitled

webwizards avatar
webwizards
plain_text
02/22/2026 3:09 PM
2.4 KB
10
Indexable
add_action( 'woocommerce_after_order_itemmeta', 'b2bking_show_price_breakdown_in_admin', 10, 3 );

function b2bking_show_price_breakdown_in_admin( $item_id, $item, $product ) {

    // Only run in admin and only for line items (not shipping, fees, etc.)
    if ( ! is_admin() || ! ( $item instanceof WC_Order_Item_Product ) ) {
        return;
    }

    // Get the product (handle variations too)
    if ( ! $product ) {
        $product = $item->get_product();
    }

    if ( ! $product ) {
        return;
    }

    // Get the regular price from the product
    $regular_price = floatval( get_post_meta($product->get_id(),'_regular_price', true) );

    // Get the price actually paid per unit (line total / quantity)
    $qty = $item->get_quantity();
    if ( $qty <= 0 ) {
        return;
    }

    $line_total = floatval( $item->get_total() );
    $paid_per_unit = $line_total / $qty;

    // Only show breakdown if there's actually a discount
    if ( $regular_price <= 0 || $paid_per_unit >= $regular_price ) {
        return;
    }

    // Calculate discount percentage
    $discount_pct = round( ( ( $regular_price - $paid_per_unit ) / $regular_price ) * 100, 2 );

    // Format prices using WooCommerce formatting
    $regular_formatted = wc_price( $regular_price );
    $paid_formatted    = wc_price( $paid_per_unit );

    // Output the breakdown
    echo '<div class="b2bking-price-breakdown" style="margin-top:8px; padding:6px 10px; background:#f8f8f8; border-left:3px solid #2271b1; font-size:12px; line-height:1.6;">';
    echo '<strong>Price Breakdown:</strong><br>';
    echo 'Regular price: ' . $regular_formatted . '<br>';
    echo 'Discount: <span style="color:#d63638; font-weight:600;">' . esc_html( $discount_pct ) . '%</span><br>';
    echo 'Discounted price: ' . $paid_formatted;
    echo '</div>';
}
Editor is loading...
Leave a Comment