WooCommerce: Programmatically Rename Variable Product Attribute Options

I think the easiest way to explain this customization is with a case study. Go to this variable product page, that I use to sell sponsorship packages on the WooWeekly newsletter: https://www.businessbloomer.com/shop/newsletters/wooweekly-sponsorship/

Now, take a look at the “Start in” attribute dropdown. That’s where a company picks the month for when the sponsorship starts. You will notice that this always displays the next 3 months based on today’s date!

And that’s exactly what I’ve done with the code below. Instead of manually changing the attribute names to “Nov 2023”, “Dec 2023”, “Jan 2024” in October 2023, then changing them again in November, and so on – I’ve come up with a way to rename attribute options dynamically, so that I don’t need to do this every month.

Which means, enjoy, and hope you can make the most of this snippet in case you need it too!

These variable product dropdown options are generated automatically with the snippet below! In this way, I don’t need to rename them every month or do any other manual work.

PHP Snippet: Dynamically Replace Attribute Option Labels @ WooCommerce Single Variable Product Page

Note: of course, you need to properly set up your variable product with attributes and attribute terms. And in order to replace some of them, you need to know their exact names:

In my case these are ‘Next Month‘, ‘2 Months‘, ‘3 Months‘ – and I want to replace them with the actual month e.g. “Oct”, “Nov”, “Dec”.

/**
 * @snippet       Override Attribute Terms @ Variable Product
 * @how-to        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 8
 * @community     https://businessbloomer.com/club/
 */

add_filter( 'woocommerce_variation_option_name', 'bbloomer_rename_attribute_option_name', 9999, 4 );

function bbloomer_rename_attribute_option_name( $name, $term, $attribute, $product ) {
	switch ( $name ) {
		case 'Next Month':
			$name = date( 'M Y', strtotime( 'first day of +1 month' ) );
			break;
		case '2 Months':
			$name = date( 'M Y', strtotime( 'first day of +2 months' ) );
			break;
		case '3 Months':
			$name = date( 'M Y', strtotime( 'first day of +3 months' ) );
			break;
	}
	return $name;
}

Another little note – this will only work when your attribute term names have more than 1 word: ‘Next Month‘, ‘2 Months‘, ‘3 Months

In case you use single words, this snippet will only change the variable product dropdown, but won’t apply the replacements on the Cart, Checkout and Thank You pages because single named attribute terms are concatenated to the parent product name as opposed to being displayed below it (cart item meta):

In this case, you need additional code. We will replace an array of strings (attribute term names) with an array of strings:

/**
 * @snippet       Override Attribute Terms @ Cart, Checkout, Thank You
 * @how-to        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 8
 * @community     https://businessbloomer.com/club/
 */

add_filter( 'woocommerce_cart_item_name', 'bbloomer_rename_variation_name_cart', 9999 );
add_filter( 'woocommerce_order_item_name', 'bbloomer_rename_variation_name_cart', 9999 );

function bbloomer_rename_variation_name_cart( $html ) {
	if ( strpos( $html, ' - ' ) !== false ) {
		$html = str_replace(
			[ "Yellow", "Green" ],
			[ "Whatever", "Whatever else" ],
			$html
		);
	}
	return $html;
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

2 thoughts on “WooCommerce: Programmatically Rename Variable Product Attribute Options

  1. Hi Rodolfo, but will it also change within the order?

    Thanks

    1. Not in the backend, because there is no filter for the attribute names. I’ve asked WooCommerce to change that: https://github.com/woocommerce/woocommerce/pull/41041

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *