Jump to content

How to get all shops associated to a product in multishop?


Recommended Posts

Hello guys. I hope you are well.

In multistore i need to copy a product from Store A to Store B. Everything is almost okay! Copy products works. But i want to prevent the copy of products if the copy already exists in database. Let me more precise:

If a product from Store A is copied to Store B, but the product already exists in Store B, then the product in store B is overrided. And i would like to prevent it! So i need to check in which stores the product already exist before the copy.

Here my code in my controller:

    $id_product = $product_source->id;
    // If product source object is valid then the product exist in the default shop. $product_source is a product object.
    if (Validate::isLoadedObject($product_source)) { 
        // or ObjectModel::existsInDatabase($id_product, Product::$definition['table'])
        // or Product::existsInDatabase($id_product)
        // I think theses 3 methods does the same thing.

        // Now, it's possible to create the product clone object
        // My code to create the cloned product goes here and works perfectly but maybe the product already exists in the targeted shop so i need 
		// to check it before copying the data.
    } else {
        // No product id found in the database (product doesn't exist, we can't clone it)
        $product_error = $this->trans("Product doesn't exists anywhere. It can't be cloned to the targeted shop", "Modules.Mymodule");
    }

 

So, how can i get all stores associated to my product? Cause when i check if the product exists in database, it return only one result: The default id shop. Not all its id shops.

Edited by Titi2 (see edit history)
Link to comment
Share on other sites

  • Titi2 changed the title to How to get all shops associated to a product in multishop?

Ok, just found the solution. I share it. It may help someone too.

The Product class has a getShopsByProduct function.

// Return an array of shops ids containing the product
$product_shops = Product::getShopsByProduct($product_id); 

// Only if you need to know if the product is in a specific shop
$my_id_shop = 2;

if (array_search($my_id_shop, array_column($product_shops, 'id_shop'))) {
	// Do something if product found in my_id_shop
}

 

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