Add the Coupon Code names to Woocommerce View Order details and email notifications

后端 未结 1 1019
死守一世寂寞
死守一世寂寞 2021-01-06 08:39

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

1条回答
  •  时光取名叫无心
    2021-01-06 09:20

    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:


    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:


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

    0 讨论(0)
提交回复
热议问题