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

问题描述 投票:0回答:1

我正在使用在businessbloomer.com 上找到的一些代码,“将自定义字段添加到产品变体”

这是代码:

add_action( 'woocommerce_variation_options_pricing', 'bbloomer_add_custom_field_to_variations', 10, 3 );
 
function bbloomer_add_custom_field_to_variations( $loop, $variation_data, $variation ) {
   woocommerce_wp_text_input( array(
'id' => 'custom_field[' . $loop . ']',
'class' => 'short',
'label' => __( 'Custom Field', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, 'custom_field', true )
   ) );
}
 
// -----------------------------------------
// 2. Save custom field on product variation save
 
add_action( 'woocommerce_save_product_variation', 'bbloomer_save_custom_field_variations', 10, 2 );
 
function bbloomer_save_custom_field_variations( $variation_id, $i ) {
   $custom_field = $_POST['custom_field'][$i];
   if ( isset( $custom_field ) ) update_post_meta( $variation_id, 'custom_field', esc_attr( $custom_field ) );
}
 
// -----------------------------------------
// 3. Store custom field value into variation data
 
add_filter( 'woocommerce_available_variation', 'bbloomer_add_custom_field_variation_data' );
 
function bbloomer_add_custom_field_variation_data( $variations ) {
   $variations['custom_field'] = '<div class="woocommerce_custom_field">Custom Field: <span>' . get_post_meta( $variations[ 'variation_id' ], 'custom_field', true ) . '</span></div>';
   return $variations;
}

如何将该自定义字段添加到所有下订单的电子邮件中? 所以它显示在订购的产品下方?

php wordpress woocommerce orders email-notifications
1个回答
0
投票

您只需使用以下命令即可在 WooCommerce 订单和电子邮件通知上显示自定义字段:

// Save "Item description" custom field value as order item metadata and display on orders / emails
add_action( 'woocommerce_checkout_create_order_line_item', 'save_custom_fields_as_order_item_metadata', 10, 4 );
function save_custom_fields_as_order_item_metadata( $item, $cart_item_key, $values, $order ) {
    if( $meta_value = $values['data']->get_meta('custom_field') ) {
        $item->add_meta_data( 'Custom field', $meta_value );
    }
}

应该可以。

© www.soinside.com 2019 - 2024. All rights reserved.