I used Add the Coupon Code names to Woocommerce View Order details and email notifications to inspire my snippet.
I'm trying to expand the snippet to also include the coupon percentage amount in brackets, if the coupon type is a percentage discount.
This is what it should look like:
Here is my attempt. Any ideas if this is correct:
add_filter( 'woocommerce_get_order_item_totals', 'add_coupons_codes_line_to_order_totals_lines', 10, 3 );
function add_coupons_codes_line_to_order_totals_lines( $total_rows, $order, $tax_display ) {
// Exit if there is no coupons applied
if( sizeof( $order->get_used_coupons() ) == 0 )
return $total_rows;
$new_total_rows = []; // Initializing
foreach($total_rows as $key => $total ){
$new_total_rows[$key] = $total;
if( $key == 'discount' ){
// Get applied coupons
$applied_coupons = $order->get_used_coupons();
if( $applied_coupons->discount_type == 'percent'){
// Get applied coupon percentge
$applied_coupons_percentage = $applied_coupons->coupon_amount;
}
// Insert applied coupon codes in total lines after discount line
$new_total_rows['coupon_codes'] = array(
'label' => __('Applied coupons:', 'woocommerce'),
'value' => implode( ', ', $applied_coupons '<p> ({$applied_coupons_percentage}%)</p>' ),
);
}
}
return $new_total_rows;
}
I use this to display the coupon percentage on the cart page. I tried to incorporate this into my snippet above:
function my_coupon_percentage_cart($value, $coupon)
{
if($coupon->discount_type == 'percent' && !empty($coupon->coupon_amount))
{
$amt = "<br><br><p><em><strong>{$coupon->coupon_amount}% OFF on Your Order</strong></em></p>";
}
return $value.$amt;
}
add_filter('woocommerce_cart_totals_coupon_html','my_coupon_percentage_cart',10,2);