0

I'm trying to rewrite the product variation url for a Phone Model attribute for example to this url:

index.php?product=example&attribute_pa_model=iphone-x

This works when I open it in the browser directly. So the original url I'd like to be:

/product/example/iphone-x

I tried with the code below, but it is not working.

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

function custom_rewrite_rules() {
    add_rewrite_tag('%attribute_pa_model%', '([a-zA-Z0-9-]+)');
    add_rewrite_rule('^product/(.+?)/(.+?)/?$', 'index.php?product=$matches[1]&attribute_pa_model=$matches[2]', 'top');
}
  • Did my answer help you? – Sally CJ Sep 10 '18 at 23:00
1
+50

This works because on this URL, $_REQUEST['attribute_pa_model'] is set, and in this example, the value is iphone-x:

index.php?product=example&attribute_pa_model=iphone-x

But on this URL, $_REQUEST['attribute_pa_model'] is not set, so the auto-selection of the product variation does not work:

/product/example/iphone-x

So on that URL, you can use the woocommerce_dropdown_variation_attribute_options_args to filter the selected value, like so:

add_filter( 'woocommerce_dropdown_variation_attribute_options_args', 'auto_select_attribute_pa_model' );
function auto_select_attribute_pa_model( $args ) {
    // If it's not the pa_model taxonomy, don't filter the $args.
    if ( empty( $args['selected'] ) && 'pa_model' === $args['attribute'] ) {
        $args['selected'] = get_query_var( 'attribute_pa_model' );
    }

    return $args;
}

Additional Note

In my tests, this is not necessary and can be removed:

add_filter('woocommerce_taxonomy_args_pa_model', 'add_model_taxonomy_args' );
  • Hello, yes that works but partially! Take a look at the link: casefloradev.wpengine.com/product/… It open up the variation, but the variation image is loaded after the page load, which is bad, both for user experience and also SEO, the main product image must not be there. Do you know how this can be fixed? – Ilian Andreev Sep 11 '18 at 0:37
  • I don't think that's related to the rewrite rule. That's indeed how WooCommerce loads the product image. Even without the custom rewrite rule, the product image would still be loaded after the page is loaded, so that users see the text-based content without having to wait for the browser to load the image. – Sally CJ Sep 11 '18 at 1:46
  • But if you disable the product images slideshow/slider, the images might be loaded while loading other content — see here for the code. – Sally CJ Sep 11 '18 at 2:25
  • Thank you, I just awarded the bounty to your answer. I don't want to turn off the gallery (also it didn't work when I tried). Just wonder if its not better to turn off the original variation image with code, as at the moment it loads the same image after page load. Or it will be bad for SEO? Do you have any advice for that? – Ilian Andreev Sep 11 '18 at 14:47
  • Sorry for the late reply, and I was actually away from my computer when I wrote my previous two comments. In those comments, I was particularly referring to the JavaScript-based product image gallery, but I realized that it might not be the issue you're talking about? And just now I visited some of the single product pages on your site, and seems like the product image is loading along with other content on the page. So has the issue been fixed, or am I not understanding it? Can you share a screen cast/shot showing the issue? PS: Thanks for the bounty award. – Sally CJ Sep 13 '18 at 6:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.