Jump to content

Share product to another shop preserving ID


50l3r

Recommended Posts

Hi guys,

I want to create a function to clone product from shop1 to shop2 preserving the same product id. Currently the only way to share products is at the time of creating a new store.

Maybe getting a product and create new one? Currently i have this part of code to get the product:

public function ajaxProcessCloneProduct(){
  	if(Calltek::hasPermissions('AdminCalltekProducts')){
		$id = !empty(Tools::getValue('id')) ? Tools::getValue('id') : null;

		if($id){
			$product = new Product($id);
			var_dump($product);
		}

		die(Calltek::JsonResponse(400, 'Parámetros incorrectos'));
  	}

	die(Calltek::JsonResponse(401, 'Acceso denegado'));
}

Thanks in advance

Link to comment
Share on other sites

1 hour ago, ndiaga said:

Hi,

You  can  do  it  with  the  API   if  shop 1   use  PrestaShop  and  shop 2  use  a  standard  PHP  or  another framework.

But  you  can  achieve  this  of  both  are  PrestaShop  installations.

I refer to the native multistore system of PrestaShop.

I created a function who pass product id and array of shop ids and reassign the product to another shops.

function assignProduct($id_product, $shops){
    $currentShop = Context::getContext()->shop->id;
    
    if($id_product && $shops){
        //Check if product exists on X shops
        $sql = "SELECT id_shop FROM `"._DB_PREFIX_."product_shop` ps WHERE id_product = '".$id_product."'";
        $query = Db::getInstance()->executeS($sql);
        $product = new Product($id_product);

        if($query && $product){
            $id_shop_default = $product->id_shop_default;
            $productShops = array_map(function($s){ return $s['id_shop']; }, $query);

            $delete = [];
            $add = [];

            // Populate array with new shops
            foreach($shops as $shop){
                if(!in_array($shop, $productShops)){
                    $add[] = $shop;
                }
            }

            // Populate array with old shops
            foreach($productShops as $shop){
                if(!in_array($shop, $shops)){
                    $delete[] = $shop;
                }
            }


            // Associate products with new shops
            $product->id_shop_list = $add;
            $product->update();
            
            foreach($delete as $shop){
                // Prevent remove product from original shop
                if($shop===$id_shop_default) continue;

                // Remove product attributes
                $sql = "DELETE FROM `"._DB_PREFIX_."product_attribute_shop` WHERE id_product = \"".$id_product."\" AND id_shop = \"".$shop."\"";
                Db::getInstance()->Execute($sql);

                // Remove images
                $sql = "DELETE FROM `"._DB_PREFIX_."image_shop` WHERE id_product = \"".$id_product."\" AND id_shop = \"".$shop."\"";
                Db::getInstance()->Execute($sql);

                // Remove customizations
                $sql = "DELETE FROM `"._DB_PREFIX_."customization_field_lang` WHERE id_shop = \"".$shop."\" AND id_customization_field IN (SELECT id_customization_field FROM `"._DB_PREFIX_."customization_field` WHERE id_product = \"".$id_product."\")";
                Db::getInstance()->Execute($sql);
                
                // Remove product
                $sql = "DELETE FROM `"._DB_PREFIX_."product_shop` WHERE id_product = \"".$id_product."\" AND id_shop = \"".$shop."\"";
                Db::getInstance()->Execute($sql);
            }

            foreach($add as $shop){
                // Duplicate attributes
                $sql = "INSERT INTO `"._DB_PREFIX_."product_attribute_shop` (id_product, id_product_attribute, id_shop, wholesale_price, price, ecotax, weight, unit_price_impact, default_on, minimal_quantity, low_stock_threshold, low_stock_alert, available_date, ctk_price, ctk_hidden) SELECT id_product, id_product_attribute, \"".$shop."\", wholesale_price, price, ecotax, weight, unit_price_impact, default_on, minimal_quantity, low_stock_threshold, low_stock_alert, available_date, ctk_price, ctk_hidden FROM `"._DB_PREFIX_."product_attribute_shop` WHERE id_product = \"".$id_product."\" AND id_shop = \"".$currentShop."\"";
                Db::getInstance()->Execute($sql);

                // Duplicate image
                $sql = "INSERT INTO `"._DB_PREFIX_."image_shop` (id_product, id_image, id_shop, cover) SELECT id_product, id_image, \"".$shop."\", cover FROM `"._DB_PREFIX_."image_shop` WHERE id_product = \"".$id_product."\" AND id_shop = \"".$currentShop."\"";
                Db::getInstance()->Execute($sql);

                // Duplicate customization
                $sql = "INSERT INTO `"._DB_PREFIX_."customization_field_lang` (id_customization_field, id_lang, id_shop, name) SELECT id_customization_field, id_lang, \"".$shop."\", name FROM `"._DB_PREFIX_."customization_field_lang` WHERE id_customization_field IN (SELECT id_customization_field FROM `"._DB_PREFIX_."customization_field` WHERE id_product = \"".$id_product."\") AND id_shop = \"".$currentShop."\"";
                Db::getInstance()->Execute($sql);

                // Disable product
                $sql = "UPDATE `"._DB_PREFIX_."product_shop` set active = 0 WHERE id_product = \"".$id_product."\" AND id_shop = \"".$shop."\"";
                Db::getInstance()->Execute($sql);
            }
            
            die(Calltek::JsonResponse(200));
            
        }else{
            die(Calltek::JsonResponse(500, 'Ocurrió un error inesperado'));
        }
    }else{
        die(Calltek::JsonResponse(400, 'Parámetros incorrectos'));
    }
}

 

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