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        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @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        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @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

  • WooCommerce: Custom Add to Cart URLs – The Ultimate Guide
    In WooCommerce you can add a product to the cart via a custom link. You just need to use the “add-to-cart” URL parameter followed by the product ID. This tutorial will show you how to create custom URLs to add simple, variable and grouped products to the cart – as well as defining the add […]
  • WooCommerce: Add Custom Field to Product Variations
    Adding and displaying custom fields on WooCommerce products is quite simple. For example, you can add a “RRP/MSRP” field to a product, or maybe use ACF and display its value on the single product page. Easy, yes. Unfortunately, the above only applies to “simple” products without variations (or the parent product if it’s a variable […]
  • WooCommerce: Display “FREE” Instead of $0.00 Price
    In older versions of WooCommerce free prices used to display as “FREE!” and products with empty prices were not publishable/purchasable. Now they’ve changed this around, but I still believe “FREE” looks much better than “$0.00”. It’s much more enticing, isn’t it? Well, here’s how you restore the old WooCommerce functionality – as usual it’s as […]
  • WooCommerce: Display Variations’ Stock @ Shop Page
    Thanks to the various requests I get from Business Bloomer fans, this week I’m going to show you a simple PHP snippet to echo the variations’ name and stock quantity on the shop, categories and loop pages. Of course, if “Manage stock” is not enabled at variation level, the quantity will be null, and therefore […]
  • WooCommerce: How to Display Variations with Color / Size Buttons?
    Displaying product pages nicely is the entrepreneur’s dream. Good UX means a much higher probability the interested customer is going to add to cart and complete the checkout. However, WooCommerce variable products come with annoying dropdowns for each attribute (color, size, style, etc. depending on what options you have set up). And as you’ve already […]

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 *