【问题标题】:Display used coupons + specific message on WooCommerce order Emails在 WooCommerce 订单电子邮件上显示使用过的优惠券 + 特定消息
【发布时间】:2020-09-18 17:17:24
【问题描述】:

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

显示优惠券的工作基于

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


我试图通过检查字符串$coupon_codes 是否包含特定单词/特定优惠券(在本例中为“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>' ;
    }

}

【问题讨论】:

    标签: php wordpress woocommerce email-notifications coupon


    【解决方案1】:

    试试这个方法

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

    【讨论】:

    • 这适用于 7uc1f3r!非常感谢。我试图研究主要差异,看看我是否能理解为什么 strpos 以前不能工作,是不是主要的区别是让它工作的事实是你将变量设置为输出而不是直接“回显”就像我在做的那样?还是有更多的更正?另外,在这种情况下,“woocommerce”会做什么?再次非常感谢您的所有帮助和建议,非常感谢!!!
    • 因为在您的代码中str_pos 用于一张优惠券以及多张优惠券。应用 1 张优惠券时,结果是 array 而不是 string。见this example
    • 当然!这完全有道理!非常感谢您的直截了当的解释,我真的很感激它
    猜你喜欢
    • 2020-07-28
    • 2016-12-22
    • 2020-11-24
    • 2021-04-07
    • 2019-07-21
    • 2023-02-01
    • 2021-04-15
    • 2019-06-06
    • 1970-01-01
    相关资源
    最近更新 更多