【问题标题】:Display custom content based on shipping method in Woocommerce email notification在 Woocommerce 电子邮件通知中根据送货方式显示自定义内容
【发布时间】:2017-11-13 18:06:32
【问题描述】:

Woocommerce 更新到 3.2 后,下面的代码不再起作用。

add_action( 'woocommerce_email_order_details', 'my_completed_order_email_instructions', 10, 4 );
function my_completed_order_email_instructions( $order, $sent_to_admin, $plain_text, $email ) {

    // Only for "Customer Completed Order" email notification
    if( 'customer_completed_order' != $email->id ) return;

    // Comptibility With WC 3.0+
    if ( method_exists( $order, 'get_id' ) ) {
        $order_id = $order->get_id();
    } else {
        $order_id = $order->id;
    }
    //$order->has_shipping_method('')
    $shipping_method_arr = get_post_meta($order_id, '_shipping_method', false); // an array
    $rate_id = $shipping_method_arr[0][0]; // the rate ID

    if ( 'flat_rate:10' == $rate_id ){
        echo pll__("Text 1");
    } else {
        echo pll__("Text 2");
    }
}

这段代码有什么错误或过时的地方?
需要进行哪些更改才能使其再次运行?

【问题讨论】:

    标签: php wordpress woocommerce orders email-notifications


    【解决方案1】:

    这是获取订单中使用的运输方法并使此功能按预期工作的正确方法:

    add_action( 'woocommerce_email_order_details', 'my_completed_order_email_instructions', 10, 4 );
    function my_completed_order_email_instructions( $order, $sent_to_admin, $plain_text, $email ) {
    
        // Only for "Customer Completed Order" email notification
        if( 'customer_completed_order' != $email->id ) return;
    
        $found = false; // Initializing variable
    
        // Iterating through Order shipping methods
        foreach($order->get_shipping_methods() as $value){
            $rate_id = $value->get_method_id(); // Get the shipping rate ID
            if ( 'flat_rate:10' == $rate_id )
                $found = true;
        }
    
        if ($found)
            echo '<p>'.__("Text 1 (found)","woocommerce").'</p>';
        else
            echo '<p>'.__("Text 2 (else)","woocommerce").'</p>';
    }
    

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

    经过测试并且可以工作……

    【讨论】:

    • 你好。似乎代码在 woocommerce 3.4 版之后停止工作。任何想法如何修复代码?
    • @Ugenijus 好的,我需要一些时间来检查问题所在并进行更新。我会尽快通知您。
    • @Ugenijus 似乎 Woocommerce 3.4 中存在与woocommerce_email_order_details 动作挂钩相关的错误。如果您尝试显示字符串之类的内容(没有任何限制)……什么都不会显示。我尝试使用其他电子邮件挂钩,但没有一个有效。 所以这是 Woocommerce 团队将在下一个版本中解决的错误。
    • 你认为他们有关于这个错误的信息吗?
    • @Ugenijus 我不知道,你可以尝试在 Woocommerce Github 上报告它...... 但这是一些黄金规则: 1. woocommerce 版本 3.4 是一个新的主要版本对于生产来说太年轻了,仍然有一些错误(这将在未来的小更新中解决)。 2.所有插件和主题尚未完全兼容。 3. 总是在舞台网站上测试更新。 4. 在应用 Woocommerce 主要版本更新之前始终等待一两个月。
    猜你喜欢
    • 2021-05-05
    • 2019-07-18
    • 2019-08-23
    • 2018-02-10
    • 1970-01-01
    • 2018-02-15
    • 2017-12-06
    • 2020-12-07
    • 2020-11-05
    相关资源
    最近更新 更多