Beeta Posted July 3, 2019 Posted July 3, 2019 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?
wakabayashi Posted July 3, 2019 Posted July 3, 2019 I don't understand what you want achieve. And what means "save" save by click on a button or $object->save()?
Beeta Posted July 4, 2019 Author Posted July 4, 2019 Yes exactly. I want to do $object->save() for every product I have in a CSV list (with product ids or references).
wakabayashi Posted July 4, 2019 Posted July 4, 2019 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); 1
wakabayashi Posted July 4, 2019 Posted July 4, 2019 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.
Beeta Posted July 5, 2019 Author Posted July 5, 2019 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.
wakabayashi Posted July 5, 2019 Posted July 5, 2019 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?
Beeta Posted July 5, 2019 Author Posted July 5, 2019 9 hours ago, wakabayashi said: Where comes the csv from? From @datakick‘s module.
Beeta Posted July 5, 2019 Author Posted July 5, 2019 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.
Traumflug Posted July 6, 2019 Posted July 6, 2019 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.
wakabayashi Posted July 6, 2019 Posted July 6, 2019 @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.
Beeta Posted July 6, 2019 Author Posted July 6, 2019 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.
Traumflug Posted July 6, 2019 Posted July 6, 2019 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: Try to find a matching product in the database. With a match found, load it: $myProduct = new Product($idProduct); If there is no match, create a new product: $myProduct = new Product(); Copy CSV data into this found/new product. $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.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now