Did you know you can easily add your own custom fields to any WooCommerce Product or WordPress Post? That is already built into WordPress.
To see the Custom Fields section, make sure Custom Fields is enabled in the screen options on the admin product pages (click on the “Screen Options” tab near the top right of any edit product page), and then you will see the Custom Fields area for the post after all the Woocommerce product info boxes. There you can add and define your own custom field for a product, and there is even a link to a WordPress page showing you various methods on how you can use those custom fields in your theme.
Once you set up your own custom field, then on all products you will see that field available in the drop down list of custom fields so you can set a value for each product after that without having to add a new field each time (only need to set up the custom postmeta field once).
Here is the WordPress codex page with info on how to use those custom fields:
https://codex.wordpress.org/
Here is an example. I had a client that wanted to display an ISBN, or barcode, number for each product to certain customers. For that we create a custom field called: isbn
That will be the postmeta key that you can use to get the post meta for display to your customers. In order to see that info, though, we are going to have to modify a WooCommerce template. WooCommerce makes this very easy, and your theme may already have some built-in custom WooCommerce templates. I won’t go into all those details, but the best way to do this is to have a child theme setup so that you can override any templates in your parent theme without them getting wiped out any time you update the parent theme. In your child theme you create a folder called “woocommerce” (without the quote), and then follow the directory structure and names of the WooCommerce templates you want to customize.
Click Here for the WooThemes docs page on overriding templates with your theme.
A good place to put our ISBN field might be in the meta.php template that WooCommerce uses to show any product meta. To make it look like the SKU field, and appear right below SKU, I added the code below the code section where WooCommerce shows SKU with a bit of custom formatting like this:
<span class="sku_wrapper isbn_wrapper">ISBN: <span class="isbn"><?php echo esc_html( get_post_meta(get_the_ID(), 'isbn', true) ); ?><span></span>
I put it in a span using the sku_wrapper class Woocommerce sets up, but added an isbn_wrapper class to it, and class isbn around the actual isbn product meta, so you can style it better. Note the ‘isbn’ as the second parameter of the get_post_meta function call. Modify that to match your own custom field key, leaving the single quotes around it. For best security practices when outputting anything we get from the database, we wrap the output in the esc_html function.
Make your custom meta.php file and put it inside the woocommerce>single-product folder of your theme, and it will work. Modify the ISBN: part of the text to make your field label say whatever you want, and modify the class names and meta key as desired to match your own custom field. Then all you need to do is create the custom field for your products, and type in the info.
If you are using my WooCommerce Wholesale Ordering extension, and only want to show the info to Wholesale Customers, my plugin has a couple of conditional functions you can use to check if the current user is a wholesale customer and if wholesale prices/info should be displayed. Check out my post on conditionally displaying info to Wholesale Customers.
<?php /** * Single Product Meta * * @author WooThemes * @package WooCommerce/Templates * @version 1.6.4 */ if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly } global $post, $product; $cat_count = sizeof( get_the_terms( $post->ID, 'product_cat' ) ); $tag_count = sizeof( get_the_terms( $post->ID, 'product_tag' ) ); ?> <div class="product_meta"> <?php do_action( 'woocommerce_product_meta_start' ); ?> <?php if ( wc_product_sku_enabled() && ( $product->get_sku() || $product->is_type( 'variable' ) ) ) : ?> <span class="sku_wrapper"><?php _e( 'SKU:', 'woocommerce' ); ?> <span class="sku" itemprop="sku"><?php echo ( $sku = $product->get_sku() ) ? $sku : __( 'N/A', 'woocommerce' ); ?></span>.</span> <?php endif; ?> <span class="sku_wrapper isbn_wrapper">ISBN: <span class="isbn"><?php echo esc_html( get_post_meta(get_the_ID(), 'isbn', true) ); ?><span></span> <?php echo $product->get_categories( ', ', '<span class="posted_in">' . _n( 'Category:', 'Categories:', $cat_count, 'woocommerce' ) . ' ', '.</span>' ); ?> <?php echo $product->get_tags( ', ', '<span class="tagged_as">' . _n( 'Tag:', 'Tags:', $tag_count, 'woocommerce' ) . ' ', '.</span>' ); ?> <?php do_action( 'woocommerce_product_meta_end' ); ?> </div>
This post has come the closest to what I am trying to achieve but since I am a novice in Woocommerce Storefront – this may not be what I need to do.
I am setting up my storefront. I have a Product: Pillows – with various sizes 16×16, 18x 18 and so on.
My manufacturer of these products has an ID# for each type of pillow size. I would like to enter a product code or ID# next to each item/size. This is not something that needs to show up on the product page for customers to see but I would like to see it when I get the notification of an order so I can forward it to my manufacturer. Is this possible?
I get Custom Fields, Variations and Attributes mixed up. Can you guide me and tell me how I can create this field to fill in when I add a product. Hope this is not confusing.
Hi Lisa,
You would normally use the SKU field of your products in WooCommerce to save those types of product ID numbers.
If you have one type of pillow that has several sizes, you would normally create that as a Variable product, and set the attribute to “Size”, and then create a variation for each size (after you have defined all the sizes in the “Size” attribute). Each variation in a variable product can have its own SKU #, so nothing custom is required to enter and save a product ID for each size.
If you are still trying to get a grasp on how WooCommerce works, and setting up the various types of products, please check their online documentation. For example, here is a link to their documentation on Variable products:
http://docs.woothemes.com/document/variable-product/
Very helpful post, Stephen! I’m wanting to show the minimum qty for the selected variation in the meta box on product pages (exactly where you are putting the isbn in this article) but am having a hard time tapping the field that’s created in your User Role minimums plugin. (my key is ‘_wholesale_min’)
It would be really nice to have this crucial ordering detail automatically update when a certain variation is selected by the end-user (much like the SKU updates based on the selected variation) Can you provide any guidance?
Daniel,
To automatically change info based on a selected variation on a variable product page, it’s a bit more complex as that data needs to be printed out into a hidden “data” field when the product page is first displayed, with all the info for each variation. Then, you need to use javascript to change what is shown whenever a new variation is selected.
However, no need to try to do that yourself as my plugin will already show the min or max qty for each selected variation if you have enabled the options to do so.
Steven,
I’ve spent more time in the code and am now seeing that hidden ‘data’ fields will have to be used, along with some javascript. I haven’t found a way to use your plugin to show the min-max quantity, as I want this information to display in the metabox, and to display accurately, when someone chooses a particular variant. In addition, your plugin just shows the lowest minimum of all variations. It should just show the true minimum for whichever variation is selected. This isn’t very helpful from and end-user’s perspective.
“In addition, your plugin just shows the lowest minimum of all variations. It should just show the true minimum for whichever variation is selected. This isn’t very helpful from and end-user’s perspective.”
This is not true. There are two sets of minimums, one for the parent variable product, and one for each variation. If you set different minimums for each variation, then my plugin shows the minimum for that variation above the add-to-cart button when that minimum is selected (and gets hidden, just like the price, when no variation is selected). The minimums my plugin shows at the top are minimums you set for the parent variable product (which won’t change).
Perhaps there is a theme issue, as it’s not clear what the “meta box” area is you are talking about. This is getting off topic, though, and should be in the support forum or via private email if it’s a theme issue or some other type of conflict.
I’m in Hawaii on vacation with my family and get back home very late tomorrow evening. I can help you with some more in-depth personal support after the 1st, if needed.
Sorry for the confusion, Stephen. I must have misunderstood. The line in the admin interface to turn on Show Shop Loop Min Qty states, “(will not show individual variation minimums).” I may also have a theme issue, so I will take some time to investigate. Thanks very much for the excellent plugin. The polish you have given your plugin really shows. Glad to be a customer, and happy New Year!
Hi, Stephen.
Thanks for sharing this code. It was very helpful! I have a simple question: how is it possible to display the custom value as an hyperlink? I created a key named “Website”, and the custom field value is a variable URL.
I’m sure there must be a very easy trick, but I couldn’t find the solution by myself. 🙁
Best.
Sorry, tried to respond, but I an currently in China with limited Internet and only my cell phone, and it keeps trying to convert my code for a link into an actual link.
Just create the html for the hyperlink, and use the custom postmeta as the URL, echo the value inside the quotes of the href value. Be sure to use the esc_url function on the url for safety.
Something like this to get your link url:
Then create a link with the regular html a tag and echo the link variable inside the href quotes. If you still don’t understand, you will need to remind me after I return on July 1, and I can post the full code. Can’t seem to do it with my phone right now.
Thanks for a very useful tutorial, Stephen! I tried some others, but this was explained good enough for a novice like me.
I have one question:
My custom field does show on the front end. But only if my SKU is filled too. I presume it has something to do with this code line:
get_sku() || $product->is_type( ‘variable’ ) ) ) : ?>
Should i replace the “sku”-part with my custom field label here too?
BR
Ah, copypasting the code didn’t quite work, what i mean is the If-sentence above the sku-wrapper.
Only that first part between the if and the endif statements wouldn’t show if you don’t have sku enabled, meaning this section:
get_sku() || $product->is_type( 'variable' ) ) ) : ?> get_sku() ) ? $sku : __( 'N/A', 'woocommerce' ); ?>.
The other parts below the endif line should still show unless your theme, or some other function, has removed the hook that calls the meta.php template. The line that I added was below that and it shows the isbn pretend meta that I used for the example. You would need to replace the ‘isbn’ within the get_post_meta function call parameters with whatever the meta key is for your custom post meta (and change the ISBN: label text to whatever you want (you can also change the span class names for your own custom styling).
ISBN:
Perfect. I added my custom meta line after the endif, and it works. Thanks!