Untitled
unknown
plain_text
2 years ago
1.3 kB
21
Indexable
add_filter('woocommerce_order_item_name', 'add_discount_info_to_order_emails', 10, 3);
function add_discount_info_to_order_emails($item_name, $item, $is_visible) {
$order = $item->get_order();
$total_discount = 0;
$order_total = 0;
// Calculate the total discount from order fees (adjust according to how discounts are stored in your orders)
foreach ($order->get_items('fee') as $fee) {
if ($fee->get_total() < 0) { // Assuming discounts are negative.
$total_discount += abs($fee->get_total());
}
}
// Calculate the total order value excluding shipping, taxes, and discounts.
foreach ($order->get_items() as $line_item) {
$order_total += $line_item->get_total(); // Get total excluding taxes.
}
// Avoid division by zero
if ($order_total <= 0 || $total_discount <= 0) {
return $item_name;
}
// Calculate this item's share of the discount.
$item_total = $item->get_total();
$item_discount = ($item_total / $order_total) * $total_discount;
// If there's a discount, format it and append it to the item name.
if ($item_discount > 0) {
$discount_formatted = wc_price($item_discount);
$item_name .= sprintf('<br><small>%s discount applied.</small>', $discount_formatted);
}
return $item_name;
}Editor is loading...
Leave a Comment