Skip to content
Advertisement

Pass custom product text field as cart item data in WooCommerce

The goal here is to have a text field on the product page.

The customer fills it in and adds to cart. The text is added to the product and included in the cart and on the checkout and on the order.

The field works and the validation works fine, but the text is not included / transferred to the cart, checkout and order.

Here’s the code:

add_action( 'woocommerce_before_add_to_cart_button', 'product_add_on', 9 );
function product_add_on() {
    $value = isset( $_POST['_custom_text_add_on'] ) ? sanitize_text_field( $_POST['_custom_text_add_on'] ) : '';
    echo '<div><label>Custom Text Add-On <abbr class="required" title="required">*</abbr></label><p><input name="_custom_text_add_on" value="' . $value . '"></p></div>';
}

add_filter( 'woocommerce_add_to_cart_validation', 'product_add_on_validation', 10, 3 ); 
function product_add_on_validation( $passed, $product_id, $qty ){
    if( isset( $_POST['_custom_text_add_on'] ) && sanitize_text_field( $_POST['_custom_text_add_on'] ) == '' ) {
        wc_add_notice( 'Custom Text Add-On is a required field', 'error' );
        $passed = false;
    }
    return $passed;
}

add_filter( 'woocommerce_add_cart_item_data', 'product_add_on_cart_item_data', 10, 2 ); 
function product_add_on_cart_item_data( $cart_item, $product_id ){
    if( isset( $_POST['_custom_text_add_on'] ) ) {
        $cart_item['custom_text_add_on'] = sanitize_text_field( $_POST['custom_text_add_on'] );
    }
    return $cart_item;
}

add_filter( 'woocommerce_get_item_data', 'product_add_on_display_cart', 10, 2 ); 
function product_add_on_display_cart( $_data, $cart_item ) {
    if ( isset( $cart_item['custom_text_add_on'] ) ){
        $data[] = array(
            'name' => 'Custom Text Add-On',
            'value' => sanitize_text_field( $cart_item['custom_text_add_on'] )
        );
    }
    return $data;
}

add_action( 'woocommerce_add_order_item_meta', 'product_add_on_order_item_meta', 10, 2 ); 
function product_add_on_order_item_meta( $item_id, $values ) {
    if ( ! empty( $values['custom_text_add_on'] ) ) {
        wc_add_order_item_meta( $item_id, 'Custom Text Add-On', $values['custom_text_add_on'], true );
    }
}

add_filter( 'woocommerce_order_item_product', 'product_add_on_display_order', 10, 2 );
function product_add_on_display_order( $cart_item, $order_item ){
    if( isset( $order_item['custom_text_add_on'] ) ){
        $cart_item_meta['custom_text_add_on'] = $order_item['custom_text_add_on'];
    }
    return $cart_item;
}

add_filter( 'woocommerce_email_order_meta_fields', 'product_add_on_display_emails' ); 
function product_add_on_display_emails( $fields ) { 
    $fields['custom_text_add_on'] = 'Custom Text Add-On';
    return $fields; 
}

Advertisement

Answer

I have copied the code and done a quick test and could found that you are missing underscore _ for the field name in two functions. You were using $_POST['custom_text_add_on'] instead of $_POST['_custom_text_add_on'] in the product_add_on_cart_item_data function. That was just a mistake.

add_filter( 'woocommerce_add_cart_item_data', 'product_add_on_cart_item_data', 10, 2 ); 
function product_add_on_cart_item_data( $cart_item, $product_id ){
    if( isset( $_POST['_custom_text_add_on'] ) ) {
        $cart_item['custom_text_add_on'] = sanitize_text_field( $_POST['_custom_text_add_on'] );
    }
    return $cart_item;
}

Hope you got the issue resolved

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement