代码之家  ›  专栏  ›  技术社区  ›  Ugenijus

在Woocommerce电子邮件通知中根据发货方式显示自定义内容

  •  3
  • Ugenijus  · 技术社区  · 8 年前

    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");
        }
    }
    

    此代码中有哪些错误或过时?
    需要做什么改变才能使其再次发挥作用?

    1 回复  |  直到 8 年前
        1
  •  3
  •   LoicTheAztec    8 年前

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

    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>';
    }
    

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