If your store offers products across multiple categories, you might find that only certain items require personalization, such as engraved gifts, custom messages on apparel, or special instructions. With this customization, we’ll add a custom text field only to specific categories that need personalization. For example, in a store that sells both t-shirts and winter wear, only t-shirts might need custom messages. This code helps you focus on those particular categories, ensuring a tailored and efficient shopping experience for your customers.
Solution: Show Custom Input Fields for Specific Product Categories in WooCommerce
This code snippet lets store owners add a custom text field to products in a specific category id of 65 as specified in the code. The inputted text is then shown in the cart, during checkout, and in the order details, making it visible to both the customer and the store admin.
add_action( 'woocommerce_before_add_to_cart_button', 'ts_display_custom_text_field', 9 ); function ts_display_custom_text_field() { global $product; // Check if the product belongs to category ID 65 if ( has_term( '65', 'product_cat', $product->get_id() ) ) { echo '<div class="custom-text-field">'; woocommerce_form_field( 'custom_text', array( 'type' => 'text', 'required' => false, // Change to true if field is required 'label' => 'Custom Text', 'placeholder' => 'Enter custom text here...', )); echo '</div>'; } } // Add custom text to cart item data only for products in category ID 65 add_filter( 'woocommerce_add_cart_item_data', 'ts_add_custom_text_to_cart', 10, 2 ); function ts_add_custom_text_to_cart( $cart_item_data, $product_id ) { $product = wc_get_product( $product_id ); // Check if the product belongs to category ID 65 and the custom text is set if ( has_term( '65', 'product_cat', $product_id ) && isset( $_POST['custom_text'] ) ) { $cart_item_data['custom_text'] = sanitize_text_field( $_POST['custom_text'] ); } return $cart_item_data; } // Display custom text in cart and checkout add_filter( 'woocommerce_get_item_data', 'ts_display_custom_text_in_cart', 10, 2 ); function ts_display_custom_text_in_cart( $cart_data, $cart_item ) { if ( isset( $cart_item['custom_text'] ) ) { $cart_data[] = array( 'name' => 'Custom Text', 'value' => sanitize_text_field( $cart_item['custom_text'] ), ); } return $cart_data; } // Save the custom text field value to the order items only for products in category ID 65 add_action( 'woocommerce_checkout_create_order_line_item', 'ts_bbloomer_save_custom_text_to_order_items', 10, 4 ); function ts_bbloomer_save_custom_text_to_order_items( $item, $cart_item_key, $values, $order ) { if ( isset( $values['custom_text'] ) ) { $item->add_meta_data( 'Custom Text', $values['custom_text'], true ); } } // Display custom text in admin order items table add_filter( 'woocommerce_order_item_name', 'ts_custom_text_display_in_admin_order_items_table', 10, 2 ); function ts_custom_text_display_in_admin_order_items_table( $item_name, $item ) { // Check if the item has custom text associated with it if ( $custom_text = $item->get_meta( 'Custom Text' ) ) { // Append the custom text to the item name $item_name .= '<br><small>' . esc_html__( 'Custom Text:', 'your-textdomain' ) . ' ' . esc_html( $custom_text ) . '</small>'; } return $item_name; }
Output
When a customer views a product that belongs to category ID 65 (as specified in the code), they’ll see a custom text field labeled “Custom Text” above the “Add to Cart” button. Here, they can enter a personalized message or any specific text they want to include with their order.
As I have specified the category ID 65 for t-shirts in the code, the custom field will appear only for products in this category, as shown below.
Adding custom input fields on the frontend of the product page allows you to gather customer messages easily. Alternatively, if you wish to display dynamic messages based on customer-selected variations, you can add the custom input field within the variations tab of the admin interface.