Anders Kitson
Anders Kitson

Reputation: 1545

Trying to place a custom field after the total section in the checkout page in woocommerce

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 (seen in a comment in the code below 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>';
}

Upvotes: 2

Views: 711

Answers (1)

entreprenerds
entreprenerds

Reputation: 1037

Remove $checkout->get_value('custom_field_name') so that it's just:

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') ,
    )
);

Any user value should be filled automatically, or it will simply be blank.

Upvotes: 2

Related Questions