Menu Close

WooCommerce: Programmatically Rename Variable Product Attribute Options

default

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
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

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
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

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;
}
View Source
Posted in WooCommerce Tips