Adding Custom Fields to Woocommerce Product Variation

This post is about how to add custom fields to woocommerce product variation. Woocommerce product variation has extended fields available to you by defaults. These fields can be seen the figure below

Adding Custom Fields Woocommerce Product Variation

But sometimes you need to add additional fields to your product variation level. This could be number of stocks of different stores, some unique code other than SKU.  Here are the steps you need to do

Create a new field for user input

Add this code to you theme’s functions.php file.

The code above will add 4 extra fields to your product variation. One of them is text field and the other three are number fields. These are the complete array of fields you can add.

  • Text Field
  • Number Field
  • Textarea
  • Dropdown Select
  • Checkbox
  • Hidden field

After adding this code you can see them appear in the backend.

Adding Custom Fields Woocommerce Product Variation

Save the data to the database

Pretty cool right! The next to do is to make sure that they are save to the database. For that you need to add another function to the theme’s functions.php.

The code above will make sure that you save them to the database. As you can see there, the value will be stored in the wp_postmeta table. Here you can specify what “meta_key” you want.

In order to show them to the front end. You can always call the function get_post_ meta(). I hope this post help you guys. See you on the next post!

2 Comment

  1. Hi,
    great post, thanks for your work.
    Why don’t you give an example of how to display the ‘ean_code’ .
    Then we don’t have to look further ….

    regards

    Joe

  2. […] closest solution I found does not link up to custom fields. The code, shown below, shows how one can set up input […]

Leave a Reply