In this WooCommerce tutorial I will be showing you how to add a custom field to a WooCommerce product.
I will go through adding a custom field on the general tab section of the WooCommerce edit product page underneath the price fields where you can enter a value in to be saved, go through saving and validating the data and then displaying the data that was saved on the product page on the front-end of your shop.
As an example I will be showing you how to add a Recommended Retail Price (RRP) to a WooCommerce product and display this on the front-end, so lets get starting.
First step would be to define the field on the edit product page, luckily WooCommerce makes this very easy due to the large array of hooks available for use as well as the built in functions to generate HTML input fields. Place the following code inside your theme’s functions.php file to define a new text field on the product edit page.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action( 'woocommerce_product_options_pricing', 'wc_rrp_product_field' ); | |
function wc_rrp_product_field() { | |
woocommerce_wp_text_input( array( 'id' => 'rrp_price', 'class' => 'wc_input_price short', 'label' => __( 'RRP', 'woocommerce' ) . ' (' . get_woocommerce_currency_symbol() . ')' ) ); | |
} |
You will now have a field on your product edit page called RRP, however if you enter something into the field and save the product, the data will not be save as we need to actually still save the data when the product gets updated, to save it and also check that a numeric value is entered add the following code to your theme’s functions.php file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action( 'save_post', 'wc_rrp_save_product' ); | |
function wc_rrp_save_product( $product_id ) { | |
// If this is a auto save do nothing, we only save when update button is clicked | |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) | |
return; | |
if ( isset( $_POST['rrp_price'] ) ) { | |
if ( is_numeric( $_POST['rrp_price'] ) ) | |
update_post_meta( $product_id, 'rrp_price', $_POST['rrp_price'] ); | |
} else delete_post_meta( $product_id, 'rrp_price' ); | |
} |
Now when you enter a value in the RRP field and update the product the field should be saved and loaded in the RRP field you defined when the page loads. The final step would be to actually display the value saved on the product page on the front-end for customers to view, to do this add the following code to your theme’s functions.php file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action( 'woocommerce_single_product_summary', 'wc_rrp_show', 5 ); | |
function wc_rrp_show() { | |
global $product; | |
// Do not show this on variable products | |
if ( $product->product_type <> 'variable' ) { | |
$rrp = get_post_meta( $product->id, 'rrp_price', true ); | |
echo '<div class="woocommerce_msrp">'; | |
_e( 'RRP: ', 'woocommerce' ); | |
echo '<span class="woocommerce-rrp-price">' . woocommerce_price( $rrp ) . '</span>'; | |
echo '</div>'; | |
} | |
} | |
// Optional: To show on archive pages | |
add_action( 'woocommerce_after_shop_loop_item_title', 'wc_rrp_show' ); |
Your end result will look like the image below when you add or edit a product
And will look like this on the front-end when a customer views the product
Just a note that this tutorial does not cover adding custom fields to variable product, that process is covered in this tutorial by my fellow team member at WooThemes, Remi Corson.
This is not working for me.
how can i Add Checkbox instead of text field?
i want to add checkbox name “HOT”. if the admin check it, so a tag should add on the product image with text HOT. same as SALE.
Fzi
Hi everyone,
There is someone could help me to give me a response about my question above and how i could add a custom product field (like exactly RRP field) but with no $ currency but another currency like Kg for example.
Thanks for everyone who give me a help
Great tutorial! I managed to get the extra price field working (Canadian price) right away. Two questions:
1. How can I choose to display the Canadian price (my new field) INSTEAD of the US price if my visitor is from Canada ( I assume I could have visitor self select country, or pull it from some kind of Geolocation DB)
2. How can I add a Canadian stock field in the Inventory tab? I was unable to find the hook for this tab (very new to WP).
Thanks a lot!
Lakshmi
Hi everyone,
Thanks Gerhard for this script, exactly what I was looking for 🙂
Just a quick question, if I wanted to add 5 additional text fields to my Woocommerce product, would your script work if I just add it 5 times?
Best wishes
R
Hi Richard, unfortunately that will not work, you will have to repeat the part inside the functions for each field, ie to add 3 fields you will repeat the
woocommerce_wp_text_input( array( 'id' => 'rrp_price', 'class' => 'wc_input_price short', 'label' => __( 'RRP', 'woocommerce' ) . ' (' . get_woocommerce_currency_symbol() . ')' ) );
part underneath each other 3 times and just change the id value for each field to be unique, same goes for the functionality in the other functions, you will repeat it for each field in that same function.hello,
How i can add sorting order to field means if you want my field in top of the general tab ?
Hi! Great tutorial, but stumbled upon a problem when i was trying to show this value in a column in the cart-page. Tried several ways to get both custom fields and acf, but doesn’t seem like woocommerce-loop finds my custom fields.. Anyone knows how to print custom field to cart?
Best regards, Jesper
Hi Jesper
You would need the product ID and then simply do a get_post_meta with the ID, because it is the cart page the global post or product variables dont represent the product anymore but the cart page instead, so you will have to loop through the cart to look up the product ids of the products in the cart, WC()->cart->cart_contents is your friend 🙂
Hi Gerhard
Very nice tut.
I have a question, how from this tutorial do I change the field to a text field instead of a numeric field?
I need to be able to type in a String and not a price 🙂
And if you know a way to add custom Date fields aswell it would be awesome!! 😀
Thanks a lot!
I am looking for a solution to add extra custom fields with number fields to each product and then to be able to show a sum of those custom fields in the shopping cart, would it be possible using your custom fields here?
Hi there,
Trying to add this in to my site, but nothing seems to change after adding it to my functions.php?
Any ideas as to why nothing is changing?
Don’t worry, I’ve got it working now. For some reason it wasn’t updating the functions.php correctly so I did it manually through FTP and now it works perfectly. Thanks!
Great tutorial. I’m looking to add a custom field that is alphanumeric instead of a price. What do I need to change to make this happen?
Hi Gerhard. This works a charm for a single add product. However the field doesn’t appear if you are importing 100’s of products using wp-all import in the woocommerce section of wp all import. Any ideas on how I can get it to appear there as well. If you could that’d be superfantastic 🙂
Hi Gerhard,
Thanks for the great tutorial. I’d love to do something similar with the date.
I notice that there’s a feature to sort by ‘newness’ on the archive page. I’d like to show the date each product was ‘published’ onto the site on the archive page and the item description. How would I go about doing this?
Thanks,
Mike
Hi ,
I just want to add product specifications via table in woo-commerce and want to update the table fields via CSV file. So can you please help me put in this problem. Thanks in advance.
I just find my solution 🙂
Thanks
How to create multiple variable products with values, and add the variable should be selected values in variations section, using custom code, could you please help me in this.
Thanks in advance
Talla
Hi,
Great solution for my question.
But is there also a way to use the default WP editor for the textarea field?
I have added a custom tab and want to display additional pricining info in just a plain text way.
But with text area I have to teach my client to use html codes for markup.
any suggestion?
Marijn
Hi Gerhard,
How do I change this to enter a text string value. For example I want to add an extra field called “Origin” which can accept any text string value. Tried messing around with your code to make it happen but not very good at php and its clearly wanting numerical input :(.
Hi Gerhard,
thanks for this work.
If I go to save the product, the field for the RRP price is empty. Where is the problem?
Can you help me/ fix this?
Thanks nic
love this code, But i have a problem, when i type in the rrp, it is showing correctly, But im getting the number 1 value before the rrp text and price. basically it looks like this:
Product name:
1 RRP: 39.99
Is there anyway to remove the 1 before the rrp? some of the products are just showing for rrp and no other price are added.
Thanks Gerhard, I am trying to solve a problem on my site. I am selling a sport event in package form.
I have made a variabel product with rooms. A person can buy (for example) 4 x tripple rooms. That adds up to 12 divers. Now I would like to get there names+ age + number of events (number of events adds a cost to the total) before they check out. So if the customer buy 2 tripple room there should come up a list of 6 fields for the names of each room and if they are coach or diver. If five of them are divers they should fill in more information. Is this possible?
Hi there,
A couple of questions.
1) How can I get the RRP to display on product variations? I can see in the code there is a section for not displaying it on product variations, what do I need to edit to display it in the variants?
2) I am looking to do this sort of thing but with an SKU number for every product, how would I go about doing this and changing the code from RRP to SKU so that this can be displayed on each page because obviously this shouldn’t really be in the woocommerce_product_options_pricing hook?
Any help would be greatly appreciated! 🙂
is there any support left on this? i need to know problem and how to fix my awaiting moderation.
Hi, i have 30 products on my website and to update the prices i use the csv importer plugin. Can I update the rrp via this plugin or would i have to do it manually.
Thanks
Camron
Hi Camron, you can do it via the CSV importer plugin, place it under the heading meta:rrp_price
You are my new hero. I spent so many hours trying to work out how to do this, and trying to find a plugin that would do it, but nothing worked. I tried this and it worked first try with no altering needed. Thank you!
Hi,
this is a great tutorial and it helped me a lot. My problem is, that I would like to display custom fields inside cart. Any kind of help / advice would be great.
Thanks
I love the work you are doing! I was able to get the msrp field to show up on the product options under the sale price. Only thing is it will not reflect this on the live product view. It shows the msrp but always seems to show “0.00” for value. Any ideas? It could be the theme I am using.
I found the problem with the msrp reflecting on the product page. Just had to change all rrp settings to msrp and I had skipped one. Since this worked I thought I would add another field. Exact layout as the rrp which I used to do my msrp this time for a UPC field, but this time it gave me an error, so I deleted all the added code altogether and now I am getting the same error message. Do you have any idea what it may be? Your code to add fields worked great, and it was not until I decided to try something else that it threw my whole site off.
Error Message: Parse error: syntax error, unexpected ‘)’, expecting ‘&’ or variable (T_VARIABLE) in /home1/kathy/public_html/amoralighting/wp-content/themes/bridge/functions.php on line 2138
Please Help!
Nevermind I figured it out. I was forgetting $ on a few variables. Problem solved!
how to add simple text field
name : view tex
Name : Text input
This is really great i have a question what if the rrp done have a value. how to set it to if there is no value input it there is no label view on the page. what i mean is on the back end if RRP fields if there is no value then the view is dont also have a label or text ? thanks
Hey Gerhard,
I have attempted this for hours with no success. I love your tweak, but am looking to remove the need for this new field to be a currency. I have changed the “RRP” text but cannot figure out how to make this custom field to allow text strings.
Does this line have something to do with it?
add_action( ‘woocommerce_product_options_pricing’, ‘wc_rrp_product_field’ );
To put it simply, I want this field to say “Package of [custom field] products”, not RRP: $[custom field]
hi Gerhard thanks for shearing good tutorial,
Gerhard in my woowcamerc themes in seller submit product page I want add a new field for demo of product and save to database and read and show on single product page,
I want use you code but i don’t know about show this field on seller submit product page.
please Help me,
Thanks
Vahid
Gerhard,
Any update on adding this to variation products? – A tutorial would be nice! 😉
Hi, great code, however One thing that seems to have caused issue, is when I do bulk edits, it removes the custom field data from all products.
Is this because the custom field does not appear in the bulk action > edit options and there for is removed / erased ?
Any idea to prevent this from happening as it really isn’t viable if bulk edits for lots of products is not an option with custom fields
This is a fantastic article and like most, I got it up and running with no issues.
The only one thing I can not seem to find is now that this is functioning is there a way to make the custom product field “filterable”
As an example, I added a field called “Gender” with the choice of Male/Female.
Using many of the filter plugins out there, it would be nice to expose this filed.
It appears that only the “Product Categories” or “Product Tags” are meant to be filtered. Can you touch on how to incorporate that or is that beyond the scope of this article and is the custom product field only intended to be displayed?
Thanks again for an awesome demo!
OK, so doing some more research (and this tutorial has been the catalyst) it looks like I may have been using the wrong verbiage.
Please correct me if I am wrong but this post is only meant to add “descriptive” information on the webpage,
Is it all possible to take these descriptive fields and also make them part of the taxonomy?
In other words can I have a user select the Gender of “Male” and then register that selection as a taxonomy? Would that work?
I’m trying to comprehend this page:
https://codex.wordpress.org/Function_Reference/register_taxonomy
Hoping this isn’t to complicated.
Hey Gerhard,
great tutorial.
I need exactly what you explained.
But I do not want to show a price.
I want to show the shipping class.
And additionally I want to show a product attribute.
Most important for me is the shipping class.
How should your code has to be changed for that?
Thanks in advance
Philipp
Thanks for this info mate. You saved me a packet in paid plugins
0 Pingbacks
Comment navigation
Comment navigation
Categories
Top Posts & Pages
Instagram
No Instagram images were found.
Get Email Notifications