Digital experiences for all disciplines
New Landing › How can we help? › Themeforest Theme Support › Dante › How to add a custom field in woocommerce checkout?
New Landing › How can we help? › Themeforest Theme Support › Dante › How to add a custom field in woocommerce checkout?
- This topic has 7 replies, 5 voices, and was last updated 9 years by Rui Guerreiro – SUPPORT.
-
Posted in: Dante
-
September 10, 2014 at 8:47 pm #109256
Hello,
Im trying to add a custom woocommerce checkout field by adding this code to the dante-child theme functions.php. (Its taken from the woocommerce tutorial about the subject.)
/** * Add the field to the checkout **/ add_action('woocommerce_after_order_notes', 'my_custom_checkout_field'); function my_custom_checkout_field( $checkout ) { 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'), ), $checkout->get_value( 'my_field_name' )); echo '</div>'; } /** * Process the checkout **/ add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process'); function my_custom_checkout_field_process() { global $woocommerce; // Check if set, if its not set add an error. if (!$_POST['my_field_name']) $woocommerce->add_error( __('Please enter something into this new shiny field.') ); } /** * Update the order meta with field value **/ add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta'); function my_custom_checkout_field_update_order_meta( $order_id ) { if ($_POST['my_field_name']) update_post_meta( $order_id, 'My Field', esc_attr($_POST['my_field_name'])); }
But nothing happens when i refresh/clear the cache of the checkout page. How come? What am I doing wrong?
September 11, 2014 at 1:35 am #109374HI,
It should work, I put that in the functions.php and it works.. Can you try that in the parent theme functions.php http://take.ms/Ua6UO
Let us know,
Thanks,
laranz.September 11, 2014 at 7:22 am #109426Hi laranz,
If i put it in the parent theme functions.php everything gets blank which to me indicates there is something wrong in the php/code?
This is how my entire child-theme functions look like:
<?php /* * * Dante Functions - Child Theme * ------------------------------------------------ * These functions will override the parent theme * functions. We have provided some examples below. * * */ /** * Add the field to the checkout **/ add_action('woocommerce_after_order_notes', 'my_custom_checkout_field'); function my_custom_checkout_field( $checkout ) { 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'), ), $checkout->get_value( 'my_field_name' )); echo '</div>'; } /** * Process the checkout **/ add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process'); function my_custom_checkout_field_process() { global $woocommerce; // Check if set, if its not set add an error. if (!$_POST['my_field_name']) $woocommerce->add_error( __('Please enter something into this new shiny field.') ); } /** * Update the order meta with field value **/ add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta'); function my_custom_checkout_field_update_order_meta( $order_id ) { if ($_POST['my_field_name']) update_post_meta( $order_id, 'My Field', esc_attr($_POST['my_field_name'])); } ?>
September 11, 2014 at 9:33 am #109475This reply has been marked as private.September 11, 2014 at 1:15 pm #109571This reply has been marked as private.September 12, 2014 at 9:30 am #109985Hi,
You are using very old version 2.53 of theme so please upgrade to latest version 2.66 of theme. Hope that should resolve the issue easily.
Thanks 🙂
With Best Regards
MohammadOctober 9, 2014 at 2:15 pm #118900Hello,
Is it possible to have this field in the cart page?I need to have order notes on the cart page instead of on the checkout page.
I tried to put in “add_action(‘woocommerce_after_cart_table'” but that didn’t work for me.October 9, 2014 at 2:39 pm #118914Hi,
Adding checkout fields it’s a know request and several solutions in the web.
Adding fields to the cart page it’s a different thing.You should contact Woocommerce support with this request.
-Rui
-
Posted in: Dante
You must be logged in and have valid license to reply to this topic.