Admin Dashboard, Product Page

WooCommerce Custom Radio Button Field For Product Editor

WooCommerce custom radio buttons showing in the product editor

Pre-Requisites

There are no pre-requisites in order for you to be able to implement this solution.

How To Implement This Solution?

Simply copy the pre-coded solution to your active theme’s functions.php or preferably the Code Snippets Plugin.

About This Solution

This guide shows you how to create custom radio buttons in the product editor of WooCommerce. This is perfect if you need to be able to select a pre-defined series of options in the backend and display the selected option on the front-end (yes, this guide shows you how you can output the option selected too).

/**
 * Snippet Name:     WooCommerce Custom Product Radio Button Field
 * Snippet Author:   ecommercehints.com
*/

// Create the custom radio metabox
add_action ('woocommerce_product_options_advanced', 'ecommercehints_custom_product_radio_metabox');
function ecommercehints_custom_product_radio_metabox() {
   echo '<div class="options_group">';
   woocommerce_wp_radio(array ( // A radio type field
      'id'                => 'custom_product_radio_metabox',
      'value'             => get_post_meta (get_the_ID(), 'custom_product_radio_metabox', true),
      'label'             => 'Radio Buttons Label',
      'description'       => 'This is the description',
      'desc_tip'          => true, // If true, place description in question mark tooltip.
     'options' => array(
       'option_1' => 'Option 1',
       'option_2' => 'Option 2',
       'option_3' => 'Option 3'
    ),
  ));
   echo '</div>';
}

// Save radio option on update
add_action ('woocommerce_process_product_meta', 'ecommercehints_save_field_on_update', 10, 2);
function ecommercehints_save_field_on_update ($id, $post) {
      update_post_meta ($id, 'custom_product_radio_metabox', $_POST['custom_product_radio_metabox']);
}

How Do I Get The Custom Radio Button Field Data?

global $product;
$custom_product_radio_metabox = $product->get_meta ('custom_product_radio_metabox');
echo $custom_product_radio_metabox;

100 WooCommerce Conversion Rate Optimisation Tips

This field is for validation purposes and should be left unchanged.

Let’s collaborate!

Need to outsource WordPress development?

Join forces with UnlimitedWP for an outsourced white label web development service you can truly rely on.

First Timer Here?

Sign up to receive 20% off on your first month with us.

26027
WELCOME OFFER