Jump to content

Welcome, Guest!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

FooLab

[1.0.8] how to programmatically edit/save a product?

Recommended Posts

I have a module that initiate a sync once a product is saved.

I have a CSV list with product ids (or references in case it needs).

How can I set up a cron that once launched "save" those products so the sync is initiated?

Share this post


Link to post
Share on other sites

I don't understand what you want achieve. And what means "save" save by click on a button or $object->save()?

Share this post


Link to post
Share on other sites

Yes exactly.

I want to do

$object->save()

for every product I have in a CSV list (with product ids or references).

Share this post


Link to post
Share on other sites

Ok I believe I understand now what you want. I don't have experience with csv files but I guess something like that should work:

$file = fopen('file.csv', 'r');
$id_product_column = x; // Define which column is id_product (first, second, or whatever)
while (($line = fgetcsv($file)) !== FALSE) {
   $id_product = $line[($id_product_column-1)];
	if ($id_product > 0) {
   		$product = new Product($id_product)
   		$product->save();
	}
}
fclose($file);

 

  • Like 1

Share this post


Link to post
Share on other sites

By the way, I hope your are testing this stuff on a testserver first. As such things could mess up your DB a lot...

Also check if you need save() or if update() is enough.

Share this post


Link to post
Share on other sites
13 hours ago, wakabayashi said:

Also check if you need save() or if update() is enough.

what are the differences between them?

I'm going to test it with a very limited list of test products.

p.s. Thank you! I'm going to give some feedback on the next days so maybe this can be useful for other thirtybees users too.

Share this post


Link to post
Share on other sites

Well save() will add new product as well. update() will only update exisiting products. I highly doubt save() makes any sense in this case...

To me this sounds all a bit complex and as I said you need to be careful. I don't understand why you have id_product at all in your csv? Where comes the csv from? 

Share this post


Link to post
Share on other sites

I have a module that auto sync product once updated, so doing that with an “external” php I hope will allow me to “force” a sync of different lists of products.

This because products that are updated from other modules like the datakick it self (or maybe “mass edit”) seams that are writing directly to the db without using the update() that unit the sync.

p.s. Maybe I’m wrong about the update() that will init the sync... I’m going to do some test next days with a couple of test products.

Share this post


Link to post
Share on other sites
On 7/5/2019 at 10:31 AM, wakabayashi said:

Well save() will add new product as well. update() will only update exisiting products. I highly doubt save() makes any sense in this case...

Looking at the code ...

https://github.com/thirtybees/thirtybees/blob/1.0.x/classes/ObjectModel.php#L538

... save() makes always sense. It does an update() for existing objects and an add() for not yet existing ones.

Share this post


Link to post
Share on other sites

@Traumflug Well it makes sense, if you want that. But as he is aksing for csv files this can lead to new entries in the db, which arent wanted. Of course depending on how the csv looks like.

Share this post


Link to post
Share on other sites
38 minutes ago, wakabayashi said:

@Traumflug Well it makes sense, if you want that. But as he is aksing for csv files this can lead to new entries in the db, which arent wanted. Of course depending on how the csv looks like.

I’m going to use that to init a save on every product id in the list. The list will include simply product ids (or reference).

I think/hope that, as the product already exist, save() will behave the same as hitting the save button in a BO product page without changing product data.

Share this post


Link to post
Share on other sites
3 hours ago, FooLab said:

I think/hope that, as the product already exist, save() will behave the same as hitting the save button in a BO product page without changing product data.

It's probably not too complex. For each CSV line do:

  1. Try to find a matching product in the database.
  2. With a match found, load it: $myProduct = new Product($idProduct);
  3. If there is no match, create a new product: $myProduct = new Product();
  4. Copy CSV data into this found/new product.
  5. $product->save();

For getting rid of products no longer in the CSV, one has to go through all products in the database and try to find a match for each in the CSV. In chunks of 10 or 100, of course.

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

×