Jump to content

How alter and save a product with PS class?


Studio 13
 Share

Recommended Posts

Hi

Im currently working on a xml module import. Simply importing stock data to prestashop from an xml output gathered from my supplier.

I've worked out the xml import and currently list the differences of all products in an AdminTab.

I have managed to get the update work with a manual sql update on each product, but this get's tricky when there are combinations involved. I can probably get that to work but as there are functions in the Product class to update a product I thought that would be better to use. Managed to actually update the product with the new quantity. Thought the product would be stored in DB with current product object loaded. So I change the say, $product->quantity to the new qty but when I used the function to save it I got all names messed up. Only 1 letter if any was stored. Almost like there where an array index stored from an converted string. I loaded the product object with language ID so the name wasn't returned as array.

Im kind of a noobie on php and there where something about "NULL VALUES" in the update function?

Sorry if I seem a little clueless here, but if somebody could point me in the right direction, that would be great.
thx.

PS v1.3, php 5.3.0

Share this post


Link to post
Share on other sites

  • 1 month later...
  • 3 months later...

Hi,

I have the same problem using PS 1.4.1.
I have created a product using the PS backoffice pages.

My product name is "test".
I have the following code :

$product = new Product(14, false, 2);
$product->price = 1009;
$product->update();


After this simple query, the name of my product is "e" and if I call my function again, it will be empty.

Any idea? is the update function bugged?

Many thanks,

J.

Share this post


Link to post
Share on other sites

I've written an xml importer, heavily modelled on the CSV importer that ps 1.4.x uses and haven't had a problem. To get a product to modify I just used:

$product = Product::getByReference($data['my_unique_reference_field']);



You can then test to see if a product to update was found, and if not then create a new product to add. The language really isn't an issue at this stage since you only need to touch that if/when you;re modifying the product title and description.

After the above you should just be able to do:

if (!$product)
{
 $product = new Product();
 self::setEntityDefaultValues($product); // See the AdminImport core tab to see what would be appropriate for a product
 $product->reference = $data['my_unique_reference_field']; // This is essential in my case as I use this to sync. products
}



After this point you can treat new or existing products in a similar way, assuming that you want to update all the product properties (name, description, price, category, features, attributes etc.).

You can set most of the basic properties to a new product, but there will come a point when you need it to have a valid id (e.g. to map it to categories, add images, add features, add attributes etc.). At this point in the processing you should call the following, but only for new products:

$result = $product->add();



Now you have an id for both products and they can share common code again for the rest of your processing.

Once you performed all your updates then save the remaining changes:

$product->update();




The language should only really come into play when you need to create the arrays for name and description and when generating the friendly urls.

@sors You can't set the name to simply a string, as it should be be multi-language array.

@jgonze You aren't updating the name and description etc., so I suspect you should use the constructer:

$product = new Product($id, true);



Otherwise you'll mess up those fields that haven't been loaded....

Paul

Share this post


Link to post
Share on other sites

Hi,

Thank you very much for your help but...

This first piece of code is working fine... without giving information about the langage (the third argument).

function productTest(){
   $product = new Product(15, true);
   $product->price = 1009;
   $product->update();
}


But if I give a third argument, the langage id, it is not working anymore...

function productTest(){
   $product = new Product(15, true, 2);
   $product->price = 1009;
   $product->update();
}



If for example, I have the name "product", and three langages defined (1, 2 and 6) the call to the update function will change the name in each langage to :
Lang 1 : the first letter of the original name ("p")
Lang 2 : the second - ("r")
and for the Lang 6 : the sixth letter ("c").

And of course, for the Lang 2 and 6, if I call the function again, the name will be empty as there is no letter anymore in position 2 and 6...

Can you reproduce the same?

J.

Share this post


Link to post
Share on other sites

I'm not sure why you want to specify the language in the constructor at all. If you want to update the product name or any of the other multi-language fields, then I believe that you would do them all anyway (if one changes, then surely they all change).

The way I would set the name for example, would be:

$multiLangName = array('1' => 'name in language 1', '2' => 'name in language 2', '6' =>'name in language 6');
$product->name = $multiLangName;



So you only ever need to use the first form.

Paul

Share this post


Link to post
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
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More