1. Home
  2. Knowledge Base
  3. WooCommerce Product Options
  4. Developer Documentation

How to change the default value of any attributes used by fields

Each option type WooCommerce Product Options adds to your products has its own unique settings and developers can change many of them in the option editor. There are situations, though, where it is necessary to fine-tune an option beyond what the editor can do and covering every possible use case is virtually impossible or would make the option editor unnecessarily complex and very difficult to navigate.

For this reason, we have provided a special function that allows you to adjust every setting an option has and even include settings that are not part of our original implementation. This is essential for allowing extensibility while keeping the plugin's interface as simple as possible for the most common cases.

The get_setting() method of the option object

The option class includes a get_setting() method that returns the value of a setting or a default value if the setting is not found. For example, a number option has a default_value setting that stores the default value of the option when the product page is first presented to your customers. The option editor includes a "Default value" setting where you can enter the number that the field will display when the page loads, which shoppers can then change to their preference.

But what if you wanted that default number to be different for each product or category? One approach would be to create a different Number option and assign it to each product or category that has a different default value. However, this increases redundancy and reduces efficiency: every time you need to change something, you must search through several options, find the correct one, and edit it. The get_setting() method of the field objects – more specifically, the wc_product_options_get_setting and wc_product_options_get_{$field_type}_{$setting_name}_setting filter hooks that it fires – allows you to manipulate the value returned for any setting of any option on any product. This extends to attributes such as step, max, min, or value. For example, you may have a Text option displaying with a default value when the page loads. Or you may want to force a Number option to change in certain increments.

Code snippets are aimed at developers. If you feel comfortable using them, our article on how to use code snippets can serve as a helpful guide. However, if you don't feel comfortable using them, then you should ask your developer. If you don't have one, then you can use our plugin customization service.

Examples

Let's illustrate the use of those filters with a few examples.

Increments of Number options

A store sells mirrors and allows customers to enter the width and height of the finished product. For production reasons, they need to ensure the values entered by shoppers are provided in increments of 5 millimeters. The Number option allows setting the number type as whole or decimal, without any further adjustments. The following code snippet will force the options to use the desired increment:

<?php
add_filter( 'wc_product_options_get_number_step_setting', 'my_custom_number_step_filter', 10, 4 );
function my_custom_number_step_filter( $step, $setting, $field, $product ) {
    if ( in_array( $field->get_name(), [ '1234', '5678' ], true ) ) {
        $step = 5;
    }

    return $step;
}

Please note that the name of the filter hook is variable and depends on the field type and the setting name you want to filter, according to this format:

wc_product_options_get_{$field_type}_{$setting_name}_setting

where you can replace $field_type and $setting_name with the field type and setting name you need to target. In the code snippet above, the field type is number and the setting name is step.

The default value of a Date option

A store sells custom stickers and allows customers to select the production time using a Date option. They need the date to default to 7 days from today when the page loads. Here is how they can set that up:

<?php
add_filter( 'wc_product_options_get_datepicker_default_value_setting', 'my_custom_datepicker_default_value_filter', 10, 4 );
function my_custom_datepicker_default_value_filter( $value, $setting_name, $field, $product ) {
	if ( $field->get_id() === '1234' ) {
		// Set default date to 7 days from today
		$value = date( 'Y-m-d', strtotime( '+7days' ) );
	}

	return $value;
}

As you can easily recognize from the structure of the filter callback functions we used above, the parameters passed to the callback are:

  • $value: The value currently used for the setting
  • $setting_name: The name of the setting being manipulated
  • $option: The current option object
  • $product: The WooCommerce product the option is attached to

Thanks to those parameters, not only will it be easy to alter the value of the setting based on the current option and product, but it will also be possible to use a product property for the value we are altering. For example, if we go back to the latest example, the number of production days may be different depending on the product and the specific value the store wants to default to might be saved as a meta field of the product. Let's say the meta name is production_days. Then, the code snippet becomes:

<?php
add_filter( 'wc_product_options_get_datepicker_default_value_setting', 'my_custom_datepicker_default_value_filter', 10, 4 );
function my_custom_datepicker_default_value_filter( $value, $setting_name, $field, $product ) {
	if ( $field->get_id() === '1234' ) {
		$production_days = $product->get_meta( 'production_days' ) ?? 7;
		$value = date( 'Y-m-d', strtotime( "+{$production_days}days" ) );
	}

	return $value;
}

The minimum value of a Customer Price option

A charity accepts donations to provide different social services. Each service costs the charity a different amount to deliver, so donors are presented with Customer Price options that have different minimums depending on the type of service. For instance, if the donor wants to donate a Hot Meal, they cannot enter less than $3 in the Customer Price option. If they donate a Winter Kit (coat + blanket), then the minimum will be $20.

Let's say that the minimum is stored in a meta field called service_cost. The filter they would need to use is the one shown below:

<?php
add_filter( 'wc_product_options_get_customer_price_min_price_setting', 'my_custom_min_price_filter', 10, 4 );
function my_custom_min_price_filter( $value, $setting_name, $field, $product ) {
	if ( $field->get_id() === '1234' ) {
		$service_cost = $product->get_meta( 'service_cost' ) ?? 0;
		
		if ( floatval( $service_cost ) > 0 ) {
			$value = $service_cost;
		}
	}

	return $value;
}

Related Articles

If searching the knowledge base hasn't answered your question, please contact support.