Razorfrog Web Design Razorfrog Web Design
  • Home
  • About
  • Services
    • Responsive Web Design
    • E-Commerce Stores
    • Logo & Brand Identity
    • Search Engine Optimization
    • Copywriting & Editing
    • Photography & Imaging
  • Portfolio
  • News
  • Contact
Close
  • Home
  • About
  • Services
    • Responsive Web Design
    • E-Commerce Stores
    • Logo & Brand Identity
    • Search Engine Optimization
    • Copywriting & Editing
    • Photography & Imaging
  • Portfolio
  • News
  • Contact

Removing pa_ from product attribute URLs in WooCommerce 2.3+

March 6, 2015

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.

Posted in: Resources Tagged: WooCommerce Author: Max Elman

Share

FacebookTwitter

16 Comments

  1. Sam Jones
    March 11, 2015 at 2:00 pm / Reply

    Bloody Brilliant. No mess, no fuss. Just works.

  2. stevelock
    March 16, 2015 at 2:51 am / Reply

    Great solution, and it works with WPML as well. Thanks a lot! My guess is WooCommerce did this change in order to be prepared for WordPress 4.2, which has a huge taxonomy change coming up. I haven’t found a confirmation on this however, so it’s just a guess. Needless to say; your fix might cause some trouble when updating to WordPress 4.2 (expected in April).

  3. Klaus Nahr
    April 19, 2015 at 12:21 pm / Reply

    THANKS!

  4. Lawrence
    June 1, 2015 at 12:31 pm / Reply

    Worked perfectly. I’m surprised they didn’t mention this in the release notes.

  5. Chad
    September 22, 2015 at 1:34 pm / Reply

    I want to have your baby.

  6. Javier
    October 13, 2015 at 1:27 am / Reply

    Simple, brilliant, magnificus…. Worked great. Razorfrog for President!
    Sell this bug solution to Woocommerce for $1,000,000

  7. CFay
    February 4, 2016 at 8:06 am / Reply

    This appears to be broken now unfortunately after the latest WooCommerce and WordPress updates. Is this working for anyone else right now/does anyone have a fix?

    • Max Elman
      February 4, 2016 at 9:24 am /

      It’s still working fine – this was due to a 301 redirection issue. Glad you got it resolved!

  8. Naydee
    February 9, 2016 at 5:16 pm / Reply

    Just wanted to say awesome! This worked like a charm, I was pulling out my hair as I couldn’t find any reason for this change. No trace of this change other then this page showing up in the search results. Thank you guys

  9. Garrus
    September 22, 2016 at 1:03 am / Reply

    Hi, I tried putting in your code…didn’t work for me. Did I miss out anything?

    my site eg. is http://www.gameon.co, with the shop at http://www.gameon.co/shop. I’m trying to show “Style” attribute with “Crystal” term. So I guess it should be http://www.gameon.co/style/crystal ?

    1) Change ur razorfrog in the code to gameon
    2) Paste in functions.php
    3) Tried http://www.gameon.co/style/crystal and http://www.gameon.co/shop/style/crystal

    Didn’t work 🙁

    Please help!

    • Max Elman
      September 22, 2016 at 1:05 am /

      Did you save / refresh Permalinks under settings?

  10. Farah
    October 26, 2016 at 3:13 am / Reply

    This doesn’t seem to be working for me, please help. I’ve re-saved the permalinks but nothing has changed 🙁

    • Max Elman
      October 26, 2016 at 12:54 pm /

      The code is still working on our sites. Have you tried disabling all plugins other than WooCommerce and using a core theme to test for conflicts?

  11. Reuben
    February 20, 2017 at 12:50 am / Reply

    Still works perfectly. Many thanks!

  12. Brandon
    April 6, 2017 at 8:03 am / Reply

    Mixed results w/ Woo 3.0. pa_ is still present. The archive page however only appears while this code is active. So it doesn’t have an archive page without this code, but the whole pa_ prepended is still there.

    • Max Elman
      April 6, 2017 at 1:34 pm /

      Hey Brandon – we’ll be finishing up our testing soon and will post updated code when ready. Thanks for checking in!

Leave a Reply / Cancel Reply

  • Prev
  • Next

© 2008-2021 RAZORFROG WEB DESIGN
Sitemap • Terms of Service • Privacy Policy
415-480-4587 • 540 Howard St
, San Francisco, CA 94105
Powered by WordPress