Jump to content
thirty bees forum

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


Recommended Posts

Posted

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?

Posted

Yes exactly.

I want to do

$object->save()

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

Posted

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
Posted

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.

Posted
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.

Posted

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? 

Posted

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.

Posted
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.

Posted
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.

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...