将自定义字段数据保存到 Woocommerce 产品变体的购物车和订单

Save Custom Field Data to Cart and Order of a Woocommerce product variation

我们设法在 Remi Corson guide here

之后为变体产品设置自定义字段

至此,当用户 select 变体时,我们可以在单个产品页面中显示自定义文本字段,但这在购买过程中还不够,因为我们需要:

A) 在购物车和结帐中显示此文本
B) 保存此信息,使其显示在感谢页面、电子邮件和管理员订单编辑页面中

类似,但具有产品变体而不是简单产品。

这是我们添加到 functions.php 以将自定义字段添加到产品变体的代码

// Add Variation Settings
add_action( 'woocommerce_product_after_variable_attributes', 'variation_settings_fields', 10, 3 );

// Save Variation Settings
add_action( 'woocommerce_save_product_variation', 'save_variation_settings_fields', 10, 2 );

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

    // Text Field
    woocommerce_wp_text_input( 
        array( 
            'id'          => '_text_field[' . $variation->ID . ']', 
            'label'       => __( 'My Text Field', 'woocommerce' ), 
            'placeholder' => 'http://',
            'desc_tip'    => 'true',
            'description' => __( 'Enter the custom value here.', 'woocommerce' ),
            'value'       => get_post_meta( $variation->ID, '_text_field', true )
        )
    );

        // Hidden field
    woocommerce_wp_hidden_input(
    array( 
        'id'    => '_hidden_field[' . $variation->ID . ']', 
        'value' => 'hidden_value'
        )
    );

}

/**
 * Save new fields for variations
 *
*/
function save_variation_settings_fields( $post_id ) {

    // Text Field
    $text_field = $_POST['_text_field'][ $post_id ];
    if( ! empty( $text_field ) ) {
        update_post_meta( $post_id, '_text_field', esc_attr( $text_field ) );
    }

    // Hidden field
    $hidden = $_POST['_hidden_field'][ $post_id ];
    if( ! empty( $hidden ) ) {
        update_post_meta( $post_id, '_hidden_field', esc_attr( $hidden ) );
    }

}

// Add New Variation Settings
add_filter( 'woocommerce_available_variation', 'load_variation_settings_fields' );

/**
 * Add custom fields for variations
 *
*/
function load_variation_settings_fields( $variations ) {
    
    // duplicate the line for each field
    $variations['text_field'] = get_post_meta( $variations[ 'variation_id' ], '_text_field', true );
    
    return $variations;

}

所以这里的目标是我们如何在商品下方的购物车和结帐中为每个变体显示此自定义字段(类似于下图 - 查看发货延迟通知)

并将每个变体的自定义字段信息保存到感谢页面、电子邮件和订单页面(我们使用此代码对简单产品进行了此操作,但这不适用于可变产品)

// Save and display "Custom Field for Simple Products" on order items everywhere
add_filter( 'woocommerce_checkout_create_order_line_item', 'action_wc_checkout_create_order_line_item', 10, 4 );
function action_wc_checkout_create_order_line_item( $item, $cart_item_key, $values, $order ) {

    // Get the Custom Field
    $value = $values['data']->get_meta( 'custom_field_for_simple_products' );

    if( ! empty( $value ) ) {
        // Save it and display it
        $item->update_meta_data( __( 'Custom Fields', 'woocommerce' ), $value );
    }
}

请帮忙!!

已更新

您的代码中存在一些错误...以下重新访问的代码将解决您的问题:

// Display Variation custom fields (admin)
add_action( 'woocommerce_product_after_variable_attributes', 'display_variation_setting_custom_fields', 10, 3 );
function display_variation_setting_custom_fields( $loop, $variation_data, $variation ) {
    echo '<div>';

    woocommerce_wp_text_input( array( // Text Field
        'id'            => "_text_field[$loop]",
        'label'         => __("My Text Field", "woocommerce"),
        'placeholder'   => "http://",
        'desc_tip'      => true,
        'description'   => __("Enter the custom value here.", "woocommerce"),
        'wrapper_class' => 'form-row form-row-full',
        'value'         => get_post_meta( $variation->ID, '_text_field', true ),
    ) );

    woocommerce_wp_hidden_input( array( // Hidden field
        'id'    => "_hidden_field[$loop]",
        'value' => 'hidden_value',
    ) );
    echo '</div>';
}


// Save Variation custom fields
add_action( 'woocommerce_save_product_variation', 'save_variation_custom_fields', 10, 2 );
function save_variation_custom_fields( $variation_id, $i ) {
    // Save Text Field
    if( isset( $_POST['_text_field'][$i] ) && ! empty( $_POST['_text_field'][$i] ) )
        update_post_meta( $variation_id, '_text_field', sanitize_text_field( $_POST['_text_field'][$i] ) );

    // Save Hidden Field
    if( isset( $_POST['_hidden_field'][$i] ) && ! empty( $_POST['_hidden_field'][$i] ) )
        update_post_meta( $variation_id, '_hidden_field', esc_attr( $_POST['_hidden_field'][$i] ) );
}

// Include our variation custom field
add_filter( 'woocommerce_available_variation', 'include_variation_custom_field', 10, 3) ;
function include_variation_custom_field( $data, $product, $variation ) {
    $data['text_field'] = $variation->get_meta( '_text_field' );

    return $data;
}

// Save and display "Custom Field for Simple Products" on order items everywhere
add_filter( 'woocommerce_checkout_create_order_line_item', 'action_wc_checkout_create_order_line_item_2', 10, 4 );
function action_wc_checkout_create_order_line_item_2( $item, $cart_item_key, $values, $order ) {
    // Get the Custom Field
    $value = $values['data']->get_meta( '_text_field' );

    if( ! empty( $value ) ) {
        // Save it and display it
        $item->update_meta_data( __( 'Custom Field', 'woocommerce' ), $value );
    }
}

代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。

相关:


索姆截图

在管理产品变体设置上:

在收到订单页面上(谢谢):

在管理员编辑订单页面上: