rewrite rules – When I modify the product’s permalink, the pages stop working


I’m trying to modify the Woocommerce slugs with the object $wp_rewrite. When I modify it, everything works as I put it, but the rest as the pages stop working. Here the code:

public function rewrite(): void {
        global $wp_rewrite;

        $wp_rewrite->add_rewrite_tag("%category%", '((^/)+)', "category=")
        $wp_rewrite->add_permastruct( 'category', '%category%' );
        $wp_rewrite->add_rewrite_tag("%product_cat%", '((^/)+)', "product_cat=");
        $wp_rewrite->add_permastruct( 'product_cat', '%product_cat%' );
        $wp_rewrite->add_rewrite_tag("%product%", '((^/)+)', "product=");
        $wp_rewrite->add_permastruct( 'product', '%product_cat%/%product%' );
    
}

This function is executed with the hook init.

Does anyone know why it doesn’t work or is it a bug?

I also tried modifying the array of extra_permastructs, but it doesn’t work either.