Jump to content

Associate Existing Product to New Shop


Kershoc

Recommended Posts

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

  • 1 year later...
  • 9 months later...

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

  • 4 years later...

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

@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

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