Error when adding a woocommerce checkout field.
-
I am trying to use Code Snippets to add a custom field to my checkout form on woocommerce using the Shopper theme.
The code works if I use the hook “woocommerce_after_order_notes”, but I am wanting it to be placed immediately before the “Place Order” button.
If I use any of the hooks on the right hand side of the checkout page, I get the following errors:PHP Fatal error: Uncaught Error: Call to a member function get_value() on string in /var/www/html/wordpress/wp-content/plugins/code-snippets/php/snippet-ops.php(446) : eval()’d code:15
Stack trace:
#0 /var/www/html/wordpress/wp-includes/class-wp-hook.php(287): my_custom_checkout_field(”)
#1 /var/www/html/wordpress/wp-includes/class-wp-hook.php(311): WP_Hook->apply_filters(”, Array)
#2 /var/www/html/wordpress/wp-includes/plugin.php(478): WP_Hook->do_action(Array)
#3 /var/www/html/wordpress/wp-content/plugins/woocommerce/templates/checkout/payment.php(49): do_action(‘woocommerce_rev…’)
#4 /var/www/html/wordpress/wp-content/plugins/woocommerce/includes/wc-core-functions.php(251): include(‘/var/www/html/w…’)
#5 /var/www/html/wordpress/wp-content/plugins/woocommerce/includes/wc-template-functions.php(2240): wc_get_template(‘checkout/paymen…’, Array)
#6 /var/www/html/wordpress/wp-includes/class-wp-hook.php(287): woocommerce_checkout_payment(”)
#7 /var/www/html/wordpress/wp-includes/class-wp-hook.php(311): WP_Hook->apply_fi in /var/www/html/wordpress/wp-content/plugins/code-snippets/php/snippet-ops.php(446) : eval()’d code on line 15The code in the snippet is:
/**
* Add the field to the checkout
*/
add_action( ‘woocommerce_review_order_before_submit’, ‘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() {
// Check if set, if its not set add an error.
if ( ! $_POST[‘my_field_name’] )
wc_add_notice( __( ‘Please enter something into this new shiny field.’ ), ‘error’ );
}/**
* 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 ( ! empty( $_POST[‘my_field_name’] ) ) {
update_post_meta( $order_id, ‘My Field’, sanitize_text_field( $_POST[‘my_field_name’] ) );
}
}/**
* Display field value on the order edit page
*/
add_action( ‘woocommerce_admin_order_data_after_billing_address’, ‘my_custom_checkout_field_display_admin_order_meta’, 10, 1 );function my_custom_checkout_field_display_admin_order_meta($order){
echo ‘<p>‘.__(‘My Field’).’: ‘ . get_post_meta( $order->id, ‘My Field’, true ) . ‘</p>’;
}add_filter(‘woocommerce_email_order_meta_keys’, ‘my_custom_order_meta_keys’);
function my_custom_order_meta_keys( $keys ) {
$keys[] = ‘My Field’; // This will look for a custom field called ‘My Field’ and add it to emails
return $keys;
}Any help would be greatly appreciated.
Thanks.
The topic ‘Error when adding a woocommerce checkout field.’ is closed to new replies.