get_results("SELECT * FROM `".WPSC_TABLE_PRODUCT_VARIATIONS."` ORDER BY `id` ASC",ARRAY_A); $options .= "
".print_r($variation_values,true).""); //exit("
".print_r($_POST,true).""); foreach($variation_values as $form_key => $variation_data) { // split input $keys = explode(",",$form_key); // sanitise input array_walk($keys, 'wpsc_sanitise_keys'); $variation_ids = $wpdb->get_col("SELECT `variation_id` FROM `".WPSC_TABLE_VARIATION_VALUES."` WHERE `id` IN ('".implode("','",$keys)."')"); asort($variation_ids); $all_variation_ids = implode(",", $variation_ids); $variation_price = (float)str_replace(",","",$variation_data['price']); $variation_stock =(int)$variation_data['stock']; // having 1.2 stock makes no sense unless dealing with kilograms or some other such measurement $variation_weight = (float)$variation_data['weight']; $variation_weight_unit = (string)$variation_data['weight_unit']; $variation_file =(int)$variation_data['file']; $variation_visibility =(int)(bool)$variation_data['visibility']; if((is_numeric($variation_data['stock']) || is_numeric($variation_price))) { $priceandstock_id = $wpdb->get_var("SELECT `priceandstock_id` FROM `".WPSC_TABLE_VARIATION_COMBINATIONS."` WHERE `product_id` = '$product_id' AND `value_id` IN ( '".implode("', '",$keys )."' ) AND `all_variation_ids` IN('$all_variation_ids') GROUP BY `priceandstock_id` HAVING COUNT( `priceandstock_id` ) = '".count($keys)."' LIMIT 1"); $variation_stock_data = $wpdb->get_row("SELECT * FROM `".WPSC_TABLE_VARIATION_PROPERTIES."` WHERE `id` = '{$priceandstock_id}' LIMIT 1", ARRAY_A); // if the associated row has already been created if(is_numeric($variation_stock_data['id'])) { foreach($keys as $key) { // if create the records to associate it with a product if they are not present if($wpdb->get_var("SELECT COUNT(*) FROM `".WPSC_TABLE_VARIATION_COMBINATIONS."` WHERE `priceandstock_id` = '{$variation_stock_data['id']}' AND `value_id` = '$key'") < 1) { $variation_id = $wpdb->get_var("SELECT `".WPSC_TABLE_VARIATION_VALUES."`.`variation_id` FROM `".WPSC_TABLE_VARIATION_VALUES."` WHERE `id` = '{$key}'"); $wpdb->query("INSERT INTO `".WPSC_TABLE_VARIATION_COMBINATIONS."` ( `product_id` , `priceandstock_id` , `value_id`, `variation_id`, `all_variation_ids` ) VALUES ( '$product_id', '{$variation_stock_data['id']}', '$key', '$variation_id', '$all_variation_ids' );"); } } // and start building the SQL query to edit the item $variation_sql = null; // return the sql array to null for each trip round the loop if(($variation_stock_data['stock'] != $variation_stock)) { $variation_sql[] = "`stock` = '{$variation_stock}'"; } if(($variation_stock_data['price'] != $variation_price)) { $variation_sql[] = "`price` = '{$variation_price}'"; } if(($variation_stock_data['weight'] != $variation_weight)) { $variation_sql[] = "`weight` = '{$variation_weight}'"; } if(($variation_stock_data['weight_unit'] != $variation_weight_unit)) { $variation_sql[] = "weight_unit = '{$variation_weight_unit}'"; } if(($variation_stock_data['file'] != $variation_file)) { $variation_sql[] = "`file` = '{$variation_file}'"; } // if there is any SQL to execute, make it so if($variation_sql != null) { $wpdb->query("UPDATE `".WPSC_TABLE_VARIATION_PROPERTIES."` SET ".implode(",",$variation_sql)."WHERE `id` = '{$variation_stock_data['id']}' LIMIT 1 ;"); } } else { // otherwise, the price and stock row does not exist, make it $wpdb->query("INSERT INTO `".WPSC_TABLE_VARIATION_PROPERTIES."` ( `product_id` , `stock`, `price`, `weight`, `weight_unit`, `file` ) VALUES ('{$product_id}', '{$variation_stock}', '{$variation_price}', '{$variation_weight}', '{$variation_weight_unit}', '{$variation_file}');"); $variation_priceandstock_id = $wpdb->get_var("SELECT LAST_INSERT_ID() FROM `".WPSC_TABLE_VARIATION_PROPERTIES."` LIMIT 1"); foreach($keys as $key) { // then make sure it is associated with the product and variations. if($wpdb->get_var("SELECT COUNT(*) FROM `".WPSC_TABLE_VARIATION_COMBINATIONS."` WHERE `priceandstock_id` = '{$variation_priceandstock_id}' AND `value_id` = '$key'") < 1) { $variation_id = $wpdb->get_var("SELECT `".WPSC_TABLE_VARIATION_VALUES."`.`variation_id` FROM `".WPSC_TABLE_VARIATION_VALUES."` WHERE `id` = '{$key}'"); $wpdb->query("INSERT INTO `".WPSC_TABLE_VARIATION_COMBINATIONS."` ( `product_id` , `priceandstock_id` , `value_id`, `variation_id`, `all_variation_ids` ) VALUES ( '$product_id', '{$variation_priceandstock_id}', '$key', '$variation_id', '$all_variation_ids' );"); } } } } } //foreach ends here //exit(); } //function ends here } ?>