我正在创建多个产品变体,用户可以在其中创建产品选项并为选项添加值。之后,在添加产品时,用户选择产品的选项,例如颜色,尺寸或移动ram,颜色,带有固定选项数量和价格的存储。我已经为其创建了数据库,但是我无法根据用户输入绑定与产品相关的变体。因为我在没有oop概念的情况下以最简单的php形式执行此操作,但我无法执行此操作
我已经尝试使用数组。但无法绑定:-
之类的选项options
option_id option_name
1 Size
2 Color
3 Ram
4 Inch
5 Storage
option_values
value_id option_id value_name
1 1 S
2 1 M
3 1 L
4 2 Red
5 2 Black
6 3 4GB
7 3 8GB
8 5 500GB
products_variants
product_id option_id value_id sku_var
1 1 2 SKU_a1
1 1 2 SKU_a2
1 2 4 SKU_a1
1 2 5 SKU_a2
sku_variant_vlaues
quantity price sku_var
5 100 SKU_a1
8 120 SKU_a2
//使用php发布数据
//固定选项
$product_quantity = isset($_POST['product_quantity ']) ? $_POST['product_quantity '] : '0';
$product_price = isset($_POST['product_price']) ? $_POST['product_price'] : '0';
/ ----------------------其他版本------------------- /
//此变量为($ all_options_filled),用户在其中选择要在产品变体中添加的选项,例如颜色,尺寸,夯度,存储量,英寸,宽度等。 在此变量中,所有输入字段名称都存储为field1,field2,field3
foreach($all_options_filled as $aof_field_name){
if(!empty(trim($aof_field_name))){
$other_options = $con->query("SELECT `option_id`, `option_key`, `option_name` FROM `options` WHERE `option_key`='".$aof_field_name."' AND `status`='ACTIVE' ORDER BY `position` ASC LIMIT 1");
if($other_options->num_rows>0){
$row_other_options=mysqli_fetch_array($other_options);
$roo_option_id= $row_other_options['option_id'];
$roo_option_key = $row_other_options['option_key'];
$roo_option_name = $row_other_options['option_name'];
$multiple_option_name[] = isset($_POST[$roo_option_key]) ? $_POST[$roo_option_key] : ''; // array + multiple fields ## multiple array
$multiple_option_id[] = $roo_option_id; // all posted fields id's
}
}
}
/ ----------------------其他版本------------------- /
插入产品数据后-> 获取产品的最新ID为($ product_latest_id) 以及每个产品的sku id和sku标题为($ sku_latest_id,$ sku_title)
if(!empty($multiple_option_name)){ // this variable came from other variants - those are created by user
//foreach($multiple_option_name as $one_multiple_option){
//var_dump($multiple_option_name);
for($i=0; $i<=sizeof($multiple_option_name)-1; $i++){
$multiple_option_name_1 = $multiple_option_name[$i];
$single_option_id = $multiple_option_id[$i];
if(!empty($multiple_option_name_1)){
//var_dump($one_multiple_option); echo "<br>";
if(!empty($multiple_option_name_1)){
//foreach($multiple_option_name_1 as $one_option_value){
//var_dump($one_option_value); echo "=> $single_option_id <br>";
for($j=0; $j<=sizeof($multiple_option_name_1)-1; $j++){
$one_option_value = $multiple_option_name_1[$j];
if(!empty($one_option_value)){
$get_one_option_value_id = $con->query("SELECT `value_id` FROM `option_values` WHERE `option_id`='".$single_option_id."' AND `value_name`='".$one_option_value."'");
if($get_one_option_value_id->num_rows>0){
list($g_option_value_id)=mysqli_fetch_array($get_one_option_value_id);
}else{
$goov_get_page_number=mysqli_query($con,"SELECT max(`position`) as position FROM `option_values` WHERE `option_id`='".$single_option_id."' ORDER BY `value_id` DESC LIMIT 1");
if($goov_get_page_number->num_rows==1){
$goov_row_pos = mysqli_fetch_array($goov_get_page_number);
$goov_position = $goov_row_pos['position']+1;
}else{
$goov_position = 1;
}
$create_one_option_value_id = $con->query("INSERT INTO `option_values`(`option_id`, `value_name`, `position`, `status`) VALUES
('".$single_option_id."', '".$one_option_value."', '".$goov_position."', 'ACTIVE')");
$get_latest_one_option_value_id = $con->query("SELECT `value_id` FROM `option_values`
WHERE `option_id`='".$single_option_id."' AND `value_name`='".$one_option_value."' AND `position`='".$goov_position."' AND `status`='ACTIVE'");
list($g_option_value_id)=mysqli_fetch_array($get_latest_one_option_value_id);
}
$sku_var = $sku_title."-$product_latest_id-$single_option_id-$g_option_value_id";
$insert_product_variant_values = $con->query("INSERT INTO `product_variant_values`(`product_id`, `option_id`, `value_id`, `value`, `sku_var`) VALUES
('".$product_latest_id."', '".$single_option_id."', '".$g_option_value_id."', '".$one_option_value."', '".$sku_var."')");
$p_quantity = $product_quantity[$j];
if($p_quantity>0){
$is_quantity_stock = 1;
}else{
$is_quantity_stock = 0;
}
$p_show_price = $show_price[$j];
$p_variant_currency = $admin_currency_code; // product currency
$p_price = $product_price[$j];
$p_variant_status = "ACTIVE";
$insert_sku_variant_vlaues = $con->query("INSERT INTO `sku_variant_vlaues`(`sku_id`, `product_id`, `sku_var`,
`stock`, `is_stock`, `show_price`, `currency`, `price`, `status`) VALUES ('".$sku_latest_id."', '".$product_latest_id."', '".$sku_var."',
'".$p_quantity."', '".$is_quantity_stock."', '".$p_show_price."', '".$p_variant_currency."', '".$p_price."', '".$p_variant_status."') ");
}
}
}
}
}
}
下面是用于数据库的表结构:
product table -
product_id
product_name
Sku table-
sku_id
sku_title
product_id
options table -
option_id
option_key // used as input field name in front id
option_name
option values table-
value_id
option_id
value_name
product_variant_values table -
product_variant_value_id
product_id
option_id
value_id
sku_var -> // i am using this by adding (sku title, product_id, option_id and value_id) to match it unique to (sku_variant_vlaues table) so i can identify which option is used for which quantity and price
sku_variant_vlaues table -
id - primary key
sku_id
product_id
sku_var -> same sku value from (product_variant_values) table
quantity
price
答案 0 :(得分:0)
$postData = array();
// $option_id you need to pass option value i create array so i print key you need to print object which contain the id of the option'
$option_array = [1 => 'Size' , 2=> 'Color'];
foreach ($option_array as $option_id => $value) { ?>
<input type="checkbox" name="option_type_id[<?php echo $option_id?>]" value="1">
<?php
}
$i = 0;
foreach ($option_array as $key => $option_type) {
$postData['option_type_id'][$key] = array();
$i++;
$array = $option_values[$key];
foreach ($array as $option_value_id => $value) {
$postData['option_type_id'][$key][] = $option_value_id;
?>
<input type="checkbox" name="option_type_id[<?php echo $key?>][option_values][]" value="<?php echo $option_value_id?>">
<?php }
}
$variants_data = array();
$lastInsertedProductId = 1;
$lastInsertedProductSKU = "SKU1";
foreach ($postData['option_type_id'] as $key => $value) {
foreach ($value as $option_id => $option_value) {
$variants_data[] = array(
'product_id' => $lastInsertedProductId,
'sku_var' => $lastInsertedProductSKU,
'option_id' => $key,
'option_value' => $option_value,
);
}
}
//它产生这样的结果
Array
(
[0] => Array
(
[product_id] => 1
[sku_var] => SKU1
[option_id] => 1
[option_value] => 1
)
[1] => Array
(
[product_id] => 1
[sku_var] => SKU1
[option_id] => 1
[option_value] => 2
)
[2] => Array
(
[product_id] => 1
[sku_var] => SKU1
[option_id] => 2
[option_value] => 4
)
[3] => Array
(
[product_id] => 1
[sku_var] => SKU1
[option_id] => 2
[option_value] => 6
)
)
echo "<pre>";print_r($variants_data);die;
//所以现在您可以简单地检查输出是否与您的数据库匹配
当您发布此输入内容时,您的功能将像这样工作,希望它将解决您的问题