在管理员的 WooCommerce 电子邮件通知中显示自定义字段

我在我的 WordPress 网站上安装了 Checkout Field Editor。我创建了自定义字段。但是,使用以下代码,pcustomer 字段同时出现在我和客户收到的“新订单”电子邮件中。相反,我希望它只属于我。我尝试编辑代码,但仍然不起作用。


add_filter( 'woocommerce_email_order_meta_fields', 'custom_woocommerce_email_order_meta_fields', 10, 3 );

function custom_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {

    $fields['meta_key'] = array(

        'label' => __( 'Label' ),

        'value' => get_post_meta( $order->id, 'meta_key', true ),

    );

    return $fields;

}


呼唤远方
浏览 78回答 1
1回答

炎炎设计

从 WooCommerce 3 开始,您的代码有点过时,有一些错误尝试以下操作:add_filter( 'woocommerce_email_order_meta_fields', 'custom_woocommerce_email_order_meta_fields', 10, 3 );function custom_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {&nbsp; &nbsp; $meta_key = '_meta_key1'; // <= Here define the correct meta key&nbsp; &nbsp; $meta_value = $order->get_meta( $meta_key1 );&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; if ( ! empty( $meta_value1 ) ) {&nbsp; &nbsp; &nbsp; &nbsp; $fields[ $meta_key1 ] = array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'label' => __( 'My label 1', "text-domain' ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'value' => $meta_value1,&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; }&nbsp; &nbsp; return $fields;}代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并有效。现在要将代码限制为仅用于管理电子邮件通知,您将使用$sent_to_admin参数变量,如下所示:add_filter( 'woocommerce_email_order_meta_fields', 'custom_woocommerce_email_order_meta_fields', 10, 3 );function custom_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {&nbsp; &nbsp; if ( $sent_to_admin ) {&nbsp; &nbsp; &nbsp; &nbsp; $meta_key1 = '_meta_key1'; // <= Here define the correct meta key&nbsp; &nbsp; &nbsp; &nbsp; $meta_value1 = $order->get_meta( $meta_key1 );&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if ( ! empty( $meta_value1 ) ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $fields[ $meta_key1 ] = array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'label' => __( 'My label 1', "text-domain' ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'value' => $meta_value1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return $fields;}代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并有效。这次,此自定义字段将不会显示在客户电子邮件通知中。注意:$order->id自 WooCommerce 3. 起不正确,请使用$order->get_id()。WordPressget_post_meta()功能可以用 WooCommerce 方法替代WC_Data&nbsp;get_meta()。
打开App,查看更多内容
随时随地看视频慕课网APP