Jump to content

Create Product Programmatically


Recommended Posts

I'm trying to add a product from my module.

The idea it so use PrestaShop webservice to get product information from our second server and then use the PS classes to create a product on the first server.

 

I get the info with WebService without any problems.

And the product gets created, but it's not complete.

                    $nlProduct = new Product(1596); // Remove ID later
                    $nlProduct->id_category_default = 2;
                    $nlProduct->id_category[]=$nlProduct->id_category_default;
                    $nlProduct->new;
                    $nlProduct->type = "simple";
                    $nlProduct->reference = $sgProduct->reference;
                    $nlProduct->weight =  $sgProduct->weight;
                    $nlProduct->price = ceil($sgProduct->wholesale_price*1.25);
                    $nlProduct->wholesale_price = $sgProduct->wholesale_price;
                    $nlProduct->active = 0;
                    $nlProduct->available_for_order = 1;
                    $nlProduct->show_price = 1;
                    $languages = Language::getLanguages();
                    foreach($languages as $lang){
                        $nlProduct->name[$lang['id_lang']] = $sgProduct->name->language;
                        $nlProduct->description[$lang['id_lang']] = $sgProduct->description->language;
                    }
                    echo($sgProduct->name->language);
//                     $nlProduct->add();
                    $nlProduct->update();

I added the ID number for the new product and changed to update while testing.

$sgProduct is the data from WebService, and $sgProduct->name->language does contain the name I want.

But the $nlProduct does no write any name to PrestaShop.

And also, the category does not get set, it has not categories selected in PS.

 

What am I missing here?

 

 

Link to comment
Share on other sites

If I use PrestaShop's BO and add category and a random name to the product, the code works and updates to the correct name and category.

But if I use the code to add() the product, still no name and category, and if i change to update() again it does not set the name, and it give me a 500 Internal Error only visible in FireBug.

Link to comment
Share on other sites

Another update.

 

Even this small code causes 500 Error, and second ECHO is not displayed in browser, but still updates the reference.

echo('Test Code<br>');
   $resource =  new Product(1599);
    $resource->reference = 'TEST '.date('Y-m-d H:i:s');
   $resource->update();
    $resource = null;
echo('Test Code End<br>');
Edited by PhpMadman (see edit history)
Link to comment
Share on other sites

  • 2 weeks later...

Ok 

 

1- if you use save the object create or update the record with the id

 

2- Do you have develop mode on ?

 

3- do you have access to your server log?

 

To find out the error we need to know where is the mistake

Link to comment
Share on other sites

Hmm. Seems I never uploaded the file to enable  dev mode.

It's the little things.

 

It was missing link_rewrite. Added that, name and description and my test code worked just fine.

 

And also, this error did not produce any error / server log

 

*EDIT*

 

Real code works fine, all text / price file gets updated.

 

Now I'll just need to figure out how to download and add the product images...

 

Thanks for you help.

Edited by PhpMadman (see edit history)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...