0

This code correctly works but does not place the custom field where I want it. In the add_action I would like to use woocommerce_review_order_after_order_total as the location attribute to place it on the page. I got the hook name from this visual guide https://businessbloomer.com/woocommerce-visual-hook-guide-checkout-page/ However this hook breaks my code and gives me the following php error.

Fatal error: Uncaught Error: Call to a member function get_value() on string in /Users/anderskitson/Local Sites/river-cafe/app/public/wp-content/themes/salient-child/functions.php on line 96

Hopefully someone can give me hand. Thanks

/**

* Add custom field to the checkout page

*/

add_action('woocommerce_after_order_notes', 'custom_checkout_field');

/* add_action('woocommerce_review_order_after_order_total', 'custom_checkout_field');*/
/* ^ This is the code that is breaking */ 

function custom_checkout_field($checkout)

{

echo '<div id="custom_checkout_field"><h2>' . __('New Heading') . '</h2>';

woocommerce_form_field('custom_field_name', array(

'type' => 'text',

'class' => array(

'my-field-class form-row-wide'

) ,

'label' => __('Custom Additional Field') ,

'placeholder' => __('New Custom Field') ,

) ,

$checkout->get_value('custom_field_name'));

echo '</div>';

}
  • If you're going to use woocommerce_review_order_after_order_total, then you'd need to use WC()->checkout, and define the function as function custom_checkout_field() - i.e. without the $checkout which WC doesn't pass to the function.. – Sally CJ Nov 6 '19 at 18:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.