After a customer completes a purchase in your WooCommerce store, they get redirected to the Order Received page.
This Order Received page contains information about the purchase, like the Order Number, the customer’s full name, email address, and payment method used, and there is also a table featuring the list of purchased items called Order Details.
Each item in the Order Details table usually has the Product name, Amount, Price, and, at the very end, a calculation of the order Subtotal, Shipping, Taxes (if any), and if you offered a discount, the discount for the total purchase.
Let’s say you are running a special deal to get sales rolling. You want to show your customers how much they are saving per item after the purchase.
Nice idea.
The problem is that WooCommerce does not have an option to display the discount per product.
So, how can you add one?
You can leverage the woocommerce_order_item_meta_end action.
In short, this action hook allows you to add custom content at the end of each order item.
The woocommerce_order_item_meta_end action allows you to use three parameters: item ID, item, and order.
First, you want to get the original item price, dividing the item subtotal by the item quantity the customer purchased.
Now, get the actual price paid per item, dividing the item total by the item quantity.
In WooCommerce, you may create Coupons that only apply to specific products. You may want to run the code only for products that qualify for the discount. Non-discounted products would not need to display a discounted price.
So, if the price the customer paid per a specific item is less than the original price of the same item, then show the discounted amount for that product.
An item will be excluded from the function when it fails this condition.
The resulting code looks like this:
add_action('woocommerce_order_item_meta_end', 'display_product_discount', 10, 3);
function display_product_discount($item_id, $item, $order) {
// Get the original product price from the order item data
$original_price = (float) $item->get_subtotal() / $item->get_quantity();
// Get the actual price the customer paid for the product in the order
$order_price_per_item = (float) $item->get_total() / $item->get_quantity();
// Check if the product was discounted
if ($order_price_per_item < $original_price) {
// Calculate the discount per product
$discount_per_product = $original_price - $order_price_per_item;
// Display discount per product and discounted price with inline CSS
echo '<div class="product-discount">';
echo 'Discount: ' . wc_price($discount_per_product) . '<br>';
echo '<span style="color: green; font-weight: bold;">Price after discount: ' . wc_price($order_price_per_item) . '</span>';
echo '</div>';
}
}
This is the final result