在 WooCommerce 中将自定义字段保存并显示为订单项元数据

Save and display a custom field as order item meta data in WooCommerce

在 woocommerce 中,我有 maisd wome 自定义,我可以在我的产品中添加自定义文本字段,在购物车和结帐中显示值 (参见下面的屏幕截图)

产品页面中的文本字段:

购物车中的价值:

结账时的价值:

但是我无法让它出现在购买详情或管理部分(见下面的截图)

没有值的结帐详细信息:

没有值的管理命令:

在我下面的代码中,有人能告诉我哪里做错了吗?

我在 functions.php 文件中使用的代码:

// Add the field to the product
add_action('woocommerce_before_add_to_cart_button', 'my_custom_checkout_field');

function my_custom_checkout_field() {
global $product;
$id = $product->get_id();


// Get the field name of InputText1
$InputText1Name = get_post_meta($id, 'InputText1', true);

 if ((!empty(get_post_meta($id, $InputText1, true)))){  
echo '<div id="InputText1">';
    echo '<label>'.__($InputText1Name).'</label> <input type="text" name="$InputText1V">';
    echo '</div>';
 }
}


// Store custom field
function save_my_custom_checkout_field( $cart_item_data, $product_id ) {


if( isset( $_REQUEST['$InputText1V'] ) ) {
    $cart_item_data[ '$InputText1V' ] = $_REQUEST['$InputText1V'];
    /* below statement make sure every add to cart action as unique line item */
    $cart_item_data['unique_key'] = md5( microtime().rand() );
}
return $cart_item_data;
}
add_action( 'woocommerce_add_cart_item_data', 'save_my_custom_checkout_field', 10, 2 );

// Render meta on cart and checkout
function render_meta_on_cart_and_checkout( $cart_data, $cart_item = null ){


// Get the product id inside the cart
foreach( WC()->cart->get_cart() as $cart_item ){
$product_id = $cart_item['product_id'];
} 


// Get the field name of InputText1
$InputText1Name = get_post_meta($product_id, 'InputText1', true);

$custom_items = array();
/* Woo 2.4.2 updates */
if( !empty( $cart_data ) ) {
    $custom_items = $cart_data;
}
if( isset( $cart_item['$InputText1V'] ) ) {
    $custom_items[] = array( "name" => $InputText1Name, "value" => $cart_item['$InputText1V'] );
}
return $custom_items;


}
add_filter( 'woocommerce_get_item_data', 'render_meta_on_cart_and_checkout', 10, 2 );


// Display as order meta
function my_field_order_meta_handler( $item_id, $values, $cart_item_key ) {
if( isset( $values['$InputText1V'] ) ) {
    wc_add_order_item_meta( $product_id, "$InputText1V", $values['$InputText1V'] );
}
}



// Update the order meta with field value

add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta');
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ($_POST['$InputText1V']) update_post_meta( $order_id, '$InputText1Name', esc_attr($_POST['$InputText1V']));
}

// Update the user meta with field value
add_action('woocommerce_checkout_update_user_meta', 'my_custom_checkout_field_update_user_meta');
function my_custom_checkout_field_update_user_meta( $user_id ) {
if ($user_id && $_POST['$InputText1V']) update_user_meta( $user_id, '$InputText1V', esc_attr($_POST['$InputText1V']) );
}

add_filter( 'woocommerce_hidden_order_itemmeta', 'hide_order_item_meta_fields' );


// Display field value on the order edit page

add_action( 'woocommerce_admin_order_data_after_billing_address',     'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta( $order ){


$order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
echo '<p><strong>'.__($InputText1V).':</strong> ' . get_post_meta( $order_id, $InputText1V, true ) . '</p>';
}


function hide_order_item_meta_fields( $fields ) {
$fields[] = 'current_view';
$fields[] = 'custom_image';//Add all meta keys to this array,so that it will not be displayed in order meta box
return $fields;
}

add_action( 'woocommerce_after_order_itemmeta', 'order_meta_customized_display',10, 3 );

function order_meta_customized_display( $item_id, $item, $product ){


$order_product_id = $item['product_id'];
$field1name = get_post_meta($order_product_id, 'InputText1', true);

echo'<br>';
print_r($InputText1V);
echo'<br>';


echo  $field1name;
echo ': ';

}

您的代码中存在错误和缺失。您也不需要所有这些功能,您应该 真正避免在自定义字段中使用 $(如果可以 capitalsslugs(或在 key slugs 中,甚至在函数名称中)。

我已经测试并重新访问了您的代码。这是您需要的,可以删除其他所有内容:

// Add the field to the product
add_action('woocommerce_before_add_to_cart_button', 'my_custom_checkout_field');
function my_custom_checkout_field() {
    global $product;

    $product_id = $product->get_id();

    // Get the field name of InputText1
    $label = get_post_meta($product_id, 'InputText1', true);

    if( ! empty( $label ) ){
        echo '<div id="InputText1">
            <label>'.$label.':</label> <input type="text" name="custom_slug" value="">
        </div>';
    }
}

// Store custom field label and value in cart item data
add_filter( 'woocommerce_add_cart_item_data', 'save_my_custom_checkout_field', 10, 2 );
function save_my_custom_checkout_field( $cart_item_data, $product_id ) {
    if( isset( $_REQUEST['custom_slug'] ) ) {
        $cart_item_data['custom_data']['label'] = get_post_meta($product_id, 'InputText1', true);
        $cart_item_data['custom_data']['value'] = sanitize_text_field( $_REQUEST['custom_slug'] );
        $cart_item_data['custom_data']['ukey'] = md5( microtime().rand() );
    }
    return $cart_item_data;
}

// Display items custom fields label and value in cart and checkout pages
add_filter( 'woocommerce_get_item_data', 'render_meta_on_cart_and_checkout', 10, 2 );
function render_meta_on_cart_and_checkout( $cart_data, $cart_item ){

    $custom_items = array();
    /* Woo 2.4.2 updates */
    if( !empty( $cart_data ) ) {
        $custom_items = $cart_data;
    }
    if( isset( $cart_item['custom_data'] ) ) {
        $custom_items[] = array(
            'name' => $cart_item['custom_data']['label'],
            'value' => $cart_item['custom_data']['value'],
        );
    }
    return $custom_items;
}


// Save item custom fields label and value as order item meta data
add_action('woocommerce_add_order_item_meta','save_in_order_item_meta', 10, 3 );
function save_in_order_item_meta( $item_id, $values, $cart_item_key ) {
    if( isset( $values['custom_data'] ) ) {
        wc_add_order_item_meta( $item_id, $values['custom_data']['label'], $values['custom_data']['value'] );
    }
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。

已测试并有效。

This way you will get the display in Order received, Order view, Edit order (admin) and email notifications…