【问题标题】:Targeting specific email with the email id in Woocommerce在 Woocommerce 中使用电子邮件 ID 定位特定电子邮件
【发布时间】:2018-05-24 07:20:51
【问题描述】:

我在 Woocommerce 中设置了自定义状态和自定义电子邮件。我想使用当前电子邮件WC_Email,而不是当前状态作为电子邮件模板中的变量。

我需要在电子邮件模板中添加一些 if 语句。我没有使用订单状态来确保如果来自订单的电子邮件被手动重新发送,它不会使用单独的电子邮件发送当前订单状态的数据。

如何在 Woocommerce 中将 WC_Email 电子邮件 ID 作为变量回显?

【问题讨论】:

    标签: php wordpress woocommerce orders email-notifications


    【解决方案1】:

    WooCommerce 中不存在 wc_order_email 类或函数,因此我更新了您的问题。

    您正在查看的是$email 变量参数WC_Email 当前类型对象)。它主要在模板和钩子中到处定义。

    要获取可用的当前电子邮件 ID 作为变量,您只需使用 $email_id = $email->id...

    要获取自定义电子邮件的当前电子邮件 ID,您应该使用此代码(仅用于测试

    add_action( 'woocommerce_email_order_details', 'get_the_wc_email_id', 9, 4 );
    function get_the_wc_email_id( $order, $sent_to_admin, $plain_text, $email ) {
        // Will output the email id for the current notification
        echo '<pre>'; print_r($email->id); echo '</pre>'; 
    }
    

    代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

    您将获得以下信息:

    • new_order
    • customer_on_hold_order
    • customer_processing_order
    • customer_completed_order
    • customer_refunded_order
    • customer_partially_refunded_order
    • cancelled_order
    • failed_order
    • customer_reset_password
    • customer_invoice
    • customer_new_account
    • customer_note

    一旦您为自定义电子邮件通知获得正确的电子邮件 ID slug,您就可以在任何以下挂钩上使用它(而不是覆盖电子邮件模板)

    woocommerce_email_header (2 个参数:$email_heading$email
    woocommerce_email_order_details (4 个参数:$order$sent_to_admin$plain_text$email
    woocommerce_email_order_meta (4 个参数:$order$sent_to_admin$plain_text$email
    woocommerce_email_customer_details (4 个参数:$order$sent_to_admin$plain_text$email
    woocommerce_email_footer (1 个参数:$email

    这里是一个代码示例,我只针对“新订单”电子邮件通知:

    add_action( 'woocommerce_email_order_details', 'add_custom_text_to_new_order_email', 10, 4 );
    function add_custom_text_to_new_order_email( $order, $sent_to_admin, $plain_text, $email ) {
        // Only for "New Order"  email notifications (to be replaced by yours)
        if( ! ( 'new_order' == $email->id ) ) return;
    
        // Display a custom text (for example)
        echo '<p>'.__('My custom text').'</p>';
    }
    

    代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

    经过测试并且有效。

    【讨论】:

    • 谢谢!我能够得到这个的变体。
    • 在答案的第一部分添加代码以测试 ID,对我不起作用:Fatal error: Uncaught ArgumentCountError: Too few arguments to function add_action(), 1 passed in /home/*****/public_html/framework/wp-content/themes/sgone/woocommerce/emails/email-header.php on line 70 and at least 2 expected 事实上,无论我尝试使用哪个钩子,这都是原因和 $email 是一个未定义的变量。
    • 看来我现在修复了错误,并且您的代码确实有效。它只是不适用于 WooCommerce 订阅的电子邮件模板。这就是为什么。有没有办法让它也起作用?
    • @Skovsgaard 再次测试……第一个测试电子邮件 ID 的函数运行良好,没有任何错误。现在对于 WooCommerce 订阅,这是另一回事,因为它不使用相同的钩子函数参数......
    • 是的,我的错。我现在让它适用于 WooCommerce 订阅电子邮件。我在代码中犯了一个小错误。对不起。 :)
    猜你喜欢
    • 2018-01-07
    • 1970-01-01
    • 1970-01-01
    • 2018-07-04
    • 1970-01-01
    • 1970-01-01
    • 2016-11-06
    • 2015-01-16
    • 1970-01-01
    相关资源
    最近更新 更多