【问题标题】:WooCommerce customized email only for processing and completed ordersWooCommerce 定制电子邮件仅用于处理和完成的订单
【发布时间】:2021-06-23 15:28:26
【问题描述】:

我正在使用插件在电子邮件订单中发送自定义推荐代码,但现在我发现每个订单电子邮件都插入了代码。

function gens_raf_customer_email( $order, $sent_to_admin, $plain_text ) {
$user_id = ( version_compare( WC_VERSION, '2.7', '<' ) ) ? $order->customer_user : $order->get_customer_id();
if( ! empty( $user_id ) && ( get_user_meta($user_id, "gens_referral_id", true) ) != '' ){
    $code = get_user_meta($user_id, "gens_referral_id", true);
} else {
    $code = ( version_compare( WC_VERSION, '2.7', '<' ) ) ? $order->billing_email : $order->get_billing_email();
}

if( $plain_text ){
    _e('Your referral code is: ','gens-raf') . $code;
} else {
    echo '<p style="text-align:center;margin-top:10px;">Your referral code is: ' .get_home_url() .'?raf='. $code . '</p>';
}  

} add_action('woocommerce_email_customer_details', 'gens_raf_customer_email', 30, 3);

我在胡闹

if ( $email->id == 'customer_completed_order' )

但是 php 代码生成器给了我错误。主要问题是仅将该电子邮件发送给已完成和/或正在处理的订单,而不是退款、取消等。

谢谢!

【问题讨论】:

    标签: php wordpress woocommerce orders email-notifications


    【解决方案1】:

    您收到错误是因为您没有将第四个 $email 参数添加到 woocommerce_email_customer_details 挂钩。

    内容只会显示在已完成的订单中,并且 处理订单模板。

    试试这个:

    add_action('woocommerce_email_customer_details', 'gens_raf_customer_email', 30, 4 );
    function gens_raf_customer_email( $order, $sent_to_admin, $plain_text, $email ) {
    
        if ( $email->id == 'customer_completed_order' || $email->id == 'customer_processing_order' ) {
            $user_id = ( version_compare( WC_VERSION, '2.7', '<' ) ) ? $order->customer_user : $order->get_customer_id();
            if( ! empty( $user_id ) && ( get_user_meta($user_id, "gens_referral_id", true) ) != '' ){
                $code = get_user_meta($user_id, "gens_referral_id", true);
            } else {
                $code = ( version_compare( WC_VERSION, '2.7', '<' ) ) ? $order->billing_email : $order->get_billing_email();
            }
        
            if( $plain_text ){
                _e('Your referral code is: ','gens-raf') . $code;
            } else {
                echo '<p style="text-align:center;margin-top:10px;">Your referral code is: ' .get_home_url() .'?raf='. $code . '</p>';
            }  
        }
        
    }
    

    代码已经过测试并且可以运行。

    【讨论】:

    • Vincenzo 确实有效.. 非常感谢!
    【解决方案2】:

    变量$email 是挂钩函数中缺少的第四个参数,您需要针对特定​​的电子邮件通知。

    改用 (对于 woocommerce 3 及更高版本)

    add_action('woocommerce_email_customer_details', 'gens_raf_customer_email', 100, 4 );
    function gens_raf_customer_email( $order, $sent_to_admin, $plain_text, $email ) {
        // Target specific notifications: Customer processing and completed orders
        if ( in_array( $email->id, ['customer_processing_order', 'customer_completed_order'] ) ) {
            $gens_raf = get_user_meta( $order->get_user_id(), "gens_referral_id", true );
            $code     = empty( $gens_raf ) ? $order->get_billing_email() : $gens_raf;
    
            if( $plain_text ){
                printf( __('Your referral code is: %s', 'gens-raf'), $code );
            } else {
                $output = sprintf( __("Your referral code is: %s", "gens-raf"), get_home_url() .'?raf='. $code );
                echo '<p style="text-align:center;margin-top:10px;">' . $output . '</p>';
            }  
        }  
    }
    

    代码位于活动子主题(或活动主题)的functions.php 文件中。它应该可以工作。

    【讨论】:

      猜你喜欢
      • 2017-05-14
      • 2018-05-31
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-03
      相关资源
      最近更新 更多