4

I noticed that in the View Order Details and email confirmations, it reflects a discount line, but doesn't state the actual Discount Code used. Furthermore, if the discount code is $0.00 (we sometimes have a $0 code for special tracking purposes), it won't even show the code at all. I spent all day trying to find a solution -- can someone give some guidance on this? Thanks.

I got this working so far to get the actual coupon code:

add_action( 'woocommerce_order_details_after_order_table', 'custom_woocommerce_coupon_line' );
function custom_woocommerce_coupon_line( $order_id ) {
    $order    = wc_get_order( $order_id );

    // An order can have no used coupons or also many used coupons
    $coupons  = $order->get_used_coupons();
    $coupons  = count($coupons) > 0 ? implode(',', $coupons) : '';
    echo $coupons;
 }

But can't figure out how to get it into the 'Discount' line... nor why the Discount line doesn't even appear when it's a $0 item with a $0 code used.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
user2337231
  • 109
  • 2
  • 10

1 Answers1

10

Updated - Handling discounts with a zero value

The following code will after "discount" line in order totals lines, displaying the applied coupons to the order:

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();
            // Insert applied coupon codes in total lines after discount line
            $new_total_rows['coupon_codes'] = array(
                'label' => __('Applied coupons:', 'woocommerce'),
                'value' => implode( ', ', $applied_coupons ),
            );
        }
    }

    return $new_total_rows;
}

Display on customer order view, with 2 applied coupons:

enter image description here


Additional code version: Handle applied coupons with a zero discount amount use this instead:

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 ) {
    $has_used_coupons = sizeof( $order->get_used_coupons() ) > 0 ? true : false;

    // Exit if there is no coupons applied
    if( ! $has_used_coupons )
        return $total_rows;

    $new_total_rows  = []; // Initializing
    $applied_coupons = $order->get_used_coupons(); // Get applied coupons

    foreach($total_rows as $key => $total ){
        $new_total_rows[$key] = $total;

        // Adding the discount line for orders with applied coupons and zero discount amount
        if( ! isset($total_rows['discount']) && $key === 'shipping' ) {
            $new_total_rows['discount'] = array(
                'label' => __( 'Discount:', 'woocommerce' ),
                'value'    => wc_price(0),
            );
        }

        // Adding applied coupon codes line
        if( $key === 'discount' || isset($new_total_rows['discount']) ){
            // Get applied coupons
            $applied_coupons = $order->get_used_coupons();
            // Insert applied coupon codes in total lines after discount line
            $new_total_rows['coupon_codes'] = array(
                'label' => __('Applied coupons:', 'woocommerce'),
                'value' => implode( ', ', $applied_coupons ),
            );
        }
    }

    return $new_total_rows;
}

Display on email notifications with a coupon that has 0 discount:

enter image description here


Code goes in function.php file of your active child theme (or active theme). Tested and works.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • This works! You are awesome. I found many solutions for my project based on your answers and you have been my savior even without engaging with me :) This unfortunately still doesn't show the coupon code used for the $0.00 item + $0 coupon scenario (we hacked together the ability for customers to redeem a free item but with a required coupon), but that's OK I've decided to use my previous code to display a separate line for these special types of codes. Will this code you submitted also work for order confirmations if I adapted it to the filter woocommerce_email_order_items_table? – user2337231 Feb 07 '19 at 23:51
  • @user2337231 I have just updated the code to handle the coupons code display even when the discount amount is equal to zero. The code handle also the email notifications where the coupon codes are displayed too. – LoicTheAztec Feb 07 '19 at 23:59
  • Hmmm... still doesn't seem to work... maybe it is because the item is $0.00 or could be conflicting with another filter? Screenshot: [link](https://imgur.com/a/U06Cgzs) – user2337231 Feb 08 '19 at 00:04
  • Hmmm must be a conflict with another piece of code in functions.... no matter, I can use the alternative solution for the $0 item + $0 code scenario, you've already been super helpful. Last question if I'm not being too bothersome: could I apply your solution directly to the "woocommerce_email_order_items_table" action? – user2337231 Feb 08 '19 at 00:14
  • @user2337231 I have re-updated the code… I have added a special code version for you… try it, it should work. – LoicTheAztec Feb 08 '19 at 00:49
  • Thanks so much for your effort and assistance... still doesn't seem to work for the $0 item + $0 code scenario. Don't worry about it though you've been more than helpful! – user2337231 Feb 08 '19 at 05:07