Custom URL for each product variation – rewrite rules
I would like to have a separate url for each of my product variations. For that I would need to create a rewrite rule, that will work on the product URLs
Ie, my product URL is https://example.com/product/tshirt/
The product has two variations – size and color. I would like to be able to access the product on url like https://example.com/product/tshirt/size-s-color-black
This clearly has something to do with the add_rewrite_rule
function. I don’t know how to build the url structure to be able to access the variations part of url
function custom_rewrite_basic() {
add_rewrite_rule('^product/([0-9]+)/([0-9]+)?', 'index.php?page_id=$matches[1]&variations=$matches[2]', 'top');
}
add_action('init', 'custom_rewrite_basic');
Anyone?
Leave an answer