I am trying to add a text field that accepts a text value from user on the product page before he/she clicks the "Add To Card" button. Here is the code that I have added in functions.php file:
function action_woocommerce_before_add_to_cart_button( )
{
// make action magic happen here...
echo "<input type='text' name='engrave-text' id='engrave-text' placeholder='Your Engraving Text'>";
};
// add the action
add_action( 'woocommerce_before_add_to_cart_button', 'action_woocommerce_before_add_to_cart_button', 10, 0 );
//Store the custom field
add_filter( 'woocommerce_add_cart_item_data', 'add_cart_item_custom_data_vase', 10, 2 );
function add_cart_item_custom_data_vase( $cart_item_meta, $product_id ) {
global $woocommerce;
$cart_item_meta['test_field'] = $_POST['engrave-text'];
return $cart_item_meta;
}
//Get it from the session and add it to the cart variable
function get_cart_items_from_session( $item, $values, $key ) {
if ( array_key_exists( 'test_field', $values ) )
$item[ 'Engraved-Text' ] = $values['test_field'];
return $item;
}
add_filter( 'woocommerce_get_cart_item_from_session', 'get_cart_items_from_session', 1, 3 );
But it is not working. Can you please help?
Thank you
You can add the fields or any thing like image or text to display using this hook
use this hook in functions.php or in your plugin file
I figured it out. The following code works: