host, $j->user, $j->password, $j->db); /* check connection */ if ($mysqli->connect_errno) { printf("Connect failed: %s\n", $mysqli->connect_error); exit(); } // get id for all products with children $sql="SELECT DISTINCT(`product_parent_id`) AS `product_id` FROM `{$j->dbprefix}hikashop_product` WHERE `product_parent_id` != 0"; $mysqli->multi_query($sql) or die($mysqli->error); $idsRec = $mysqli->store_result() or die($mysqli->error); while($id = $idsRec->fetch_object()){ //get product's details $sql="SELECT * FROM `{$j->dbprefix}hikashop_product` WHERE `product_id` = {$id->product_id}"; $mysqli->multi_query($sql) or die($mysqli->error); $fatherRec = $mysqli->store_result() or die($mysqli->error); $father = $fatherRec->fetch_object(); // create charateristic into hikashop_characteristic $sql="INSERT INTO `{$j->dbprefix}hikashop_characteristic` (`characteristic_parent_id`, `characteristic_value`, `characteristic_alias`) VALUES (0, '%s', '%s' )"; $sql=sprintf($sql, $mysqli->real_escape_string($father->product_name), $mysqli->real_escape_string('Varianti per ' . $father->product_code)); $mysqli->query($sql) or die($mysqli->error); $fatherVariantId = $mysqli->insert_id; // get all children for product $sql="SELECT * FROM `{$j->dbprefix}hikashop_product` WHERE `product_parent_id` = {$id->product_id} ORDER BY `product_name`"; $mysqli->multi_query($sql) or die($mysqli->error); $childrenRec = $mysqli->store_result() or die($mysqli->error); $firstVariant = true; while($child = $childrenRec->fetch_object()){ //create a variant for every children $sql="INSERT INTO `{$j->dbprefix}hikashop_characteristic` (`characteristic_parent_id`, `characteristic_value`, `characteristic_alias`) VALUES ({$fatherVariantId}, '%s', '' )"; $sql=sprintf($sql, $mysqli->real_escape_string($child->product_name)); $mysqli->query($sql) or die($mysqli->error); $childVariantId = $mysqli->insert_id; //create the relation between variant and product for every children if($firstVariant){ // relation to father charateristic for father product $sql="INSERT INTO `{$j->dbprefix}hikashop_variant`(`variant_characteristic_id`, `variant_product_id`, `ordering`) VALUES ({$fatherVariantId}, {$id->product_id}, 1)"; $mysqli->query($sql) or die($mysqli->error); // relation to default charateristic for father product $sql="INSERT INTO `{$j->dbprefix}hikashop_variant`(`variant_characteristic_id`, `variant_product_id`, `ordering`) VALUES ({$childVariantId}, {$id->product_id}, 0)"; $mysqli->query($sql) or die($mysqli->error); $firstVariant=false; } // all children are variant! $sql="UPDATE `{$j->dbprefix}hikashop_product` SET `product_type` = 'variant' WHERE `product_id` = {$child->product_id}"; $mysqli->query($sql) or die($mysqli->error); // set relation $sql="INSERT INTO `{$j->dbprefix}hikashop_variant`(`variant_characteristic_id`, `variant_product_id`, `ordering`) VALUES ({$childVariantId}, {$child->product_id}, 0)"; $mysqli->query($sql) or die($mysqli->error); } $childrenRec->close(); $fatherRec->close(); } $idsRec->close(); $mysqli->close(); echo ("Code ran, !!! DON'T RELOAD !!!"); ?>