在Woocommerce订单和电子邮件中显示产品变体的自定义字段 [英] Displaying custom field of product variations within Woocommerce orders and emails

查看:156
本文介绍了在Woocommerce订单和电子邮件中显示产品变体的自定义字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我成功在WooCommerce后端添加了一个用于产品变化的自定义字段,并且能够显示其价值.我也想在订单和电子邮件中包含此值.

I successfully added a custom field for product variations within the backend of WooCommerce and was able to display its value. I'd like to contain this value within order and emails as well.

//Display Fields in admin on product edit screen
add_action( 'woocommerce_product_after_variable_attributes', 'woo_variable_fields', 10, 3 );

//Save variation fields values
add_action( 'woocommerce_save_product_variation', 'save_variation_fields', 10, 2 );

// Create new fields for variations
function woo_variable_fields( $loop, $variation_data, $variation ) {

echo '<div class="variation-custom-fields">';

  // Text Field
  woocommerce_wp_text_input( 
    array( 
      'id'          => '_text_field['. $loop .']', 
      'label'       => __( 'additional fees (e.g. monthly fee)', 'woocommerce' ), 
      'placeholder' => '',
      //'desc_tip'    => true,
      'wrapper_class' => 'form-row form-row-first',
      //'description' => __( 'Enter the custom value here.', 'woocommerce' ),
      'value'       => get_post_meta($variation->ID, '_text_field', true)
    )
  );

echo "</div>"; 

}

/** Save new fields for variations */
function save_variation_fields( $variation_id, $i) {

// Text Field
$text_field = stripslashes( $_POST['_text_field'][$i] );
update_post_meta( $variation_id, '_text_field', esc_attr( $text_field ) );
}

// Custom Product Variation
add_filter( 'woocommerce_available_variation', 'custom_load_variation_settings_products_fields' );

function custom_load_variation_settings_products_fields( $variations )    { 
$variations['variation_custom_field'] = get_post_meta( $variations[ 'variation_id' ], '_text_field', true );  
return $variations;
}

我正在使用以下内容在购物车中显示自定义字段的值.

I'm using the following to display the custom field's value within the cart.

add_filter( 'woocommerce_get_item_data', 'display_custom_field_as_item_data', 20, 2 );
function display_custom_field_as_item_data( $cart_data, $cart_item ) {
    if( $value = get_post_meta( $cart_item['data']->get_id(), '_text_field', true ) ){
        $cart_data[] = array(
            'name' => __( 'Additional Monthly Fee', 'woocommerce' ),
            'value' => sanitize_text_field( $value )
        );
    }
    return $cart_data;
}

目前,我在购物车/结帐模板中的产品价格/每件商品的总和以下显示其值,并显示以下内容

Further as for now I'm displaying its value below the product price / total of each item within cart/checkout template with the following

echo get_post_meta( $_product->get_id(), '_text_field', true );

如何在订单和电子邮件中显示此字段的值?

How about displaying the value of this field within orders and emails?

推荐答案

已更新:要显示订单页面和电子邮件通知,请尝试以下附加功能( woocommerce_get_item_data挂钩函数,因此您需要将其替换为以下代码):

Updated: To display that in order pages and email notifications try the following additional functions (I have maid some changes in the woocommerce_get_item_data hooked function, so you need to replace it with the following one):

// Save custom field value in cart item
add_filter( 'woocommerce_add_cart_item_data', 'save_custom_field_in_cart_object', 30, 3 );
function save_custom_field_in_cart_object( $cart_item_data, $product_id, $variation_id ) {

    // Get the correct Id to be used
    $the_id = $variation_id > 0 ? $variation_id : $product_id;

    if( $value = get_post_meta( $the_id, '_text_field', true ) )
        $cart_item_data['custom_data'] = sanitize_text_field( $value );

    return $cart_item_data;
}

// Display on cart and checkout pages
add_filter( 'woocommerce_get_item_data', 'display_custom_field_as_item_data', 20, 2 );
function display_custom_field_as_item_data( $cart_data, $cart_item ) {
    if( isset( $cart_item['custom_data'] ) ){
        $cart_data[] = array(
            'name' => __( 'Additional Monthly Fee', 'woocommerce' ),
            'value' => $cart_item['custom_data']
        );
    }
    return $cart_data;
}

// 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 ) {

    if( isset( $values['custom_data'] ) )
        wc_add_order_item_meta( $item_id, __( 'Additional Monthly Fee', 'woocommerce' ), $values['custom_data'] );
}

代码进入您的活动子主题(或主题)的function.php文件中.经过测试并可以正常工作.

它将在所有订单"页面和电子邮件通知中显示自定义字段标签和值.

It will display the custom field label and value in all Order pages and in email notifications too.

这篇关于在Woocommerce订单和电子邮件中显示产品变体的自定义字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆