在 WooCommerce 订单电子邮件通知中显示使用过的优惠券

Display used coupon(s) in WooCommerce order email notifications

我有一个显示已用优惠券的代码:

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
}

以及在 WooCommerce table 行中显示信息的代码:

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;
}

如何将使用过的优惠券转移到价值字段?像这样 'value' => 'used_coupon'

由于您可以通过 woocommerce_get_order_item_totals 挂钩访问 $order 对象,因此您可以以相同的方式应用它。

所以你得到:

// 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 );