0

I want to add the some custom fields at checkout with woocommerce and then use this info to call an API to validate the data provided. For this I need these fields to be created and registered in the process so that I can get this data later, however I am having some issue with the get_value() function as it leads to a PHP fatal error.

add_action( 'woocommerce_checkout_before_order_review_heading', 'insurance_choice_checkout');
function insurance_choice_checkout () {

        function insurance_validation_div() {
         woocommerce_form_field( 'gender-selection', array(
      'type'            => 'radio',
      'required'        => true,
      'label'           => pll__('Veuillez indiquer votre sexe'),
      'class'           => array('radio-checkout'),
      'options'         => array(
        'male'         => pll__('Homme'),
        'female'    => pll__('Femme'),
      )), $checkout->get_value('gender-selection'));
            
            woocommerce_form_field( 'insurance-birthdate', array(
       'type'          => 'date',
        'id'          => 'birthpicker',
       'class'         => array('insurebd'), // CSS Class
       'label_class'   => array('insurelabelbd'),
       'input_class'   => array('insureinputbd'),
       'required'      => true, // Mandatory or Optional
       'label'         => pll__('Veuillez entrer votre date de naissance'),
     ), $checkout->get_value('insurance-birthdate'));

        
        }

        ?>
<div id="insurance_row_checkout">
    <div class="loader" id="loaderdiv"></div>           
    <h2 class="insurancecheckout">

            <div class="insurancegb" style="display:none;">
                
        <?php insurance_validation_div()?>

            </div>
</div>

I removed everything from the code that wasn't relevant for simplicity. Basically, since i added $checkout->get_value('insurance-birthdate')) and $checkout->get_value('gender-selection')) to register these custom fields in the checkout process I get the following PHP error:

Uncaught Error: Call to a member function get_value() on null

I am struggling to find why this is causing this.

Eliott
  • 89
  • 10
  • `$checkout->get_value(..` where is `$checkout` defined? hence the error message – 7uc1f3r Dec 16 '20 at 14:31
  • You're right thank you, WC()->checkout->get_value() works instead! – Eliott Dec 16 '20 at 14:32
  • 1
    You're welcome. Some hooks are better suited to add a custom field to the checkout page because they already contain `$checkout` as parameter, see for example [Add a new custom field to Woocommerce checkout](https://stackoverflow.com/a/61208720/11987538) or [WooCommerce 4.0 custom checkout & ACF field value on email, admin order, and thank you page](https://stackoverflow.com/a/60680437/11987538). Regards – 7uc1f3r Dec 16 '20 at 14:35

0 Answers0