Hi Anna,
Thank you for your incredible support. I have tried to use your code to add a “Note to Seller” that is on every single product page and can then be seen in the “Order Details” in the dashboard. I used this code:
// Message or Custom Field for the Product Page for Customers to enter text.
// This should be added to your theme/child theme functions.php file
/* Creates Field after add to cart button */
function add_custom_info_field() {
echo ‘<table class=”variations” cellspacing=”0″>
<tbody>
<tr>
<td class=”label”><label for=”custom”><span style=”font-size:10px;color:#999;font-weight:400;”>Message:</span></label></td>
<td class=”value”>
<input type=”text” name=”custom-info-message” size=”30″value=”” placeholder=”Add a Message Here…” />
</td>
</tr>
</tbody>
</table>’;
}
add_action( ‘woocommerce_after_add_to_cart_button’, ‘add_custom_info_field’ );
/* Saves field data */
function save_add_custom_info_field( $cart_item_data, $product_id ) {
if( isset( $_REQUEST[‘custom-info-message’] ) ) {
$cart_item_data[ ‘custom_info_message’ ] = $_REQUEST[‘custom-info-message’];
/* below statement make sure every add to cart action as unique line item */
$cart_item_data[‘unique_key’] = md5( microtime().rand() );
}
return $cart_item_data;
}
add_action( ‘woocommerce_add_cart_item_data’, ‘save_add_custom_info_field’, 10, 2 );
/* Renders field entry on cart and checkout */
function render_mssg_meta_on_cart_and_checkout( $cart_data, $cart_item = null ) {
$custom_items = array();
if( !empty( $cart_data ) ) {
$custom_items = $cart_data;
}
if( isset( $cart_item[‘custom_info_message’] ) ) {
$custom_items[] = array( “name” => ‘Custom Info or Message’, “value” => $cart_item[‘custom_info_message’] );
}
return $custom_items;
}
add_filter( ‘woocommerce_get_item_data’, ‘render_mssg_meta_on_cart_and_checkout’, 10, 2 );
/* Renders field info onto orders pages and emails */
function custom_info_order_meta_handler( $item_id, $values, $cart_item_key ) {
if( isset( $values[‘custom_info_message’] ) ) {
wc_add_order_item_meta( $item_id, “custom_info_message”, $values[‘custom_info_message’] );
}
}
add_action( ‘woocommerce_add_order_item_meta’, ‘custom_info_order_meta_handler’, 1, 3 );
—————————————
and it works perfectly. There is just one thing I am wondering about that I cannot figure out.
I want to make it a textarea, or, a multi-line text, so that users can press “Enter” without the form adding what they have written and the item to the cart. I just can’t seem to figure out how to do this and have the form still work. Is this by any chance something that by just looking at it you could know easily what code is needed to make the form a textarea, and allow the user to press enter to separate lines of text without the product being immediately added to the cart?
Thank you so much.