Kershoc Posted November 13, 2015 Share Posted November 13, 2015 Hello, I am looking for a way to associate an existing product to a shop. I am developing a module that groups products into catalogs and then allows you to assign these catalogs to shops. When you assign a catalog to a shop it will get the list of products belonging to that catalog and then associate those products to the shop if that shop doesn't have an entry for the product already. I am having some issues with the actual associations. At first I thought I might be able to use the associateTo() method to accomplish this. Example: $product = new Product($id_product); $product->associateTo($shop->id); This sets up an entry in the product_shop table for the association, however that's all it does just a simple association. None of the other product data is duplicated. I've been pouring through the source but I have yet to be able to find any methods in either the product or the shop classes that would facilitate associating an existing product to a shop. I'm hoping that someone has run into this and could point me in the proper direction. With all the multishop work one would think that there would be a builtin method somewhere to easily associate a product to a new shop. Thanks! Link to comment Share on other sites More sharing options...
bdesprez Posted August 22, 2017 Share Posted August 22, 2017 Hi, I have the same need, did you find a solution ? Link to comment Share on other sites More sharing options...
veryweak Posted June 1, 2018 Share Posted June 1, 2018 Up! Did anyone found a "programmatic" easy way to duplicate a product correctly into an additional shop ? Link to comment Share on other sites More sharing options...
musicmaster Posted June 6, 2018 Share Posted June 6, 2018 Prestools has a function for that. But it is in one of the paid plugins, Link to comment Share on other sites More sharing options...
veryweak Posted June 6, 2018 Share Posted June 6, 2018 I already managed to solve it, by adding a $product->update(); instruction after doing the association and after programmatically setting the context to all shops. This "fixed" the product without data to all the associated shop. Link to comment Share on other sites More sharing options...
Ali Samie Posted August 3, 2022 Share Posted August 3, 2022 I have this code: if(ObjectModel::existsInDatabase($product->id, Product::$definition['table'])) { $status['productUpdate'] = (bool) $product->update(); } else { $product->force_id = true; $status['productAdd'] = (bool) $product->add(); } $status['productAssociateTo'][$id_shop] = (bool) $product->associateTo($id_shop); I have successfully added this product into a main shop which is with id of 1 Now I need to associate this product into another id_shop. But I do not understand what is wrong with my code. The association happens but it is incomplete. Some data like, images, categories, prices, quantities, features and attributes are not moved. It is just an empty product. Link to comment Share on other sites More sharing options...
ventura Posted August 3, 2022 Share Posted August 3, 2022 Keep in mind the store context when instantiating the object Link to comment Share on other sites More sharing options...
Ali Samie Posted August 3, 2022 Share Posted August 3, 2022 (edited) @ventura Do you mean like this? Shop::setContext(Shop::CONTEXT_SHOP, $id_shop); Edited August 3, 2022 by stifler97 (see edit history) Link to comment Share on other sites More sharing options...
Ali Samie Posted August 3, 2022 Share Posted August 3, 2022 @ventura I also tried this and failed: $this->context->shop->id_shop_list = [$id_shop_default,$id_shop]; The problem is that in the default shop with id 1 every thing is ok with the new product. But in the shop with $id_shop for example $id_shop of 4, new product is half empty after creation. It has images but the cover is missing. It has no combinations. Prices are all set to zero. And the product is out of stock too. The other thing is that I can not save the product in BO either. It shows an error which says just failed. No details. It is set to offline too. Link to comment Share on other sites More sharing options...
ventura Posted August 3, 2022 Share Posted August 3, 2022 indeed if (Shop::isFeatureActive()) { Shop::setContext(Shop::CONTEXT_SHOP, $id_shop); } Link to comment Share on other sites More sharing options...
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