You can add the code anywhere in the Prestashop. If you are using any module, then you can add the code in any file of the module. Just make sure that your code runs wherever you paste it.
You can even make your own php file and run the file from url and the product will be added into the database. You don't even need tpl file to create a new product -
In the following example, let's suppose that you are creating a new php file in the root folder of prestashop.
First it is necessary to include these rows in your php file so that you can use other core prestashop classes. Then you can simply write your code to add new product -
$product->reference = $product_code;
$product->price = $price;
$product->avtive = 1
$product->weight = $weight
// Keep assigning whatever you want
// And then finally
Your product will be saved into the database. And if you want to add new product using your module then simply you can add the code into the modulename.php file inside getContent() method. Then whenever you open module's configuration page from prestashop backend then a new product will be added into the database. Here you don't need to include following lines -
You don't need tpl file to save the product unless you want to provide information through user interface.