• I’m upgrading an existing site with a Woocommerce store. My urls are pretty and I want to keep them that way, but Woocommerce won’t let me.

    I know woocommerce says to leave /product/ and /product-category/ in place. I know there’s a few expensive plugins that can supposedly achieve this, and I’ve seen the multiple posts about using the Custom Post Type URL plugin and another hack to get something similar, but let’s leave that aside for now.

    So it’s a destination site where you book & buy stuff – tours, attractions and activities are the product categories.

    On the current site which only provides information, the URLs look like this:

    Product Category Pages:
    visitcapetown.dev/tours/
    visitcapetown.dev/attractions/
    visitcapetown.dev/activities/

    Product Pages:
    visitcapetown.dev/tours/craft-beer-tour/
    visitcapetown.dev/attractions/table-mountain/
    visitcapetown.dev/activities/ultimate-frisbee/

    I have managed to get this far in Woocommerce also.

    In Woocommerce permalinks admin I set ‘Product category base’ to ‘.’ and ‘Custom Base’ to ‘%product_cat%’ plus this plugin:

    function add_my_rules() {
       // Interrupt Woo
       $woo_perm = get_option('woocommerce_permalinks');
       $woo_perm['product_base'] = '%product_cat%';
       update_option( 'woocommerce_permalinks', $woo_perm );
    
       $rules_array = array(
                                ['maincat' => 'tours'],
                                ['maincat' => 'attractions'],
                                ['maincat' => 'activities']
                            );
    
        foreach ($rules_array as $rule) :
            // ReWrite It
            add_rewrite_rule('('.$rule['maincat'].')/page/?([0-9]{1,})/?$', 'index.php?product_cat='.$rule['maincat'].'&paged=$matches[1]', 'top');
            add_rewrite_rule('('.$rule['maincat'].')/?$', 'index.php?product_cat='.$rule['maincat'], 'top');
    
        endforeach;
    
        flush_rewrite_rules();
    }
    add_action('init', 'add_my_rules');

    Almost everything works:
    – Products link / work correctly;
    – Product categories link / work correctly;
    – Normal WordPress pages link / work correctly;
    – Normal WordPress post categories link / work correctly;
    – Normal WordPress posts >>link<< correctly;

    There is just 1 caveat:
    – Normal WordPress posts 404.

    In admin Permalinks, my post ‘Custom Structure’ is ‘/articles/%postname%/’.

    If I change that to ‘/%category%/%postname%/’ then the posts work, but the problem moves to Woocommerce products which then 404s.

    Even if I add a rewrite rule to test it with a specific URL for a post and tell it where to find that post (the ugly URLs keep on working), it still 404s, so I’m thinking there’s a conflicting rule that I can’t find.

    Can anybody please help with this conflict or explain why this is happening? I will be much appreciated.

Viewing 1 replies (of 1 total)
  • Thread Starter EastOfWest a11n

    (@eastofwest)

    Ok, I’ve now added the following above ‘// ReWrite It’:

    // More Important
            add_rewrite_rule('articles/([^/]+)(/[0-9]+)?/?$', 'index.php?name=$matches[1]&page=$matches[2]', 'top');
            add_rewrite_rule('articles/author/([^/]+)/?$', 'index.php?author_name=$matches[1]', 'top');

    Now it all works!

    There might be some other unexpected 404s lurking around, but I’m fairly confident the solution will be to add the rewrite rules above the ones for Woocommerce.

    It might not be the right way, but it is a way. Perhaps some kind soul that comes across this post and sees me heading for doom could stear me clear?

Viewing 1 replies (of 1 total)
  • The topic ‘Woocommerce Product & Post Category Conflicts’ is closed to new replies.