aivakha
aivakha

Reputation: 43

Display used coupon(s) in WooCommerce order email notifications

I have a code that displays an used coupon:

add_action( 'woocommerce_email_after_order_table', 'add_payment_method_to_admin_new_order', 15, 2 );
function add_payment_method_to_admin_new_order( $order, $is_admin_email ) {

        if( $order->get_used_coupons() ) {

            $coupons_count = count( $order->get_used_coupons() );
            $i = 1;
            $coupons_list = '';
            foreach( $order->get_used_coupons() as $coupon) {
                $coupons_list .=  $coupon;
                if( $i < $coupons_count )
                    $coupons_list .= ', ';
                $i++;
            }
            echo '<p></p>';
            echo '<p><strong>Купон:</strong> ' . $coupons_list . '</p>';

        } // endif get_used_coupons
}

And the code that displays the information in the WooCommerce table row:

add_filter( 'woocommerce_get_order_item_totals', 'bbloomer_add_recurring_row_email', 10, 2 );
function bbloomer_add_recurring_row_email( $total_rows, $myorder_obj ) {
    $total_rows['recurr_not'] = array(
    'label' => __( 'Купон:', 'woocommerce' ),
    'value'   => 'blabla'
    );

return $total_rows;
}

How can I transfer the used coupon to the value field ? Like this 'value' => 'used_coupon'

Upvotes: 2

Views: 853

Answers (2)

Mohamed Mostafa
Mohamed Mostafa

Reputation: 1

for those how interested, the updated code is


// Function to display used coupons in email notifications
function action_woocommerce_email_after_order_table( $order, $sent_to_admin, $plain_text, $email ) {
    // Get used coupons
    $coupons = $order->get_coupon_codes();

    if ( ! empty( $coupons ) ) {
        echo '<p><strong>Coupon</strong> ' . implode( ', ', $coupons ) . '</p>';
    }
}
add_action( 'woocommerce_email_after_order_table', 'action_woocommerce_email_after_order_table', 10, 4 );

// Function to display used coupons in order totals
function filter_woocommerce_get_order_item_totals( $total_rows, $order, $tax_display ) {
    // Get used coupons
    $coupons = $order->get_coupon_codes();

    if ( ! empty( $coupons ) ) {
        $total_rows['recurr_not'] = array(
            'label' => __( 'Coupon', 'woocommerce' ),
            'value' => implode( ', ', $coupons ),
        );
    }

    return $total_rows;
}
add_filter( 'woocommerce_get_order_item_totals', 'filter_woocommerce_get_order_item_totals', 10, 3 );

Upvotes: 0

7uc1f3r
7uc1f3r

Reputation: 29614

Since you have access to the $order object via the woocommerce_get_order_item_totals hook, you can apply this in the same way.

So you get:

// After order table
function action_woocommerce_email_after_order_table( $order, $sent_to_admin, $plain_text, $email ) {
    // Get used coupons
    if ( $order->get_used_coupons() ) {
        // Total
        $coupons_count = count( $order->get_used_coupons() );

        // Initialize
        $i = 1;
        $coupons_list = '';

        // Loop through
        foreach ( $order->get_used_coupons() as $coupon ) {
            // Append
            $coupons_list .=  $coupon;

            if ( $i < $coupons_count )
                $coupons_list .= ', ';

            $i++;
        }

        // Output
        echo '<p><strong>Купон:</strong> ' . $coupons_list . '</p>';
    }
}
add_action( 'woocommerce_email_after_order_table', 'action_woocommerce_email_after_order_table', 10, 4 );

// Display on customer orders and email notifications
function filter_woocommerce_get_order_item_totals( $total_rows, $order, $tax_display ) {
    // Get used coupons
    if ( $order->get_used_coupons() ) {
        // Total
        $coupons_count = count( $order->get_used_coupons() );

        // Initialize
        $i = 1;
        $coupons_list = '';

        // Loop through
        foreach ( $order->get_used_coupons() as $coupon ) {
            // Append
            $coupons_list .=  $coupon;

            if ( $i < $coupons_count )
                $coupons_list .= ', ';

            $i++;
        }

        // Output
        $total_rows['recurr_not'] = array(
            'label'     => __( 'Купон:', 'woocommerce' ),
            'value'     => $coupons_list,
        );
    }

    return $total_rows;
}
add_filter( 'woocommerce_get_order_item_totals', 'filter_woocommerce_get_order_item_totals', 10, 3 );

Upvotes: 1

Related Questions