add_rewrite_rule with query variables

First of all, Your code does not work because rewrite rules need to be flushed ( function flush_rewrite_rules ), and they need to be flushed for every $id change. Generally this is very bad idea, for performance reasons.

You should add rewrite rule only with post_type=product, as You want to pass “p” as $_GET parameter

add_rewrite_rule( 'app-api/v2/ecommerce', "index.php?post_type=product" );

then in template_include hook function look for $_GET[‘pid’] and based on it, choose Your template

Hope, it helps