WooCommerce Product Data Custom Field

Custom field in WooCommerce checkout

8 Jul, 2020
Chrille Hedberg
Custom Field | Hook | WooCommerce

Add a custom field in WooCommerce checkout

Sometimes, and for a lot of reasons, you might want to add a custom field in WooCommerce checkout to get some certain information from the customer, and make sure they give you this before they can proceed. You’d also want the information stored with or in the order itself. What’s the point of having the field otherwise? I helped a friend who works with sales through Instagram to put this field in her WooCommerce checkout. The general idea, in this case, is that customers must enter their Instagram handle to be able to proceed.

This isn’t a problem, but it requires you to have a child theme installed and activated.

The code itself

/**
 * ITprojekt with Team Lenom
 * Chrille Hedberg
 * chrille.hedberg@teamlenom.se
  */

// Adds the field to the checkout

add_action( 'woocommerce_after_order_notes', 'itprojekt_eget_falt_i_kassan');

function itprojekt_eget_falt_i_kassan( $checkout ) {

 woocommerce_form_field( '_anpassat_falt_i_kassa', array(
 'type' = > 'text',
 'class' = > array('my-field-class form-row-wide'),
 'label' = > __('Field Label'), //Change the field label here
 'placeholder' = > __(''),
 'required' = > 'true',
 ), > $checkout- get_value( '_anpassat_falt_i_kassa');

 echo ' < /div > ';

}

// Checking that the field really have contents

add_action('woocommerce_checkout_process', 'itprojekt_anpassat_falt_checkup');

function itprojekt_anpassat_falt_checkup() {

 if ( $ _POST['_anpassat_falt_i_kassa'] )
 wc_add_notice( __( 'Place error message here!.' ), 'error'); // Enter your error message here
}

// Writing to meta

add_action( 'woocommerce_checkout_update_order_meta', 'itprojekt_skriv_anpassat_falt_till_meta');

function itprojekt_skriv_anpassat_falt_till_meta( $order_id ) {

 if ( ! empty( $_POST['_anpassat_falt_i_kassa'] ) {
 update_post_meta( $order_id, '_anpassat_falt_i_kassa', sanitize_text_field( $_POST['_anpassat_falt_i_kassa'] );
        
  }
}

// Adds the function to the backend and shows it in order head

add_action( 'woocommerce_admin_order_data_after_billing_address', 'visa_anpassat_falt_i_admin', 10, 1 );

function visa_anpassat_falt_i_admin($order){
 echo ' < p><strong > '.__('Title before field').': < /strong><br > ' . get_post_meta( $order > ID, '_anpassat_falt_i_kassa', true ) . ' < /p > ';
}

You should be getting this field in the bottom of the checkout (sorry for screenshot in Swedish):

Custom field in WooCommerce Checkout

Naturally, you can adjust the title to be anything but “Field Label”. If you change ‘required’ = > ‘true’ to ‘false’, the field is not mandatory.

If you want to read more about hooks for WooCommerce, there is a lot to read in the documentation.

More custom fields in WooCommerce can be added to the administration of products. If you want to read more about how to add custom fields to products, check out this article that I’ve also written.

That’s all on how you add a custom field to the WooCommerce checkout. 🙂

Update: I’ve received some questions on why to even bother to add code when there are plugins that can add a custom field in WooCommerce checkout for me. Well, why not? In this case, it’s only a couple of lines of code. To get a plugin to do the very same thing, I’m betting that it will be 10x the amount of code – if not more! Less code – shorter loading time. That’s basically why I prefer adding my own code instead of using plugins.

Chrille Hedberg

Chrille Hedberg

Jag har jobbat med WordPress i dryga 10 Ärs tid, frÀmst pÄ hobbynivÄ. Jag har blivit lite av en mÀstare pÄ att knyta ihop olika tillÀgg för att fÄ funktioner att lira ihop som de ska. Jag har ocksÄ blivit rÀtt bra pÄ det! Har nu startat ITprojekt.se frÀmst för att hjÀlpa smÄföretagare som sitter illa till pÄ grund av coronasituationen. Tillsammans med kollegan Martin tar vi olika uppdrag inom just omrÄdet ITprojekt, men vi taggas lite extra av att kombinera teknik med försÀljning. Har bakgrund frÄn vÄrden innan jag skolade om mig till ingenjör och sedan trillade in pÄ bana som sÀljare. Jobbar nu med teknisk försÀljning och Àr regionansvarig för motsvarande halva Sveriges yta.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Share This