I have a woocmmerce site, where there are different types of products such as simple product, variable, auction and subscription.
I would like for each type of product to have a custom permalink, for example for auctions I would like:
domainname.com/auction/name-of-product
while for simple products I would like:
domainname.com/simple/name-of-product
I tried to create a code like this and updated the permalinks, but when I view the product pages I receive a 404 error:
add_filter( 'post_type_link', 'custom_product_permalink_by_type', 10, 2 );
function custom_product_permalink_by_type( $permalink, $post ) {
if ( $post->post_type !== 'product' ) return $permalink;
$product = wc_get_product( $post->ID );
if ( $product && $product->get_type() === 'auction' ) {
return home_url( 'auction/' . $post->post_name . "https://stackoverflow.com/" );
} else {
return home_url( 'product/' . $post->post_name . "https://stackoverflow.com/" );
}
}
add_action( 'init', 'custom_product_rewrite_rules' );
function custom_product_rewrite_rules() {
add_rewrite_rule( '^auction/([^/]+)/?$', 'index.php?post_type=product&name=$1', 'top' );
add_rewrite_rule( '^product/([^/]+)/?$', 'index.php?post_type=product&name=$1', 'top' );
}
does anyone know the correct solution to do this?