plugin development – Pass Values in URL on WooCommerce Product Page

Question

I currently pass values to a product page dynamically with $_GET params in url. However I’d like to switch to using query_vars.

Current product URL: /event/play/?eid=1765&wt=ou

I would like to change to: /event/play/eid/1765/wt/ou

Current attempts to do with query_vars:

function add_ww_query_vars( $qvars ) {
    $qvars[] = 'eid';
    $qvars[] = 'wt';

    return $qvars;
}
add_filter( 'query_vars', 'add_ww_query_vars' );


function rewrite_ww_product_pages() {
    add_rewrite_rule( 'event/play/eid/(d*)$', 'index.php?post_type=product&eid=$matches[1]', 'top' );
    flush_rewrite_rules();
}
add_action( 'init', 'rewrite_ww_product_pages' );


function ww_product_template_redirect( $template ) {
    if ( ! empty( get_query_var( 'eid' ) ) )
        return get_stylesheet_directory_uri() . 'woocommerce/single-product.php';

    return $template;
}
add_action( 'template_include', 'ww_product_template_redirect' );

The pattern is recognized, but it redirects to catalog page after attempting to go to single-product.php.

0
JoeAdroit 2 months 2021-12-03T22:47:19-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse