douxian5076 2017-03-30 11:08
浏览 62
已采纳

在电子邮件通知上显示自定义结帐帐单字段

In WooCommerce, I want to add some custom fields on checkout which will be displayed under the billing section on the e-mail confirmation. My custom fields and their values, shown on the checkout form and on the order page on back end (WooCommerce -> Orders). So far everything works great.

The problem is that the e-mail that i receive does not contain the custom fields and their values.

Code shown below:

    add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
    function custom_override_checkout_fields( $fields ) {
        $fields['billing']['billing_field_testing'] = array(
            'label'     => __('TestingField', 'woocommerce'),
            'placeholder'   => _x('TestingField', 'placeholder', 'woocommerce'),
            'required'  => false,
            'class'     => array('form-row-wide'),
            'clear'     => true
             );
        return $fields;
    }

    add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
    function my_custom_checkout_field_display_admin_order_meta($order)
    {
        echo '<p><strong>'.__('TestingField').':</strong> ' . get_post_meta( $order->id, '_billing_field_testing', true ) . '</p>';
    }

Please help.

Thanks in advance

  • 写回答

2条回答 默认 最新

  • dpy3846 2017-03-30 11:38
    关注

    Updated: To display your custom checkout field in email notifications below the billing address, use this function hooked in woocommerce_email_customer_details action hook with a priority above 20:

    add_action('woocommerce_email_customer_details','add_custom_checkout_field_to_emails_notifications', 25, 4 );
    function add_custom_checkout_field_to_emails_notifications( $order, $sent_to_admin, $plain_text, $email ) {
    
        $output = '';
        $billing_field_testing = get_post_meta( $order->id, '_billing_field_testing', true );
    
        if ( !empty($billing_field_testing) )
            $output .= '<div><strong>' . __( "Some text:", "woocommerce" ) . '</strong> <span class="text">' . $billing_field_testing . '</span></div>';
    
        echo $output;
    }
    

    The Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    This code is tested and works.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题