![]() |
|
[Tut] How to Add Custom Field to Product in WooCommerce - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: PHP Development (https://sickgaming.net/forum-82.html) +--- Thread: [Tut] How to Add Custom Field to Product in WooCommerce (/thread-99646.html) |
[Tut] How to Add Custom Field to Product in WooCommerce - xSicKxBot - 06-30-2022 How to Add Custom Field to Product in WooCommerce by Vincy. Last modified on June 29th, 2022. WooCommerce is the best among the top eCommerce software which is free. It is based on WordPress platform. Setting up a shopping cart is quite easy with WooCommerce. In previous articles, we have seen some WooCommerce-based code for the following utilities. The WooCommerce product showcase displays the information that meets the customer demand. Sometimes preliminary details cannot convey all and let the customer identify their destiny. This is something that needs adding additional information using custom-field integration. WooCommerce plugins support adding custom fields to products via the admin interface. We will see those options below and their usage methodologies of them. Why a WooCommerce shop admin needs Custom Fields Before adding custom fields to a product, learn why a WooCommerce shop needs this. Because, some of the shops have a routine product gallery with usual data like title, image and price. In that case, the custom fields are not required. The below list shows the advantages of this custom fields feature on a WooCommerce shop.
Types of custom fields creation supported by WooCommerce plugins The available WooCommerce plugins can add two types of custom fields for products.
1. Data fields – The field accepts information to be displayed on the UI This custom field is for showing more information apart from the regular title, price and all. This will help to show product metadata about features, version, rating and all. For example, a monitor, to show extra information like, Brand | Dell Model | Dell 20H Screen Size | 23 Features | HDMI, Anti-glare 2. Add-on fields – Fields that collect data from the user on the storefront This type of custom field is about creating options for the customer to interact with. It is to allow adding more specifications or customization on the pursued product. It is for adding personalized information by the customers on the product purchase. Example,
Methods of adding custom fields display in WooCommerce
Method 1: Using the plugin We have taken three plugins to support adding custom fields to a product in a WooCommerce shop. Those are having advanced features and are also easy to set up.
1. Advanced Product Fields for WooCommerce This plugin is used for adding additional fields to the WooCommerce product page. It helps to embed product add-on fields to allow customers to personalize the order. Look into the plugin details and prerequisites below. Version | 4.5 or higher Active installations | 20,000+ PHP version | 5.6 or higher Last updated | 2 weeks ago Features
Steps to use Download this plugin from the official WordPress repository. Then install and activate it for the WooCommerce shop. If you are new to WordPress, then see here how to set up a plugin.
This is the WooCommerce admin interface to add the custom fields for products. I have added the Size option for the product as a ratio group. ![]() This will show the added custom field on the product front end as a radio option. ![]() 2. Product Addons for WooCommerce This plugin says the purpose clearly by its name itself. Yes, it is to add more add-on fields to let the customer interact with the shop to personalize the purchase. An easy custom form builder allows for building a field group for the WooCommerce shop pages. Have a look at the version and other details below. WordPress Version | 4.0 or higher Active installations | 30,000+ PHP version | 5.6 or higher Last updated at | 3 weeks ago Download this plugin from the WordPress repository linked. The plugin provides a drag-and-drop interface to build custom field groups for products. Features
Steps to use There are a few steps to add and display a custom field on a product page of a WooCommerce theme.
See the following two screenshots to add custom fields and map them for the product. ![]() ![]() Then, the WooCommerce shop will show the custom fields on the single product page. ![]() 3. Advanced Custom Fields It supports full control of the WordPress edit screen and custom form data. WordPress Version | 4.7 or higher Active Installations | 2+ million PHP Version | 5.6 or higher Last updated | 6 days ago The simple and intuitive plugin has powerful functions and over 30 field types Features
Steps to customize Download or install via admin to enable this plugin before going to read the below steps.
Step 1: Add and customize field group Go to Custom Fields via the admin menu. Add a new field group and specify the UI location and other settings. It helps to set states and styles to add and show the custom fields on a product page. It manages the relative position and alignment of the field on the UI. Fields can also be mapped for posts, products, pages and more. Step 2: Add form fields into the group The field add interface allows the following data to enter. For combo fields, it asks to enter multiple options to the loaded.
Step 3: Add data for the custom fields based on products If the custom field group is mapped for the location of product pages, then the add/edit page will show it. Add the data for the custom fields which will be displayed on the WooCommerce product at the shop. ##image Step 4: Display the product information on the front-end Then display the custom fields data on the product single page at the front-end. In this plugin, we need to add a simple shortcode to display the custom fields on the product page. The [acf field=”<field-id or slug>”] shortcode is used for displaying the custom field group. There are a couple of functions in WordPress to display the fields for a WooCommerce product. Code: <php
the_field("<field-id or slug>");
?>or Code: <php
$customFieldGroup = get_field("<field-id or slug>");
echo $customFieldGroup;
?>Thus, we have seen how to add product custom fields in a WooCommerce platform using plugins. If you want to implement the same without using plugins, it’s very simple with a few steps. These steps render the custom fields on the WooCommerce product page. After collecting the user inputs in the product page, the custom code will display them on the cart page. Steps to implement this method by adding custom code are listed below.
Add this custom code in the WordPress active theme’s function.php file. 1. Adding custom fields in the product data panel Create code for displaying custom fields in the WooCommerce product data panel. The woocommerce_product_options_general_product_data hook is used to call this code. Code: /* Add product meta field for the WooCommerce admin */
function woocommerce_custom_select_dropdown(){ $select = woocommerce_wp_select(array( 'id' => '_select_color', 'label' =>__('Select color', 'woocommerce'), 'options' => array( 'Black' => __('Black','woocommerce'), 'Blue' => __('Blue','wooocommerce'), 'Pink'=> __('Pink','woocommerce') ), ));
}
add_action('woocommerce_product_options_general_product_data', 'woocommerce_custom_select_dropdown');This example adds a dropdown as a custom field for products. So, it uses the woocommerce_wp_select function to set the following parameters.
There are similar functions to add other types of form fields. For example,
2. Save the user input in the custom fields to cart metadata. Then, another action hook is added to save the custom field value on the product meta. This will be called on saving the product details. It calls the woocommerce_process_product_meta action and hooks the handler. Code: /* Save custom field data for the product */
function woocommerce_product_custom_fields_save($post_id)
{ $woocommerce_select_color_field = $_POST['_select_color']; if (!empty($woocommerce_select_color_field)) { update_post_meta($post_id, '_select_color', esc_attr($woocommerce_select_color_field)); }
}
add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');In this section, it prepares an add-on field HTML if the product meta is not empty. First, the admin chooses and saves the product color option. Then this code will add a dropdown field on the product single page. It reads the current product meta with the reference of the global post object. Then, it fetches the product meta by the custom field id. In the WooCommerce product page, this custom field is added before the cart button. It is because of hooking the action woocommerce_before_add_to_cart_button to call this handler. Code: /* Show add-on field based on the saved custom field */
function woocommerce_display_select_option_value()
{ global $post; $product = wc_get_product($post->ID); $select_option_title_field = $product->get_meta('_select_color'); if ($select_option_title_field) { printf('
<div><select name="color_option" class="input-text text">
<option value="Default Color">Default Color</option>
<option value="' . $select_option_title_field . '">' . $select_option_title_field . '</option> </select></div>', esc_html($select_option_title_field)); }
}
add_action('woocommerce_before_add_to_cart_button', 'woocommerce_display_select_option_value');The following filter hooks handle the functions to display the selected custom option. It has the cart and checkout pages as its target to render the UI component. This code is triggered on loading the cart item data. It calls the filter hook woocommerce_add_cart_item_data and displays the selected custom field value. It adds the data into the cart item array which will be later used in the checkout page UI. Code: /* Display selected option in the cart */
function woocommerce_add_custom_field_item_data($cart_item_data, $product_id)
{ if (! empty($_POST['color_option'])) { $cart_item_data['select_field'] = $_POST['color_option']; } return $cart_item_data;
}
add_filter('woocommerce_add_cart_item_data', 'woocommerce_add_custom_field_item_data', 10, 2);This is to parse the cart item array and display the custom field value on the checkout page. It uses the woocommerce_cart_item_name hook to do this change in the checkout UI. Code: /* Display selected option in the checkout */
function woocommerce_cart_display($name, $cart_item, $cart_item_key)
{ if (isset($cart_item['select_field'])) { $name .= sprintf('<p>%s</p>', esc_html($cart_item['select_field'])); } return $name;
}
add_filter('woocommerce_cart_item_name', 'woocommerce_cart_display', 10, 3);Thus, we have seen both methods to add custom fields to products. It is with or without plugins to enable WooCommerce product custom fields. I hope, this article gives a basic knowledge in this area. Also, it might help you to understand and replicate the steps to customize your shop. ↑ Back to Top |