Add custom field in products to display in cart, checkout and in orders

心不动则不痛 提交于 2019-12-06 16:03:52

Update 2

Try the following (that normally adds a dropdown in single product pages and save/display the selected value in cart item):

// Frontend: custom select field (dropdown) in product single pages
add_action( 'woocommerce_before_add_to_cart_button', 'fabric_length_product_field' );
function fabric_length_product_field() {
    global $product;

    // Select field
    woocommerce_form_field('fitting_color', array(
        'type'     => 'select',
        'class'    => array('my-field-class form-row-wide'),
        'label'    => __('_fitting_color', 'woocommerce'),
        'required' => true, // or false
        'options'  => array(
            ''    => __('Select a color', 'woocommerce'),
            'black'    => __('Black', 'woocommerce'),
            'white'    => __('White', 'woocommerce'),
        ),
    ),'');
}

// Add "fitting_color" selected value 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 ){
    if( isset($_POST['fitting_color']) && ! empty($_POST['fitting_color'])) {
        $cart_item_data['fcolor']= array(
            'value' => esc_attr($_POST['fitting_color']),
            'unique_key' => md5( microtime() . rand() ), // <= Make each cart item unique
        );
    }
    return $cart_item_data;
}


// Display custom cart item data in cart and checkout pages
add_filter( 'woocommerce_get_item_data', 'display_custom_cart_item_data', 10, 2 );
function display_custom_cart_item_data( $cart_item_data, $cart_item ) {
    if ( isset( $cart_item['fcolor']['value'] ) ){
        $cart_item_data[] = array(
            'name' => __( 'Fitting color', 'woocommerce' ),
            'value' => $cart_item['fcolor']['value'],
        );
    }
    return $cart_item_data;
}

Code goes on function.php file of your active child theme (or active theme). It should works.


For save/display on order (and display on email notifications), you will use:

// Save chosen slelect field value to each order item as custom meta data and display it everywhere
add_action('woocommerce_checkout_create_order_line_item', 'save_order_item_product_fitting_color', 10, 4 );
function save_order_item_product_fitting_color( $item, $cart_item_key, $values, $order ) {
    if( isset($values['fcolor']['value']) ) {
        $key = __('Fitting color', 'woocommerce');
        $value = $values['fcolor']['value'];
        $item->update_meta_data( $key, $value );
    }
}

And the missing field validation:

// Field validation
add_filter( 'woocommerce_add_to_cart_validation',  'dropdown_fitting_color_validation', 10, 3 );
function dropdown_fitting_color_validation( $passed, $product_id, $quantity ) {
    if( isset($_POST['fitting_color']) && empty($_POST['fitting_color']) ) {
        wc_add_notice( __( "Please select a Fitting color", "woocommerce" ), 'error' );
        return false;
    }

    return $passed;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!