Discussions

Set price when creating a product via commerce_product_save($product)?

How do you set the price when creating a product with commerce_product_save()? I'm first calling commerce_product_new($type) and then filling in fields of the returned product, but there isn't one for price. I also have a "stock" field I'd like to set.

Thanks for the help.

Posted: Sep 21, 2011

Comments

Prince Manfred on September 21, 2011

Thanks for the response. I came up with a very similar solution earlier after loading in an existing product and just dumping the variable. I cut the commerce_product_new call and just built the product from an empty object. Is there any execution time savings from doing this do you think? Is there a draw back? Also, it looks like the creation and update times are set for you after you pass the object off to be saved. Is there any reason you should set it manually?

One last thing. Where is the product_id you are returning come from? Does commerce_product_save assign it to the variable when you call it?

Sorry for all the questions. They aren't hugely important since it seems to be working fine, but if I can fine tune the function at all it'd be great.

Thanks again!

rfay Randy Fay on September 22, 2011

I haven't studied the performance of product creation so can't help there.

If I'm not mistaken, the product_id is set in commerce_product_save() if the product_id passed in is 0, Might be in commerce_product_new() though.