I've created a shortcode which outputs product schema in JSON-LD format.
1 schortcode = 1 product = 1 product schema output 2 schortcode = 2 product = 2 product schema output
The problem is that then I've realized that all the products in an article should create a single JSON-LD (see the image).
2 schortcode = 2 product = 1 product schema output
This means that the schema output should be outputted only by the last shortcode of this kind.
Shortcodes output in wordpress are usually independent one from the others, they are "stateless" (the output depends only on the input parameters). But can I force them to a different behaviour? I've thought of using global variables inside the shortcode handler and output the aggregated JSON only after the last shortcode has been called).
Is this a good solution?