Learn how to add custom fields to settings in WooCommerce meta boxes — and give back to WooCommerce when you find some code that can be improved.
There are times when you need to “mess” with meta boxes in the WooCommerce back end. Meta boxes are the draggable boxes you find in the product, order, and coupon edit pages.
WooCommerce meta boxes contain the current custom post type settings for your products. Think of the “regular price” — that’s a custom field you can find in the “Product Data” meta box in the “product” custom post type edit screen.
Today, we’ll study the woocommerce_wp_text_input
function. It provides a neat way to generate an additional text input in case you need additional settings for your products.
For example, suppose you have a “regular price” and a “sale price” but you also want to have a “list price” or “msrp” — a manufacturer’s suggested retail price (MSRP). There’s two ways you could do this:
- NOT the best way: You could write the MSRP into the HTML description field for every product yourself, but this locks the price as static text in the content where it really doesn’t belong. You might lose track of it or accidentally write over it, and you can’t reuse it on other products without doing the same thing.
- The BEST way: Instead, you can use
woocommerce_wp_text_input
to quickly generate an input field on the back end with the right classes, name and id for the product. That means you can store the additional price in the database once the current post is saved and use it on other products.
As usual, we’ll study the WooCommerce core function code for woocommerce_wp_text_input
in this tutorial, see where and why it’s used, and finally, we’ll cover a quick case study. Enjoy!