I’m trying to get form input from the product page to the cart, but no luck so far. This is what I did:
-
Made a file called custom-input.php with an input field inside.
<input type="text" id="custom-input" name="custom-input" value="custom-input">
-
Added the field inside the add to cart form (simple.php) on the product page using an action hook. I only want it to display if the product has a certain tag so there’s an if statement too.
<?php if ( has_term( 'custom-input-tag', 'product_tag' ) ) { do_action( 'woocommerce_add_custom_input' ); } ?>
-
Added a row inside the table in cart-totals.php to display the form output.
<tr class="custom-input"> <th><?php _e( 'Your custom input is', 'woocommerce' ); ?></th> <td><!--Custom input field text comes here--></td> </tr>
After doing some research inside Woocommerce files I found that they use the WC cart class. It stores user data within sessions. The cart totals for example are retrieved with this code:
function wc_cart_totals_subtotal_html() {
echo WC()->cart->get_cart_subtotal();
}
<td><?php wc_cart_totals_subtotal_html(); ?></td>
I think this is the way to go, but failed to make a function that puts the form input on my cart page. If anyone has thoughts on this, help would be much appreciated!
Read more here: Post input field text from product page to cart