1

In my woocommerce product page I am trying to add a custom input field so that customers can enter a value in the quantity field that will be carried through the checkout process.

I found some documentation on the Woocommerce site about hooks/actions http://docs.woothemes.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/.

I have added the code below to my functions.php file and at the very least it should display an input field on the product field but it only displays the h2 value.

<?php
/**
* Add the field to the product page
*/
add_action( 'woocommerce_before_add_to_cart_button', 'custom_quantity' );

function custom_quantity( $product ) {

echo '<div id="my_custom_checkout_field"><h2>' . __('My Field') . '</h2>';

woocommerce_form_field( 'my_field_name', array(
    'type'          => 'text',
    'class'         => array('my-field-class form-row-wide'),
    'label'         => __('Fill in this field'),
    'placeholder'   => __('Enter something'),
    ), $product->get_value( 'my_field_name' ));

echo '</div>';

}
?>

Can anyone help as to why this is happening? Is there a fault in my code. I am new to woocommerce and trying to figure this out.

fnk
  • 301
  • 2
  • 4
  • 16
  • I believe you need to `echo woocommerce_form_field()`. Also, as mentioned at this question: http://stackoverflow.com/q/27313882/383847 you may want to consider the [Product Add-ons](http://www.woothemes.com/products/product-add-ons/) plugin. – helgatheviking Dec 08 '14 at 13:38
  • echo woocommerce_form_field() doesn't work unfortunately. Prefer to do this in code as opposed to buying the plugin. – fnk Dec 08 '14 at 14:03
  • Have you tried adding the 'default' option? `'default' => ''` – Tim Hallman Dec 08 '14 at 14:55
  • If so the issue may be with the `woocommerce_before_add_to_cart_button` hook. Have you tried other hooks? Also, I believe you'll need another function to validate and process the input field. – Tim Hallman Dec 08 '14 at 14:58
  • I tried `default => ''` but no luck, I think it may be the woocommerce_before_add_to_cart_button but I'm pretty sure that is the hook I need to add a field to the form. I have tried other hooks and the same thing happens. The input field doesn't appear. I will add another function to validate the field, for now I am trying to figure out why the field won't appear. – fnk Dec 08 '14 at 15:09
  • Its answered here: http://stackoverflow.com/questions/21650019/woocommerce-add-input-field-to-every-item-in-cart – Trigve Hagen Oct 16 '16 at 21:52

0 Answers0