Welcome to Discussion Forum, where you can ask questions and receive answers from other members of the community.

Add New Product Programmatically : Prestashop

1 like 0 dislike
I have never used Prestashop but I have I client with a shop created with it.
 
I need to insert products from external database to Prestashop one.
 
I found some code in php like:
 
$product = new Product();
....
 
One I have this code, where have I to use it?
 
I try to create a new module but don't know how to link .tpl page to execute the code.
 
Could someone explain my where to save the php code? I Have to use it into a class? Where to put the code and the page so instructions are recognized?
 
Thanks.
asked Apr 6, 2017 in Prestashop by harsh (8,710 points)

1 Answer

0 like 0 dislike

Hi,

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.

include('/config/config/.inc.php')

include('init.php')

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

$product->save();

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 -

include('/config/config/.inc.php')

include('init.php')

You don't need tpl file to save the product unless you want to provide information through user interface.

    
answered Apr 10, 2017 by Mayank Singh (940 points)

Related questions

0 like 0 dislike
0 answers 186 views
0 like 0 dislike
1 answer 644 views
0 like 0 dislike
0 answers 15 views
15 views asked Apr 28, 2017 in Prestashop by harsh (8,710 points)
0 like 0 dislike
1 answer 23 views
23 views asked Mar 30, 2017 in Prestashop by harsh (8,710 points)
0 like 0 dislike
0 answers 388 views
...