Skip to content

When upgrading to WooCommerce 2.3 or higher, you may notice that any use of custom attribute archives such as /style/vintage (an archive of all products with the “vintage” attribute) no longer work and instead force you to use /pa_style/vintage.

Strangely, this wasn’t addressed in the changelog or release notes, and WooThemes staff only has to say “I believe it could have been that the error was fixed“, referring to a potential URL conflict.

We’re using the cleaner URL style on Nomad Rugs, creating archives for pages such as 6×9 rugs (nomadrugs.com/size/6×9/) and contemporary styles (nomadrugs.com/style/contemporary/). We needed to keep these links functioning while upgrading to the latest WooCommerce release, so we put together the below functions.

If you’re mindful of your slugs and categories and want to keep using this cleaner URL style, add this to your child theme’s functions.php or your functionality plugin.

// Change attribute rewrite rules
add_action('woocommerce_register_taxonomy', 'razorfrog_woo_register_taxonomy');
function razorfrog_woo_register_taxonomy() {
	global $razorfrog_woo_attribute_labels;
	$razorfrog_woo_attributes_labels = array();

	if ( $attribute_taxonomies = wc_get_attribute_taxonomies() ) {
		foreach ( $attribute_taxonomies as $tax ) {
			if ( $name = wc_attribute_taxonomy_name( $tax->attribute_name ) ) {
				$razorfrog_woo_attribute_labels[ $tax->attribute_label ] = $tax->attribute_name;
				add_filter('woocommerce_taxonomy_args_'.$name, 'razorfrog_woo_taxonomy_args');
			}
		}
	}
}

function razorfrog_woo_taxonomy_args($taxonomy_data) {
	global $razorfrog_woo_attribute_labels;
	
	if (isset($taxonomy_data['rewrite']) && is_array($taxonomy_data['rewrite']) && empty($taxonomy_data['rewrite']['slug'])) {
		$taxonomy_data['rewrite']['slug'] = $razorfrog_woo_attribute_labels[ $taxonomy_data['labels']['name'] ];
	}	
	return $taxonomy_data;
}

Once added, be sure to re-save your Permalink structure to apply these changes.

Max Elman is Razorfrog's Founder and Project Manager. He is a tech-savvy internet guru and has led Razorfrog's award-winning web design team since 2008. View Max's bio for more details.

Toggle light/dark mode

Back To Top