WooCommerce has multiple methods of storing product data. All standard product fields include name, price, categories, tags, and measurements. There is also the option to add product features. But what if you need to store more product fields? What if you need something more customizable than product variants like checkboxes or text boxes? This is where WooCommerce custom fields come in.
Today's tutorial will show youhow to add custom WooCommerce product fields. Whether you want to add rich properties to a WooCommerce product page or present alternative product variants at the click of a button, creating custom fields in WooCommerce only takes a few minutes. You can then customize every feature of your WooCommerce dashboard and quickly add site information that would normally require manual data entry.
Why do we need to add WooCommerce custom product fields?
So what are the main benefits of using custom product fields? Here is the short list:
- Product custom fields are used to add more product information to items such as PCs, where technical data such as RAM and processor power are displayed.
- Create messages that can be quickly changed into something else.
- For example, an additional video is an example of a unique media element that cannot be introduced using ordinary WooCommerce features.
- Specify dates and times for a product countdown, auction, or release date.
- Customers can browse other product alternatives in addition to variations.
- Diagrams detailing how a product is used.
- Add-on offers provide the consumer with multiple upsells that complement the existing purchase. Brush attachments for electric toothbrushes or various charms for a bracelet are two examples.
- You want to include unusual or additional data on a product page that isn't supported by WooCommerce or easier to fill out with custom fields.
Common types of custom product fields on WooCommerce
When individuals mention WooCommerce custom fields, they usually imply one of two things:
- Including further product information: This is the most common use of custom fields. Custom fields are used in WordPress to add additional fields to a post, page or item. These are used to store and display more information, e.g. B. additional text fields, data, photos, etc.
- Sell more product possibilities instead of using variations: This includes additional elements such as drop-down lists, checkboxes, radio buttons, text boxes, etc. that the buyer must fill out before adding the goods to their shopping cart. Additional product options are not technically custom fields at all; They are better known as product add-ons! However, many people refer to these possibilities as custom fields.
How to add custom product fields on WooCommerce product pages
Method 1: Coding
As you can see in the example below, we will show you how to add custom fields to theedit productbook page. Additionally, we will show you how to modify the functions.php file in the theme folder to add these fields.
Set up custom product fields
The first step is to connect to the woocommerce_product_options_general_product_data variable. The function associated with this hook is responsible for displaying the new fields. The custom field values are stored via a second hook, woocommerce_process_product_meta. Both activities are performed in the following code:
// The code to display the WooCommerce custom product fieldsadd_action( 'woocommerce_product_options_general_product_data', 'woocommerce_product_custom_fields' ); // The code below saves WooCommerce custom product fieldsadd_action( 'woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save' );
function woocommerce_product_custom_fields () { global $woocommerce, $post;echo '<div class=" product_custom_field ">';// this function has the logic to create custom fields// this function contains an input text field, a text area and a number field echo ' </div>';}
To add the fields, we often use WooCommerce's built-in functions, such as:
woocommerce_wp_text_input
Package: WooCommerce\Admin\Functions
Is located: include/admin/wc-meta-box-functions.php
woocommerce_wp_textarea_input
Package: WooCommerce\Admin\Functions
Is located: include/admin/wc-meta-box-functions.php
// Custom product text field woocommerce_wp_text_input( array( 'id' => '_custom_product_text_field', 'label' => __( 'My Text Field', 'woocommerce' ), 'placeholder' => 'Custom product text field', ' desc_tip' => 'true' ) );
Instead of displaying the usual field description, it uses desc_tip to display those awesome little bubbles to the right of the field. This property applies to all field types.
// Custom product number field woocommerce_wp_text_input( array( 'id' => '_custom_product_number_field', 'placeholder' => 'Custom product number field', 'label' => __('Custom product number field', 'woocommerce'), ' type' => ' number', 'custom_attributes' => array( 'step' => 'any', 'min' => '0' ) ));
The default value of the step in the above code is one with min set to zero. Essentially this means that we expect a good deal here (at least greater than zero). The text box should be created with the following code:
// Custom Product Textarea woocommerce_wp_textarea_input( array( 'id' => '_custom_product_textarea', 'placeholder' => 'Custom Product Textarea', 'label' => __('Custom Product Textarea', 'woocommerce') ) );
Here is the full code for custom input fields that you need to put in the theme folder's functions.php file:
// show fieldsadd_action('woocommerce_product_options_general_product_data', 'woocommerce_product_custom_fields');// save fieldsadd_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');function woocommerce_product_custom_fields(){ global $woocommerce, $post; echo '<div class="product_custom_field">'; // Custom product text field woocommerce_wp_text_input( array( 'id' => '_custom_product_text_field', 'placeholder' => 'Custom product text field', 'label' => __('Custom product text field', 'woocommerce'), 'desc_tip' => ' true' ) ); //Custom product number field woocommerce_wp_text_input( array( 'id' => '_custom_product_number_field', 'placeholder' => 'Custom product number field', 'label' => __('Custom product number field', 'woocommerce'), 'type' => ' number', 'custom_attributes' => array( 'step' => 'any', 'min' => '0' ) ) ); //Custom product text area woocommerce_wp_textarea_input( array( 'id' => '_custom_product_textarea', 'placeholder' => 'Custom product text area', 'label' => __('Custom product text area', 'woocommerce') ) ); echo '</div>';}
Save your fields in the database
After setting up the custom product fields, you need another feature to save the data when the user clicks the refresh or publish button. We call this function woocommerce_product_custom_fields_save. This function is linked to the woocommerce_process_product_meta hook. This method is pretty simple; it first determines whether the field is empty. If not, update_post_meta() is used to generate a post meta. To protect the data we used esc_attr() and esc_html(). Here is the code to store all the values of the fields:
function woocommerce_product_custom_fields_save($post_id){ // Benutzerdefiniertes Produkttextfeld $woocommerce_custom_product_text_field = $_POST['_custom_product_text_field']; if (!empty($woocommerce_custom_product_text_field)) update_post_meta($post_id, '_custom_product_text_field', esc_attr($woocommerce_custom_product_text_field));// Benutzerdefiniertes Produktnummernfeld $woocommerce_custom_product_number_field = $_POST['_custom_product_number_field']; if (!empty($woocommerce_custom_product_number_field)) update_post_meta($post_id, '_custom_product_number_field', esc_attr($woocommerce_custom_product_number_field));// Custom Product Textarea Field $woocommerce_custom_procut_textarea = $_POST['_custom_product_textarea']; if (!empty($woocommerce_custom_procut_textarea)) update_post_meta($post_id, '_custom_product_textarea', esc_html($woocommerce_custom_procut_textarea));}
Get your data back from the database
We display the values on the frontend after the fields are created and their values are saved. In this case, working with custom WooCommerce templates would be the ideal option. We use the well-known get_post_meta() method to retrieve this information.
<?php while (have_posts()) : the_post(); ?><?php wc_get_template_part('content', 'single-product'); ?><?php// Display the value of the custom product text field echo get_post_meta($post->ID, '_custom_product_text_field', true);// Display the value of the custom product number field echo get_post_meta(get_the_ID(), '_custom_product_number_field ', true) ;// Display the value of the custom product textarea echo get_post_meta(get_the_ID(), '_custom_product_textarea', true); ?><?php endwhile; // end of loop. ?>
Add custom product fields on the product page
Some business owners don't know that they can add custom fields to the product data field. This provides a unique way to display information that doesn't fit into the standard WooCommerce UX. The good news is that these custom fields can be easily added from the backend. These changes are then reflected in the frontend as custom fields on product pages, cart pages, and other places. These custom fields can also appear on order status pages. To explain the concept of WooCommerce custom product fields, let's show you how to add a new custom field to the Product Data section of a WooCommerce product page:
function woocommerce_product_custom_fields(){ $args = array( 'id' => 'woocommerce_custom_fields', 'label' => __('Add WooCommerce Custom Fields', 'cwoa'), ); woocommerce_wp_text_input($args);} add_action('woocommerce_product_options_general_product_data', 'woocommerce_product_custom_fields');
This is what the snippet would look like in the frontend. As you can see, the custom field has the same label as the $args array.
Save your changes
To add custom fields to product pages, use the following code snippet. The most important feature of the snippet is that it uses traditional WooCommerce methods and actions.
Funktion save_woocommerce_product_custom_fields($post_id){ $product = wc_get_product($post_id); $custom_fields_woocommerce_title = isset($_POST['woocommerce_custom_fields']) ? $_POST['woocommerce_custom_fields'] : ''; $product->update_meta_data('woocommerce_custom_fields', sanitize_text_field($custom_fields_woocommerce_title)); $product->save();}add_action('woocommerce_process_product_meta', 'save_woocommerce_product_custom_fields');
View your new custom fields
The custom fields are shown in the following excerpt. The procedure is simple: it validates the value of the custom field and confirms that it has a value. If the case is valid, the value is displayed as the title of the field.
Funktion woocommerce_custom_fields_display(){ global $post; $product = wc_get_product($post->ID); $custom_fields_woocommerce_title = $product->get_meta('woocommerce_custom_fields'); if ($custom_fields_woocommerce_title) { printf( '<div><label>%s</label><input type="text" id="woocommerce_product_custom_fields_title" name="woocommerce_product_custom_fields_title" value=""></div>', esc_html ($custom_fields_woocommerce_title) ); }}add_action('woocommerce_before_add_to_cart_button', 'woocommerce_custom_fields_display');
The custom field will appear on the product page as shown in the image below. The field title is “WooCommerce Custom Product Fields Title”, which is the ID value in the excerpt.
Method 2: Using a third-party plugin
For example, in this approach, we create a fictional computer store that sells laptops. Entering the computer specifications for each new notebook would take a long time. So we're adding fields to select these properties based on the laptops we offer.
Install the free one firstAdvanced custom fieldsPlugin on your WordPress site to add custom fields to WooCommerce goods. Then find out how here.
Step 1: Create a new field group
Navigate to the new oneCustom Fieldstab in your WordPress dashboard. Then nextfield groups, clickAdd new.
Give a field group a name. A field group is a set of fields for a single product or part of your website, e.g. B. A list of attributes to be displayed on all laptop product pages.
Step 2: Add your custom fields
To add a custom field to this group, select itadd fieldTaste.
In the next section you can add:
- field label– primarily for your use.
- Feldname– These are the sections you will use to create your custom code or shortcode in our instance. You can leave this as the default value of the plugin.
- Field type– the format you create for this field. Date pickers, checkboxes, dropdowns and radio buttons are among the options available. You may also need to add some guidance for yourself or the developer - as well as a statement of whether or not this field is required.
If you are dealing with a field that contains numerous choices (e.g. radio buttons), next add your Choices field. These are the choices you want to showcase in the backend of your product page, e.g. B. different core processors.
The other options, such as Items such as the default value, the layout, and whether or not to show the value or label are usually optional.
When you are finished with a field or group, clickPublishorTo updatebutton to save it.
press theadd fieldbutton again to add additional fields to your group.
Step 3: Customize additional field group settings
When you're done with the field group, move on to theLocationModule. This is where you tell WordPress where you want the field group to appear on your dashboard. The product should be set asPost-Typ. Alternatively, if you want to know more about it, you can select a product category.
Make sure thefield groupis available inIdeas. This way users can show it on these product pages and maybe show your customers the field options. Your personal choices determine the style. The same applies to the position, the placement of the labels and the arrangement of the instructions. It's entirely up to you and the way you organize your backend. If in doubt, leave them at the default settings. If you have numerous field groups that appear on product pages, use the order number. This determines the order in which certain field groupings are displayed. You can also add a description to your screen and hide certain features.
Step 4: Add more data to the products
Next, go to the editor for the product you want to add custom fields to. If all your settings are correct, you should see the custom fields belowproduct dataCrate. For example, if you were to create a new laptop product page, you would select some additional items as in the image below.
Step 5: Show custom field in frontend
You need to display the data from your custom fields on the front-end single product page to complete the process. You can put this information anywhere, but the long or short description of the product is an excellent place to start. Use the[acf-Feld=""]
Shortcode to display field data in frontend. Add the between the quotation marksFeldname:
The field name for each field can be found in the field group editing interface:
To finish, select thePublishorTo updateButton. When customizing your product in the frontend you will see your custom fields with the shortcodes:
Diploma
The benefit of using Advanced Custom Fields over other field builder plugins is that you can insert your new fields anywhere on your site, including product pages, items, and cart modules, allowing you to customize your dashboard and speed up the content creation process. It's a fantastic alternative to boost your creativity and make the time-consuming task of creating product pages a little easier.
We hope this tutorial will help you understandhow to add custom WooCommerce product fieldsand save it to the WordPress backend without any special technical knowledge (plus a quick look at how to do it programmatically). Follow the instructions in this guide and implement them on your website. It's an excellent non-technical way of storing and displaying additional product information in WooCommerce.
FAQs
How do I use advanced custom fields in WooCommerce? ›
- Visit Plugins > Add New.
- Search for “Advanced Product Fields for WooCommerce”
- Activate the plugin from your Plugins page.
- Click on the new menu item WooCommerce > Product Fields and create your first Custom Field Group.
- Step 1: Define an Array of Fields on Checkout Page. ...
- Step 2: Add Custom Fields to WooCommerce Checkout Page. ...
- Step 3: Concatenate Fields as per Requirement. ...
- Step 4: Display Custom Fields on Order Page. ...
- Step 5: Display Fields on Account Page.
- WordPress Dashboard > WooCommerce > Bulk Edit Products.
- WordPress Dashboard > Plugins > Installed Plugins > Bulk Edit Products, Prices & Attributes for WooCommerce > Bulk Edit Products.
Log into your WordPress site and go to the WooCommerce->Settings page. Select the “Custom Fields” tab and click on the “Add Custom Field” button. Input the name of your custom field and select the type of field you want to add (e.g. text, select, checkbox, etc.).
How do I display custom field values in WordPress? ›- Open the single. php file or page. ...
- Find the_content function so you can list your custom field data after the actual content of the post or page.
- Use the get_post_meta function to fetch custom field values using their meta key then list them using PHP echo.
Simply create a new post or edit an existing one. Go to the custom fields meta box and select your custom field from the drop-down menu and enter its value. Click on the 'Add Custom Field' button to save your changes and then publish or update your post.
How do advanced custom fields work? ›Link to heading#Welcome. Advanced Custom Fields is a WordPress plugin which allows you to add extra content fields to your WordPress edit screens. These extra content fields are more commonly referred to as Custom Fields and can allow you to build websites faster and educate your clients quicker.
What is advanced custom field? ›Advanced Custom Fields Plugin
The free version allows users to quickly and easily add 30+ fields all over your WordPress dashboard — including to posts, users, taxonomy terms, media, comments and custom options pages — and display the custom field values in any theme template file.
Once you've installed and activated the free version of Advanced Custom Fields from WordPress.org, go to Custom Fields > Add New to create your first Field Group. As the name suggests, a “Field Group” is a group of one or more custom fields that are displayed together in your WordPress dashboard.
How do you add custom fields to WooCommerce products and subsequently through the order cycle? ›- Step 1: Add Data in a Custom Session, on 'Add to Cart' Button Click. ...
- Step 2: Add Custom Data in WooCommerce Session. ...
- Step 3: Extract Custom Data from WooCommerce Session and Insert it into Cart Object. ...
- Step 4: Display User Custom Data on Cart and Checkout page. ...
- Step 5: Add Custom Data as Metadata to the Order Items.
How to custom checkout fields in WooCommerce without plugin? ›
There are a few ways that you can change the checkout page in WooCommerce without using a plugin. One way is to use the Customizer. Go to WooCommerce > Settings > Checkout and select the tab for the page you want to customize. From here, you can add, remove, or rearrange fields.
How do I add more than 50 variations in WooCommerce? ›- Install and activate Markup by Attribute for WooCommerce on your WordPress site. The plugin's main function is to markup the price on certain attributes. ...
- Go to WooCommerce > Settings > Products > Markup by Attribute.
- Set the Variation Max to the limit you want.
- Step 1: Install & activate the plugin. ...
- Step 2: Filter variable products. ...
- Step 3: Select variable products. ...
- Step 4: Add bulk variations. ...
- Product Variations Table for WooCommerce. ...
- YITH WooCommerce Color and Label Variations. ...
- The Ultimate Bulk Variations Matrix Plugin.
Adding multiple attributes is easy to do. Simply go to the “Attributes” tab on the product page and click “Add New Attribute.” From there, you can enter the name of the attribute, such as “Size” or “Color.” Then, add the values for that attribute.
How do I add a custom field to a field catalog? ›Go to 'Sales and Distribution > Basic Functions > Pricing > Pricing Control' and execute 'Define Condition Tables'. Select 'Conditions: Allowed fields' and enter 'ZZPSTYV' as a new entry. 5. Note: When you create the condition table Annn, you can now use the field ZZPSTYV as a key field.
How do I add a custom field to a template? ›- Go to the post editor screen and check “Custom Fields.”
- Find the Custom Fields box as part of your post editing. ...
- Add the key, which in this case is “Type of exercise.” We will use this box to add specific details to the post. ...
- Add the value. ...
- Save the post.
For this use T-code SE51. Give the Program name and search for the screen number, click Change Button. Then, in the module pool program - Click the Layout button and include your field inside manually and provide the properties for that field.
How do I get advanced custom field value in WordPress? ›To retrieve a field value as a variable, use the get_field() function. This is the most versatile function which will always return a value for any type of field. To display a field, use the the_field() in a similar fashion.
How to create advanced custom fields in WordPress without plugin? ›- The Edit Post screen in WordPress.
- Check the box "Custom Fields"
- The Custom Fields area.
- An example of saving the information about a product in custom fields.
- Add extra data into a custom field.
- Homepage after adding custom fields.
- Log in to WordPress as the administrator.
- On the Dashboard in the left sidebar, click Plugins, and then click Add New:
- Search for “Ninja Forms” and click Install Now. ...
- On the Dashboard, in the left sidebar click Ninja Forms, and then click Add New:
What is the difference between custom fields and advanced custom fields in WordPress? ›
The options page feature in ACF Pro allows you to add extra admin pages to edit ACF fields. Unlike WordPress custom fields, this allows you to create global fields that are not attached to a particular post or page. Instead, the data can be displayed on any page throughout your WordPress site.
How many custom fields can one object create? ›Custom objects have one or more fields that store data associated with a custom object record. There is a maximum of 1024 fields allowed for a single custom object.
What are the benefits of advanced custom fields? ›Advanced Custom Fields is a plugin that simplifies pretty much all aspects of custom data fields in WordPress. It streamlines the front-end procedure for displaying data from custom fields and the interface for adding and updating custom fields in your WordPress dashboard.
What are custom fields examples? ›Examples of custom fields include data such as title, URL, name, timestamp, or any piece of data you want to define on a model.
What is a key difference between standard and custom fields? ›Standard object | Custom object |
---|---|
We can't Truncate standard objects | It is possible to Truncate custom objects |
It is possible to create custom fields on standard objects | Custom objects contain some standard field, for example, Name, Created by, Last modified by and so on |
These fields might be added to an Org via a managed package or through direct customization. Standard fields in contrast are those that are already present in the Salesforce schema when a new Organization is created. They are present in all Orgs where the same features are enabled.
How do I enable advanced custom fields? ›Unzip the file you have downloaded, and upload the folder called 'advanced-custom-fields-pro' to your wp-content/plugins directory. Once ACF PRO is uploaded, you will need to activate it by finding it in the list on the Plugins page in your site's Administration area, and clicking on the 'Activate' link.
How many custom fields can be created on an object in an unlimited edition? ›Most people are familiar with these field limits. For example, a Developer Edition org can have up to 500 custom fields, while Unlimited and Performance allows 800 custom fields in total.
How do I create a custom field schema builder? ›- From Setup, enter Schema Builder in the Quick Find box, then select Schema Builder.
- Click the Elements tab.
- Click a field and drag it onto an object on the canvas.
- Enter a Field Label. ...
- Enter a Description of the custom field.
- Step 1: Create the Single Product Template. ...
- Step 2: Choose a Pre-Designed Product Page Template, or build one from scratch. ...
- Step 3: Add the Product Widgets that will make up your page. ...
- Step 4: Preview the Product Page With Another Product. ...
- Step 5: Set the Conditions.
How do you add variable products to your WooCommerce site step by step? ›
Variable Product in WooCommerce FAQs
Follow these steps to create a variable product in WooCommerce: In WooCommerce admin go to Products -> Attribures. Click the Add new attribute section and fill in the necessary fields. Tap the button Add attribute.
Conditional checkout fields for WooCommerce enable you to customize your checkout page by adding new fields to collect valuable information from your customers. You can edit, hide, or add custom checkout fields. The custom field option contains field labels, placeholders, maximum lengths, and validations of fields.
What is checkout Field Editor for WooCommerce? ›The checkout field editor provides you with an interface to add, edit, and remove fields shown on your WooCommerce checkout page. Fields can be added and removed from the billing and shipping sections, as well as inserted after these sections next to the standard 'order notes'.
How do I bypass WooCommerce plugins? ›To override WooCommerce template files in your theme (or better yet, child theme) simply make a folder named 'woocommerce' within your theme directory, and then create the folders/template file you wish to override within it.
How do I add product data tab to WooCommerce? ›To create Saved Tabs, go to Settings > Custom Product Tabs for WooCommerce. Click on the Add New button to create a new Saved Tab. Enter a title and fill the content for this new Saved Tab. Then click on the Save Tab button.
How do I add sample data in WooCommerce? ›...
Import via WooCommerce Products
- Go to: Products > All Products.
- Select Import.
- Run Importer. ...
- Select Choose file and then select the sample-products. ...
- Continue.
Importing products with attributes
Click on WebToffee Import Export (Pro) > Import menu from the WordPress admin panel. Select Products under post type. Select an import method: One can choose among: Quick import, Pre-saved template and Advanced import . Map import columns: To map import columns.
Go to WooCommerce → Settings → Products → Product Tables. Enter your license key and choose the default options for your product tables. Make sure you include the add-to-cart column in the 'Columns' section, and select the variations dropdown option under 'Variations'.
How do I add a custom field in WordPress without plugin? ›To display it, click the Screen Options and tick the Custom Fields option. The Custom Field box should now appear at the bottom of the post content editor. Now, we can add our custom meta-data. A single meta-data is handled with key name/value pairs.