Stefano Luoni
Stefano Luoni

Reputation: 79

WooCommerce Url rewrites

I've got the same problem described in this post: [WordPress URL rewrite for WooCommerce attributes, except that I need to filter by attribute not only inside a category. Unfortunately, I cannot post a comment before reaching a higher reputation, so I'm creating a new question.

I defined a manufacturer attribute and if I want to browse all products from a certain manufacturer, I can use a url like www.example.com/shop/?filter_manufacturer=230, where 230 is the attribute ID. I tried adding a endpoint, like suggested in the post linked above above, but I cannot get the rewrites working; for example, if I try to open www.example.com/shop/manufacturer/manufacturer_name I get a 404 error.

It's not clear to me if I should change anything in the permalink settings in Wordpress and, if yes, how. I've always flushed the rewrite rules after every edit, BTW.

Upvotes: 3

Views: 8067

Answers (2)

Ilian Andreev
Ilian Andreev

Reputation: 1091

I had a similar problem and was able to solve it like this:

function add_model_taxonomy_args($args) {
    $args['query_var'] = 'filter_model';
    return $args;
}
add_filter('woocommerce_taxonomy_args_pa_model', 'add_model_taxonomy_args' ); 

function custom_rewrite_rules() {
	add_rewrite_tag('%filter_model%', '([a-zA-Z0-9-]+)');
	add_rewrite_rule('^c/phone-cases/(.+?)/?$', 'index.php?product_cat=phone-cases&filter_model=$matches[1]', 'top');
}
add_action('init', 'custom_rewrite_rules', 10, 2);

Sample Url: c/phone-cases/iphone-8-plus/

Resulting Rewrite: index.php?product_cat=phone-cases&filter_model=iphone-8-plus

Upvotes: 1

helgatheviking
helgatheviking

Reputation: 26329

The missing link between your question regarding WooCommerce attributes and the linked answer is that product attributes are merely taxonomies with a 'pa_' appended to their name.

In your case the taxonomy is called "pa_manufacturer". WooCommerce sets these up by default to have no query var attached.

So in lieu of filtering query_vars we are going to target when WooCommerce registers that particular taxonomy. I've also modified to remove anonymous functions.

In my example I am using "color", so adjust to "manufacturer". I was able to then go to a URL of http://example.com/shop/color/black and see all the black products. Note that this doesn't get you a term archive where /shop/color will list all the colors. That is a different question and a lot more work.

I didn't test the activation part, so if you get 404s after activating you can just delete the whole activation function and simply go to Settings>Permalinks and save the permalinks again.

/**
 * Plugin Name: Add an WooCommerce attribute endpoint to the URLs
 * Plugin URI:  http://stackoverflow.com/q/28460538/383847
 * Credit to: http://stackoverflow.com/a/24331768/1287812
 */

function so_28460538_add_rewrite_endpoint(){
    add_rewrite_endpoint( 'color', EP_ALL );
}
add_action( 'init', 'so_28460538_add_rewrite_endpoint' );


function so_28460538_attribute_args( $args ){
    $args['query_var'] = 'color';
    return $args;
}
add_filter( 'woocommerce_taxonomy_args_pa_color', 'so_28460538_attribute_args' ); 


/**
 * Refresh permalinks on plugin activation
 * Source: http://wordpress.stackexchange.com/a/108517/12615 
 */
function WCM_Setup_Demo_on_activation(){
    if ( ! current_user_can( 'activate_plugins' ) )
        return;

    $plugin = isset( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : '';
    check_admin_referer( "activate-plugin_{$plugin}" );

    add_rewrite_endpoint( 'color', EP_ALL ); #source: http://wordpress.stackexchange.com/a/118694/12615
    flush_rewrite_rules();
}
register_activation_hook(   __FILE__, 'WCM_Setup_Demo_on_activation' );

EDIT

Adding some screenshots of my settings in case it will help determine why you are getting 404s:

Here are my permalinks settings:

permalink settings

and here is the WooCommerce setting for determining the product archive page:

WC shop settings

And finally, here is the result of visting: http://local.wordpress.dev/shop/color/black/

Where shop is the pretty permalink for the product archive page set above. All items have a 'black' color attribute.

front-end result

Upvotes: 1

Related Questions