在Woocommerce订单和电子邮件中显示产品变体的自定义字段

时间:2018-03-20 11:33:05

标签: php woocommerce orders variations email-notifications

我在WooCommerce的后端成功添加了产品变体的自定义字段,并且能够显示其价值。我希望在订单和电子邮件中包含此值。

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

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

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

此外,我现在以低于产品价格/购物车/结帐模板中每件商品总价的价格显示以下

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

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

1 个答案:

答案 0 :(得分:1)

已更新:要在订单页面和电子邮件通知中显示,请尝试以下附加功能(我已在woocommerce_get_item_data挂钩功能中进行了一些更改,因此您需要更换它与以下一个)

// 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文件。经过测试和工作。

  

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