如何在Woocommerce单个产品页添加自定义订单备注(无插件PHP实现)
Your current code uses the woocommerce_after_order_notes hook, which only targets the checkout page. To add the field to the single product page and carry its value through to the cart and final order, follow these complete PHP steps:
1. Add the Custom Field to the Single Product Page
We'll place the field right above the "Add to Cart" button using the woocommerce_before_add_to_cart_button hook (adjust the hook if you want it positioned elsewhere):
// Add custom note field to single product page add_action('woocommerce_before_add_to_cart_button', 'add_single_product_custom_note_field'); function add_single_product_custom_note_field() { echo '<div class="custom-product-note-section">'; woocommerce_form_field('product_custom_note', array( 'type' => 'textarea', // Use 'text' instead if you want a short input field 'class' => array('form-row-wide'), 'label' => __('Custom Instructions for This Product'), 'placeholder' => __('Enter your special requests here...'), 'required' => false, // Set to true if this field is mandatory ), ''); echo '</div>'; }
2. Save the Field Value to Cart Item Data
When the user adds the product to their cart, we store the note as metadata tied to that specific cart item:
// Save custom note to cart item metadata add_filter('woocommerce_add_cart_item_data', 'save_product_note_to_cart_item', 10, 3); function save_product_note_to_cart_item($cart_item_data, $product_id, $variation_id) { if (!empty($_POST['product_custom_note'])) { $cart_item_data['product_custom_note'] = sanitize_textarea_field($_POST['product_custom_note']); // Add unique key to preserve separate notes for identical products in cart $cart_item_data['unique_note_key'] = md5(microtime() . rand()); } return $cart_item_data; }
3. Display the Note in Cart and Checkout Pages
Show the custom note under the product name in both the cart and checkout screens:
// Display custom note in cart and checkout add_filter('woocommerce_get_item_data', 'show_product_note_in_cart', 10, 2); function show_product_note_in_cart($item_data, $cart_item) { if (isset($cart_item['product_custom_note'])) { $item_data[] = array( 'key' => __('Custom Product Note'), 'value' => $cart_item['product_custom_note'], ); } return $item_data; }
4. Save the Note to the Order Line Item
When the order is placed, attach the note to the corresponding order item so it appears in order details (for both you and the customer):
// Save custom note to order line item add_action('woocommerce_checkout_create_order_line_item', 'save_product_note_to_order', 10, 4); function save_product_note_to_order($item, $cart_item_key, $values, $order) { if (isset($values['product_custom_note'])) { $item->add_meta_data(__('Custom Product Note'), $values['product_custom_note'], true); } }
5. (Optional) Style the Field
Add this CSS to your theme's custom CSS section to make the field look polished:
.custom-product-note-section { margin-bottom: 1.5em; } .custom-product-note-section textarea { width: 100%; padding: 10px; border: 1px solid #e0e0e0; border-radius: 4px; resize: vertical; }
How to Implement
- Copy all the PHP code snippets into your child theme's
functions.phpfile (avoid editing the parent theme directly to prevent losing changes on updates). - Adjust the field type, label, placeholder, and required status to match your needs.
This setup ensures the custom note is tied to each individual product in the cart—so if a customer adds the same product multiple times with different notes, each will be preserved separately. The note will appear in the order details for both you (in the WordPress admin) and the customer (in their order confirmation and account page).
内容的提问来源于stack exchange,提问作者Saravana

