На первом этапе я показываю все атрибуты из серверной части
Шаг 1. Выберите атрибуты

(Здесь я отобразил все атрибуты из серверной части)
Код моего представления:
Код: Выделить всё
@foreach($attributes as $attribute)
{{$attribute->name}}
@endforeach
Next
Код Ajax:
Код: Выделить всё
$('#stepOne').click(function(e){
e.preventDefault();
var attribute = "";
$(":checkbox").each(function () {
var ischecked = $(this).is(":checked");
if (ischecked) {
attribute += $(this).val() + ",";
}
});
$.ajax({
url: "/configStepOne",
type:"POST",
data:{
"_token": "{{ csrf_token() }}",
attribute:attribute,
},
success:function(response){
$('#successMsg').show();
$('#stepOneResponse').append(response.html);
},
error: function(response) {
console.log(response.responseJSON.message);
},
});
});
Код: Выделить всё
public function configStepOne(Request $request) {
if (!is_null(Session::get('attributes'))) {
Session::forget('attributes');
}
Session::put('attributes', $request->attribute);
$config_attribute_array = [];
if (!is_null(Session::get('attributes'))) {
$config_attribute = Session::get('attributes');
$config_attribute = substr($config_attribute, 0, -1);
$config_attribute_array = explode(',', $config_attribute);
}
$html = "";
foreach($config_attribute_array as $value) {
$config_attr = Attributee::findOrFail($value);
$html .= '
'.$config_attr->name.'
';
foreach($config_attr->options as $option) {
$html .= '';
$html .= '';
$html .= ''.$option->option.'';
$html .= '';
}
$html .= '
';
}
return response()->json(['success'=>'Successfully', 'html' => $html]);
}
Шаг 2: значения атрибутов< /strong>

В моем представлении клинка у меня есть:
Код: Выделить всё
Next
Код: Выделить всё
Color
Red
Black
Blue
Green
Size
XL
SM
medium
Extra small
Format
Download
DVD
Next
Код: Выделить всё
$('#stepTwo').click(function(e){
e.preventDefault();
var option = "";
var option_id = "";
$("#stepOneResponse :checkbox").each(function () {
var ischecked = $(this).is(":checked");
if (ischecked) {
option += $(this).val() + ",";
option_id += $(this).attr('id') + ",";
}
});
$.ajax({
url: "/configStepTwo",
type:"POST",
data:{
"_token": "{{ csrf_token() }}",
option:option,
option_id:option_id,
},
success:function(response){
$('#successMsg').show();
console.log(response);
$('#stepTwoImageResponse').append(response.imageHtml);
$('#stepTwoPriceResponse').append(response.priceHtml);
$('#stepTwoQtyResponse').append(response.qtyHtml);
$('#count').text(response.count);
},
error: function(response) {
console.log(response.responseJSON.message);
},
});
});
Код: Выделить всё
public function configStepTwo(Request $request) {
if (!is_null(Session::get('options'))) {
// Session::forget('option');
}
Session::put('options', $request->option);
Session::put('options_id', $request->option_id);
$config_attribute = "";
$config_attribute_opt_string = "";
$config_attribute_array = [];
$config_attribute_opt_array = [];
$config_attribute_opt_array_id = [];
if (!is_null(Session::get('attributes'))) {
$config_attribute = Session::get('attributes');
$config_attribute = substr($config_attribute, 0, -1);
$config_attribute_array = explode(',', $config_attribute);
// dd($config_attribute_array);
}
if (!is_null(Session::get('options'))) {
$config_attribute_opt = Session::get('options');
$config_attribute_opt = substr($config_attribute_opt, 0, -1);
$config_attribute_opt_array = explode(',', $config_attribute_opt);
// dd($config_attribute_opt_array);
}
if (!is_null(Session::get('options_id'))) {
$config_attribute_opt_id = Session::get('options_id');
// $config_attribute_opt_id = strstr($config_attribute_opt_id, '-', true);
$config_attribute_opt_id = substr($config_attribute_opt_id, 0, -1);
$config_attribute_opt_array_id = explode(',', $config_attribute_opt_id);
}
foreach ($config_attribute_opt_array_id as $key => $value) {
$config_attribute_opt_string .= strstr($value, '-', true). ',';
// $config_attribute_opt_string .= $value. ',';
}
$config_attribute_opt_string = substr($config_attribute_opt_string, 0, -1);
$config_attribute_opt_array_id = explode(',', $config_attribute_opt_string);
$array_count = array_count_values($config_attribute_opt_array_id);
$count = 1;
foreach ($array_count as $key => $value) {
$count *= $value;
}
Session::put('count', $count);
$imageHtml = "";
$imageHtml .= '
Select attribute
Select';
foreach($config_attribute_array as $value) {
$config_attr = Attributee::findOrFail($value);
$imageHtml .= ''.$config_attr->name.'';
}
$imageHtml .= '';
foreach($config_attribute_opt_array as $value) {
$config_opt = AttributeOption::findOrFail($value);
$imageHtml .= '';
$imageHtml .= ''. $config_opt->option.'';
$imageHtml .= '';
$imageHtml .= '';
}
$imageHtml .= '';
$priceHtml = "";
$priceHtml .= '
Select attribute
Select';
foreach($config_attribute_array as $value) {
$config_attr = Attributee::findOrFail($value);
$priceHtml .= ''.$config_attr->name.'';
}
$priceHtml .= '';
foreach($config_attribute_opt_array as $value) {
$config_opt = AttributeOption::findOrFail($value);
$priceHtml .= '';
$priceHtml .= ''. $config_opt->option.'';
$priceHtml .= '';
$priceHtml .= '';
}
$priceHtml .= '';
$qtyHtml = "";
$qtyHtml .= '
Select attribute
Select';
foreach($config_attribute_array as $value) {
$config_attr = Attributee::findOrFail($value);
$qtyHtml .= ''.$config_attr->name.'';
}
$qtyHtml .= '';
foreach($config_attribute_opt_array as $value) {
$config_opt = AttributeOption::findOrFail($value);
$qtyHtml .= '';
$qtyHtml .= ''. $config_opt->option.'';
$qtyHtml .= '';
$qtyHtml .= '';
}
$qtyHtml .= '';
return response()->json(['success'=>'Successfully',
'imageHtml' => $imageHtml,
'priceHtml' => $priceHtml,
'qtyHtml' => $qtyHtml,
'count' => $count,
]);
}
< strong>Шаг 3: Массовые изображения, цена и количество[/b]

После выбора «Применять уникальную цену по атрибуту для каждого товара»
[img]https://i .sstatic.net/x6jjO.png[/img]
Просмотреть файл
Код: Выделить всё
Price
Apply single of price to all SKUs
Apply unique prices by attribute to each SKU
Skip price at this time
Next
Здесь я попытался добавить все параметры выбора атрибута.. и когда пользователь выбирает атрибут, соответственно появляется другой раздел ввода, где пользователь может ввести значение параметра атрибута
Когда пользователь нажимает «Далее»
Ajax вызывает код с кодом
Код: Выделить всё
$('#stepThree').click(function(e){
e.preventDefault();
var option = "";
$("input[name='option_price[]']").each(function () {
if($(this).val()) {
option += $(this).val() + ",";
}
});
console.log(option);
$.ajax({
url: "/configStepThree",
type:"POST",
data:{
"_token": "{{ csrf_token() }}",
option:option,
},
success:function(response){
$('#successMsg').show();
console.log(response);
$("#tabImageNav").removeClass("active");
$("#tabImage").removeClass("active");
$('#tabDetailNav').addClass('active');
$('#tabDetail').addClass('active');
$('#table').append(response.table);
},
error: function(response) {
// $('#nameErrorMsg').text(response.responseJSON.errors.message);
console.log(response.responseJSON.message);
},
});
});
});
In configStepThree method from controller
public function configStepThree(Request $request) {
if (!is_null(Session::get('mulit_options'))) {
// Session::forget('mulit_options');
}
Session::put('mulit_options', $request->option);
$config_attribute = "";
$config_attribute_opt_string = "";
$config_attribute_array = [];
$config_attribute_opt_array = [];
$config_attribute_opt_array_id = [];
if (!is_null(Session::get('attributes'))) {
$config_attribute = Session::get('attributes');
$config_attribute = substr($config_attribute, 0, -1);
$config_attribute_array = explode(',', $config_attribute);
}
if (!is_null(Session::get('options'))) {
$config_attribute_opt = Session::get('options');
$config_attribute_opt = substr($config_attribute_opt, 0, -1);
$config_attribute_opt_array = explode(',', $config_attribute_opt);
}
if (!is_null(Session::get('options_id'))) {
$config_attribute_opt_id = Session::get('options_id');
$config_attribute_opt_id = substr($config_attribute_opt_id, 0, -1);
$config_attribute_opt_array_id = explode(',', $config_attribute_opt_id);
}
foreach ($config_attribute_opt_array_id as $key => $value) {
$config_attribute_opt_string .= strstr($value, '-', true). ',';
}
$count = Session::get('count');
$table = "";
$table .= '';
$table .= '';
$table .= '';
$table .= 'Image';
$table .= 'SKU';
$table .= 'Quantity';
foreach ($config_attribute_array as $key => $attribute) {
$config_attr = Attributee::findOrFail($attribute);
$table .= ''.$config_attr->name.'';
}
$table .= 'Price';
$table .= '';
$table .= '';
$table .= '';
//This is what I want as collection
$collection = [
[
'Color' => 'Red',
'Size' => 'XL',
'Format' => 'Download',
'price' => '200',
'qty' => '1',
'image' => 'path',
'image_count' => '0'
],
[
'Color' => 'Red',
'Size' => 'SM',
'Format' => 'Download',
'price' => '200',
'qty' => '1',
'image' => 'path',
'image_count' => '0'
],
[
'Color' => 'Black',
'Size' => 'XL',
'Format' => 'Download',
'price' => '300',
'qty' => '1',
'image' => 'path',
'image_count' => '0'
],
[
'Color' => 'Black',
'Size' => 'SM',
'Format' => 'Download',
'price' => '300',
'qty' => '1',
'image' => 'path',
'image_count' => '0'
]
];
// dd($collection);
// for ($i=1; $i $value) {
$table .= '';
$table .= '
[img]../assets/img/thumbnail.jpg[/img]
'.$value['image_count'].'';
$table .= 'Simple but not simpler';
$table .= ''.$value['qty'].'';
foreach ($config_attribute_array as $key => $attribute) {
$config_attr = Attributee::findOrFail($attribute);
$table .= ''.$value[''.$config_attr->name.''].'';
}
$table .= ''.$value['price'].'';
$table .='';
}
// }
$table .= '';
$table .= '';
return response()->json(['success'=>'Successfully',
'table' => $table,
]);
}

просмотр кода файла лезвия
Здесь я хочу отобразить все значения, соответствующие предыдущие шаги.
Примечание
Код: Выделить всё
$collection = [
[
'Color' => 'Red',
'Size' => 'XL',
'Format' => 'Download',
'price' => '200',
'qty' => '1',
'image' => 'path',
'image_count' => '0'
],
[
'Color' => 'Red',
'Size' => 'SM',
'Format' => 'Download',
'price' => '200',
'qty' => '1',
'image' => 'path',
'image_count' => '0'
],
[
'Color' => 'Black',
'Size' => 'XL',
'Format' => 'Download',
'price' => '300',
'qty' => '1',
'image' => 'path',
'image_count' => '0'
],
[
'Color' => 'Black',
'Size' => 'SM',
'Format' => 'Download',
'price' => '300',
'qty' => '1',
'image' => 'path',
'image_count' => '0'
]
];
Пожалуйста, не судите меня по моему коду. Я немного новичок в этом, и мой код не чист, и я повторил код, не соответствующий принципу DRY.
Подробнее здесь: https://stackoverflow.com/questions/720 ... using-ajax