简体   繁体   English

在Woocommerce管理员电子邮件通知中显示产品自定义字段值

[英]Display a product custom field value in Woocommerce admin email notifications

I have this code in my function.php, how can i dispaly this field value in admin mail order? 我的function.php中有此代码,如何在管理员邮件订单中显示此字段值? Thanks! 谢谢!

//1.1 Display field in admin
add_action('woocommerce_product_options_inventory_product_data', 'woocommerce_product_custom_fields');
function woocommerce_product_custom_fields(){
    global $woocommerce, $post;
    echo '<div class="product_custom_field">';
    woocommerce_wp_text_input(
        array(
            'id' => '_custom_product_text_field',
            'placeholder' => 'Name',
            'label' => __('customtext', 'woocommerce'),
            'desc_tip' => 'true'
        )
    );    
    echo '</div>';
}

//1.2 Save field
add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');
function woocommerce_product_custom_fields_save($post_id){
    $woocommerce_custom_product_text_field = $_POST['_custom_product_text_field'];
    if (!empty($woocommerce_custom_product_text_field))
        update_post_meta($post_id, '_custom_product_text_field', esc_attr($woocommerce_custom_product_text_field));

}

Update 2 There is two very similar alternatives (the first one is better): 更新2有两种非常相似的替代方法(第一种更好):

To display easily this product custom field values in emails (and in orders too) you can use the following code, that will save this data in order items, once the order is placed. 要在电子邮件(以及订单)中轻松显示此产品自定义字段值,您可以使用以下代码,一旦下订单,该代码会将这些数据保存在订单项中。

First alternative code (the best new one that use WC 3+ recent CRUD methods) : 第一个备用代码(使用WC 3+最新CRUD方法的最佳新代码

// 1.3 Save custom field value in order items meta data (only for Woocommerce versions 3+)
add_action( 'woocommerce_checkout_create_order_line_item', 'add_custom_field_to_order_item_meta', 20, 4 );
function add_custom_field_to_order_item_meta( $item, $cart_item_key, $values, $order ) {
    $custom_field_value = get_post_meta( $item->get_product_id(), '_custom_product_text_field', true );
    if ( ! empty($custom_field_value) ){
            $item->update_meta_data( __('Custom label', 'woocommerce'), $custom_field_value );
    }
}

… or … … 要么 …

The other alternative (the old one updated) : 另一种选择(旧的更新)

// 1.3 Save custom field value in order items meta data
add_action( 'woocommerce_add_order_item_meta', 'add_custom_field_to_order_item_meta', 20, 3 );
function add_custom_field_to_order_item_meta( $item_id, $values, $cart_item_key ) {

    $custom_field_value = get_post_meta( $values['data']->get_id(), '_custom_product_text_field', true );
    if ( ! empty($custom_field_value) )
        wc_add_order_item_meta( $item_id, __('Custom label', 'woocommerce'), $custom_field_value );
}

Code goes in function.php file of your active child theme (or theme). 代码进入您的活动子主题(或主题)的function.php文件中。 Tested and works. 经过测试和工作。

On orders you will get that (for both): 在订单上,您将获得(两者):

在此处输入图片说明

On emails you will get that (for both): 在电子邮件中,您会得到的(两者):

在此处输入图片说明

This is my code now 这是我的代码

//1.1 Display custom field 
add_action('woocommerce_product_options_inventory_product_data', 'woocommerce_product_custom_fields');
add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');
function woocommerce_product_custom_fields(){
    global $woocommerce, $post;
    echo '<div class="product_custom_field">';
    woocommerce_wp_text_input(
        array(
            'id' => '_custom_product_text_field',
            'placeholder' => 'text',
            'label' => __('Custom', 'woocommerce'),
            'desc_tip' => 'true'
        )
    );    
    echo '</div>';
}

// 1.2 Save this field in admin
function woocommerce_product_custom_fields_save($post_id){
    $woocommerce_custom_product_text_field = $_POST['_custom_product_text_field'];
    if (!empty($woocommerce_custom_product_text_field))
        update_post_meta($post_id, '_custom_product_text_field', esc_attr($woocommerce_custom_product_text_field));

}

// 1.3 Save custom field value in order items meta data
add_action( 'woocommerce_add_order_item_meta', 'add_custom_field_to_order_item_meta', 20, 3 );
function add_custom_field_to_order_item_meta( $item_id, $values, $cart_item_key ) {

    $custom_field_value = get_post_meta( $values['data']->get_id(), '_custom_product_text_field', true );
    if ( ! empty($$custom_field_value) )
        wc_add_order_item_meta( $item_id, __('Custom label', 'woocommerce'), $custom_field_value );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 WooCommerce email 通知中为管理员显示自定义字段 - Display a custom field in WooCommerce email notifications for admin 在 WooCommerce email 通知中显示产品自定义字段 - Display product custom fields in WooCommerce email notifications 在 WooCommerce email 通知上显示自定义产品图像 - Display custom product image on WooCommerce email notifications 在 Woocommerce 电子邮件通知上显示自定义元字段值 - Display custom meta field value on Woocommerce email notifications 在 WooCommerce 管理订单页面中保留并显示产品自定义字段值 - Keep and display product custom field value in WooCommerce Admin Order pages 在Woocommerce中查找并​​显示产品自定义字段值 - Find and display a product custom field value in Woocommerce 如何在 WooCommerce 中显示产品自定义字段值? - How display a product custom field value in WooCommerce? 在自定义管理订单电子邮件中显示 Woocommerce 产品图片 - Display Woocommerce Product Image in custom admin order email 在 WooCommerce 订单管理页面 (ACF) 显示产品自定义字段 - Display product custom field in WooCommerce Order admin Page (ACF) 仅在 WooCommerce 管理单个订单中显示产品自定义字段 - Display a product custom field only in WooCommerce Admin single orders
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM