I have created some custom fields on my product page and would like to display them in the cart but for the life of me I cannot figure out how to?
I would like the fields saved and displayed on the cart and in the order admin. I am not quite sure how to display the information and would appreciate any help.
Here is my code:
//add the fields to the form
add_action( 'woocommerce_before_add_to_cart_button', 'my_custom_checkout_field' );
function my_custom_checkout_field($fields) {
echo '<table class="table variations">
<tr>
<td class="label"><label class="control-label span3" for="inputTextone">'.__('Text Line 1', 'woocommerce').' </label></td>
<td class="value"><input type="text" name="text_line_1" id="inputTextone" class="span11"></td>
</tr>
<tr>
<td class="label"><label class="control-label span3" for="inputTexttwo">'.__('Text Line 2', 'woocommerce').' </label></td>
<td class="value"><input type="text" name="text_line_2" id="inputTexttwo" class="span11"></td>
</tr>
<tr>
<td class="label"><label class="control-label span3">'.__('Additional Info', 'woocommerce').' </label></td>
<td class="value"><input type="text" name="additional_info" id="inputAdditional" class="span11"></td>
</tr>
</table>';
}
//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['text_line_1'] = $_POST['text_line_1'];
$cart_item_meta['text_line_2'] = $_POST['text_line_2'];
$cart_item_meta['additional_info'] = $_POST['additional_info'];
return $cart_item_meta;
}
//add the data to the session
add_filter( 'woocommerce_get_cart_item_from_session', 'get_cart_items_from_session', 1, 3 );
function get_cart_items_from_session( $item, $values, $key ) {
if (array_key_exists( 'text_line_1', $values ) ) {
$item['text_line_1'] = $values['text_line_1'];
}
return $item;
}
//display the data on the cart page
add_filter('woocommerce_get_item_data', 'show_custom_data');
function show_cart_data( $custom_fields ) {
$custom_fields['text_line_1'] = $_POST['text_line_1'];
return $custom_fields;
}