How to add an input field to WooCommerce cart items

Let users update input fields in the cart

This is a simple method to add a text input field to each WooCommerce cart item. Customers can enter text into the field which will be saved as meta data to the cart item – so you will receive it with the order.

Add a textarea field to a WooCommerce cart item

First, we just need to add the textarea field. We use the woocommerce_after_cart_item_name hook so our textarea will appear after the product name.

* Add a text field to each cart item
function prefix_after_cart_item_name( $cart_item, $cart_item_key ) {
$notes = isset( $cart_item['notes'] ) ? $cart_item['notes'] : '';
'<div><textarea class="%s" id="cart_notes_%s" data-cart-id="%s">%s</textarea></div>',
add_action( 'woocommerce_after_cart_item_name', 'prefix_after_cart_item_name', 10, 2 );
* Enqueue our JS file
function prefix_enqueue_scripts() {
wp_register_script( 'prefix-script', trailingslashit( plugin_dir_url( __FILE__ ) ) . 'update-cart-item-ajax.js', array( 'jquery-blockui' ), time(), true );
'ajaxurl' => admin_url( 'admin-ajax.php' )
wp_enqueue_script( 'prefix-script' );
add_action( 'wp_enqueue_scripts', 'prefix_enqueue_scripts' );

You can see below how it’ll look in the cart (using the Storefront theme).

WooCommerce cart item textarea

At the moment, the user will be able to enter text into the field but the text won’t save. We are going to use some AJAX to save the text.

The code above not only adds the textarea to the cart item, it also enqueues a JavaScript file ready for our AJAX.

$('.prefix-cart-notes').on('change keyup paste',function(){
message: null,
overlayCSS: {
background: '#fff',
opacity: 0.6
var cart_id = $(this).data('cart-id');
type: 'POST',
url: prefix_vars.ajaxurl,
data: {
action: 'prefix_update_cart_notes',
security: $('#woocommerce-cart-nonce').val(),
notes: $('#cart_notes_' + cart_id).val(),
cart_id: cart_id
success: function( response ) {

Now, when the user types anything, the contents of the text field get sent back to the server ready to be saved as meta data to the cart item.

* Update cart item notes
function prefix_update_cart_notes() {
// Do a nonce check
if( ! isset( $_POST['security'] ) || ! wp_verify_nonce( $_POST['security'], 'woocommerce-cart' ) ) {
wp_send_json( array( 'nonce_fail' => 1 ) );
// Save the notes to the cart meta
$cart = WC()->cart->cart_contents;
$cart_id = $_POST['cart_id'];
$notes = $_POST['notes'];
$cart_item = $cart[$cart_id];
$cart_item['notes'] = $notes;
WC()->cart->cart_contents[$cart_id] = $cart_item;
wp_send_json( array( 'success' => 1 ) );
add_action( 'wp_ajax_prefix_update_cart_notes', 'prefix_update_cart_notes' );
function prefix_checkout_create_order_line_item( $item, $cart_item_key, $values, $order ) {
foreach( $item as $cart_item_key=>$cart_item ) {
if( isset( $cart_item['notes'] ) ) {
$item->add_meta_data( 'notes', $cart_item['notes'], true );
add_action( 'woocommerce_checkout_create_order_line_item', 'raa_add_custom_data_to_order', 10, 4 );

The prefix_update_cart_notes function does a security check using the WooCommerce cart nonce then saves the content of the textarea as meta data in the cart item. You can check out this article for more information about updating cart meta for items that have already been added to the cart.

Add the custom text to the order meta

Finally, we want to pass our meta data to the order so that we can use it after the customer has checked out. The prefix_checkout_create_order_line_item function takes care of that, iterating through each item and saving notes when it finds them.

Popular Posts

2 thoughts on “How to add an input field to WooCommerce cart items

Hi Gareth, is there a way to limit this function to particular product IDs in the cart? Thanks


Hi Edwin – yes, you’d need to add a conditional statement in the prefix_after_cart_item_name function to check whether the cart item contained the product ID.


Leave a Reply

Your email address will not be published. Required fields are marked *