我在特定产品中有两个自定义字段。我想在购物车和结账页面中的每个变体名称后面添加每个自定义字段。
add_action('woocommerce_before_add_to_cart_button', 'action_before_add_to_cart_button', 20);
function action_before_add_to_cart_button() {
global $post, $product;
if (is_product() && $post->ID == 19652) {
?>
<div class="custom-fields">
<div>
<input class="form-control alt" name="first_custom_name" type="text"
placeholder="<?php esc_html_e('Name for First bottle'); ?>" required>
</div>
<div>
<input class="form-control alt" type="text" name="sec_custom_name"
placeholder="<?php esc_html_e('Name for Second bottle'); ?>">
</div>
</div>
<?php
}
}
这是产品页面的自定义字段,但我不知道如何将数据从自定义字段提取到购物车订单行。我见过有地方更新meta函数,但是由于数据是客户输入的,所以数据是在meta中更新的。如果选择不同的变体怎么办?有相同的数据吗?我想为不同的变化添加不同的数据。这可能吗?
以下代码将保存您的自定义字段,并将其显示在购物车、结账、订单和电子邮件通知中。
// Utility function (your custom field settings)
function get_custom_fields_data() {
return array(
'first_custom_name' => esc_html__('Name for First bottle'),
'sec_custom_name' => esc_html__('Name for Second bottle'),
);
}
// Displaying custom input fields in single product
add_action('woocommerce_before_add_to_cart_button', 'action_before_add_to_cart_button', 20);
function action_before_add_to_cart_button() {
global $post, $product;
if ( is_product() && $post->ID == 19652 ) {
echo '<div class="custom-fields">';
foreach ( get_custom_fields_data() as $field_key => $field_label ) {
printf('<div><input class="form-control alt" name="%s" type="text" placeholder="%s" required>
</div>', $field_key, $field_label );
}
echo '</div>';
}
}
// Save custom fields as custom cart item data
add_filter('woocommerce_add_cart_item_data', 'add_custom_cart_item_data', 20, 2 );
function add_custom_cart_item_data( $cart_item_data, $product_id ) {
foreach ( get_custom_fields_data() as $field_key => $field_label ) {
if ( isset($_POST[$field_key]) && ! empty($_POST[$field_key]) ) {
$cart_item_data[$field_key] = sanitize_text_field($_POST[$field_key]);
}
}
return $cart_item_data;
}
// Display custom fields in Cart and Checkout
add_filter( 'woocommerce_get_item_data', 'display_custom_cart_item_data', 20, 2 );
function display_custom_cart_item_data( $cart_data, $cart_item ) {
foreach ( get_custom_fields_data() as $field_key => $field_label ) {
if( isset($cart_item[$field_key]) ) {
$cart_data[] = array(
'key' => $field_label,
'value' => $cart_item[$field_key],
);
}
}
return $cart_data;
}
// Save and display custom fields (custom order item metadata)
add_action( 'woocommerce_checkout_create_order_line_item', 'save_order_item_custom_meta_data', 10, 4 );
function save_order_item_custom_meta_data( $item, $cart_item_key, $values, $order ) {
foreach ( get_custom_fields_data() as $field_key => $field_label ) {
if( isset($values[$field_key]) ) {
$item->update_meta_data($field_key, $values[$field_key]);
}
}
}
// Add readable "meta key" label name replacement
add_filter('woocommerce_order_item_display_meta_key', 'filter_wc_order_item_display_meta_key', 10, 3 );
function filter_wc_order_item_display_meta_key( $display_key, $meta, $item ) {
if( $item->get_type() === 'line_item' ) {
foreach ( get_custom_fields_data() as $field_key => $field_label ) {
if( $meta->key === $field_key ) {
$display_key = $field_label;
}
}
}
return $display_key;
}
代码位于子主题的functions.php 文件中(或插件中)。已测试并工作。