在 WooCommerce 订单电子邮件中显示使用过的优惠券 + 特定消息

Display used coupons + specific message on WooCommerce order Emails

我正在尝试在 WooCommerce 订单电子邮件中显示使用过的优惠券 + 根据使用过的优惠券添加特定消息。

显示优惠券的工作基于

Add Applied Coupon Code in Admin New Order Email Template - WooCommerce


我试图通过检查字符串 $coupon_codes 是否包含特定的 words/specific 优惠券来扩展它,在本例中为 "grouppurchase" , 然后显示一条消息。

它需要用于 $coupon_codes,因为它可以(并且将会)与多张优惠券一起使用

我设法让它与以下代码一起工作

How do I check if a string contains a specific word?

问题是 (strpos) 它会在使用任何优惠券时显示消息,与优惠券中写的无关紧要,我不明白为什么

我遵循这个答案背后的 strpos 逻辑

// Display used Coupons on Emails and Add GroupPurchase Warning
add_action( 'woocommerce_email_after_order_table', 'display_applied_coupons', 10, 4 );
function display_applied_coupons( $order, $sent_to_admin, $plain_text, $email ) {

    // Only for admins and when there at least 1 coupon in the order
    if ( ! $sent_to_admin && count($order->get_items('coupon') ) == 0 ) return;

    foreach( $order->get_items('coupon') as $coupon ){
        $coupon_codes[] = $coupon->get_code();
    }

    // For one coupon
    if( count($coupon_codes) == 1 ){
        $coupon_code = reset($coupon_codes);
        echo '<p>'.__( 'Used Coupon: ').$coupon_code.'<p>';
    }

    // For multiple coupons
    else {
        $coupon_codes = implode( ', ', $coupon_codes);
        echo '<p>'.__( 'Used Coupons: ').$coupon_codes.'<p>';
    }

    if (strpos($coupon_codes, 'groupurchase') !== false) {
        echo '<p>' . 'With this coupon you are participating in a group purchase' . '<p>' ;
    }

}

试试这个方法

// The email function hooked that display the text
function display_applied_coupons( $order, $sent_to_admin, $plain_text, $email ) {

    // Only for admins and when there at least 1 coupon in the order
    if ( ! $sent_to_admin && count($order->get_items('coupon') ) == 0 ) return;

    foreach( $order->get_items('coupon') as $coupon ){
        $coupon_codes[] = $coupon->get_code();
    }

    // For one coupon
    if( count( $coupon_codes ) == 1 ){
        $coupon_code = reset( $coupon_codes );

        // Set variable output
        $output = '<p>' . __( 'Coupon Used: ', 'woocommerce' ) . $coupon_code . '</p>';

        // Extra
        if ( strpos( $coupon_code, 'groupurchase') !== false ) {
            // Append to
            $output .= '<p>' . __('With this coupon you are participating in a group purchase', 'woocommerce') . '</p>';
        }

        // Echo output
        echo $output;
    } else { // For multiple coupons
        // Loop
        foreach ( $coupon_codes as $coupon_code ) {
            // Set variable output
            $output = '<p>' . __( 'Coupon Used: ', 'woocommerce' ) . $coupon_code . '</p>';

            // Extra
            if ( strpos( $coupon_code, 'groupurchase') !== false ) {
                // Append to
                $output .= '<p>' . __('With this coupon you are participating in a group purchase', 'woocommerce') . '</p>';
            }

            // Echo output
            echo $output;       
        }
    }
}
add_action( 'woocommerce_email_order_details', 'display_applied_coupons', 10, 4 );