0

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;

}
fnk
  • 301
  • 2
  • 4
  • 16

1 Answers1

1

I have written an article for the exact purpose. http://sarkware.com/how-to-pass-custom-data-to-cart-line-item-in-woocommerce-without-using-plugins/

Sark
  • 4,458
  • 5
  • 26
  • 25
  • The only thing i noticed is that the labels are displaying as name_on_tshirt as opposed to name on tshirt – fnk Dec 12 '14 at 10:36
  • you can change the meta key here : wc_add_order_item_meta( $item_id, "name on tshirt", WC()->session->get( $cart_item_key.'_name_on_tshirt') ); without any space, but i am afraid keys should be without any white space. you might want to look at this http://stackoverflow.com/questions/2003036/php-using-spaces-in-associative-array-indices – Sark Dec 12 '14 at 10:43