Skip to content

Instantly share code, notes, and snippets.

@HDDen
Last active May 16, 2023 08:41
Show Gist options
  • Save HDDen/85afae861f52041c0cd20c6f98e89fa4 to your computer and use it in GitHub Desktop.
Save HDDen/85afae861f52041c0cd20c6f98e89fa4 to your computer and use it in GitHub Desktop.
/**
* Заходим в редактирование товара, далее в опции.
* Предсоздаём нужные, добавляем новые строки в нужном количестве.
* Затем выполняем скрипт - он лишь заполняет инпуты строк
*/
(function(){
// включаем вывод в консоль
var debug = true;
// объект "селектор": количество (если меньше - не работаем), пары 'селектор': 'значение'
var optionsObj = {
// Необходимо?
'div[id^="tab-option"] input[value="Добавить по вкусу"] ~ .form-group > .col-sm-10': {
'quantity': 1,
'select': 0,
},
// Опция "Добавить по вкусу"
'div[id^="tab-option"] input[value="Добавить по вкусу"] ~ .table-responsive tbody': {
// служебные
'quantity': 11,
// далее общие
'tr td:nth-child(2) input': 1000, // Количество
'tr td:nth-child(3) select': 0, // Вычитать со склада
// затем персональные
'tr:nth-child(1) td:nth-child(1) select': 134, // id добавки
'tr:nth-child(1) td:nth-child(4) input': '69.0000', // Цена
'tr:nth-child(1) td:nth-child(6) input': '50.0000', // Вес
'tr:nth-child(2) td:nth-child(1) select': 138,
'tr:nth-child(2) td:nth-child(4) input': '49.0000',
'tr:nth-child(2) td:nth-child(6) input': '30.0000',
'tr:nth-child(3) td:nth-child(1) select': 131,
'tr:nth-child(3) td:nth-child(4) input': '59.0000',
'tr:nth-child(3) td:nth-child(6) input': '30.0000',
'tr:nth-child(4) td:nth-child(1) select': 135,
'tr:nth-child(4) td:nth-child(4) input': '29.0000',
'tr:nth-child(4) td:nth-child(6) input': '10.0000',
'tr:nth-child(5) td:nth-child(1) select': 139,
'tr:nth-child(5) td:nth-child(4) input': '49.0000',
'tr:nth-child(5) td:nth-child(6) input': '40.0000',
'tr:nth-child(6) td:nth-child(1) select': 132,
'tr:nth-child(6) td:nth-child(4) input': '29.0000',
'tr:nth-child(6) td:nth-child(6) input': '15.0000',
'tr:nth-child(7) td:nth-child(1) select': 136,
'tr:nth-child(7) td:nth-child(4) input': '59.0000',
'tr:nth-child(7) td:nth-child(6) input': '30.0000',
'tr:nth-child(8) td:nth-child(1) select': 140,
'tr:nth-child(8) td:nth-child(4) input': '99.0000',
'tr:nth-child(8) td:nth-child(6) input': '40.0000',
'tr:nth-child(9) td:nth-child(1) select': 133,
'tr:nth-child(9) td:nth-child(4) input': '79.0000',
'tr:nth-child(9) td:nth-child(6) input': '50.0000',
'tr:nth-child(10) td:nth-child(1) select': 137,
'tr:nth-child(10) td:nth-child(4) input': '89.0000',
'tr:nth-child(10) td:nth-child(6) input': '40.0000',
'tr:nth-child(11) td:nth-child(1) select': 100,
'tr:nth-child(11) td:nth-child(4) input': '79.0000',
'tr:nth-child(11) td:nth-child(6) input': '50.0000',
},
// Необходимо?
'div[id^="tab-option"] input[value="Убрать ингредиенты"] ~ .form-group > .col-sm-10': {
'quantity': 1,
'select': 0,
},
// Опция "Убрать ингредиенты"
'div[id^="tab-option"] input[value="Убрать ингредиенты"] ~ .table-responsive tbody': {
// служебные
'quantity': 10,
// далее общие
'tr td:nth-child(2) input': 1000, // Количество
'tr td:nth-child(3) select': 0, // Вычитать со склада
// затем персональные
'tr:nth-child(1) td:nth-child(1) select': 81, // id добавки
'tr:nth-child(2) td:nth-child(1) select': 85,
'tr:nth-child(3) td:nth-child(1) select': 75,
'tr:nth-child(4) td:nth-child(1) select': 80,
'tr:nth-child(5) td:nth-child(1) select': 87,
'tr:nth-child(6) td:nth-child(1) select': 89,
'tr:nth-child(7) td:nth-child(1) select': 92,
'tr:nth-child(8) td:nth-child(1) select': 70,
'tr:nth-child(9) td:nth-child(1) select': 68,
'tr:nth-child(10) td:nth-child(1) select': 83,
},
};
/////////////////////////////////////////////////////////
// дальше не редактируем, удачи!
// стартуем
/////////////////////////////////////////////////////////
if (debug) console.log('OpenCart optsFiller: Стартовали');
// начинаем перебор основных селекторов - т.е. ищем опции
for (var selector in optionsObj){
var parent = document.querySelector(selector);
if (!parent){
if (debug) console.log('%cOpenCart optsFiller: Не найден элемент по селектору '+selector+', пропускаем', 'color: red');
continue;
}
// проверяем количество потомков
if (!(optionsObj[selector].quantity)){
if (debug) console.log('%cOpenCart optsFiller: Для селектора '+selector+' не указано свойство "quantity". Пропускаем', 'color: red');
continue;
}
if (parent.childElementCount != optionsObj[selector].quantity){
if (debug) console.log('%cOpenCart optsFiller: Для селектора '+selector+' количество потомков не равно эталону в "quantity" ('+parent.childElementCount+', а должно '+optionsObj[selector].quantity+'). Пропускаем', 'color: red');
continue;
}
// всё норм - перебираем элементы
if (debug) console.log('OpenCart optsFiller: Начинаем перебор детей '+selector);
for (var childSelector in optionsObj[selector]){
// служебные строки пропускаем
if (childSelector == 'quantity') continue;
//if (debug) console.log('OpenCart optsFiller: optionsObj[selector], childSelector', optionsObj[selector], childSelector);
var child = parent.querySelectorAll(childSelector);
if (!child.length){
if (debug) console.log('%cOpenCart optsFiller: Дочерних '+childSelector+' не существует, пропускаем', 'color: red', child);
continue;
}
// устанавливаем значение
if (debug) console.log('OpenCart optsFiller: Устанавливаем значение "'+optionsObj[selector][childSelector]+'" для '+childSelector, child);
child.forEach(function(el){
if (el.tagName == 'SELECT'){
el.value = optionsObj[selector][childSelector];
el.dispatchEvent(new Event('change'));
coloring(el, 'good');
} else if ( (el.tagName == 'INPUT') && (el.getAttribute('type') == 'text') ){
el.value = optionsObj[selector][childSelector];
el.dispatchEvent(new Event('change'));
coloring(el, 'good');
} else {
coloring(el, 'bad');
}
});
}
if (debug) console.log('OpenCart optsFiller: закончили перебор потомков '+selector);
}
if (debug) console.log('OpenCart optsFiller: перебрали все селекторы, завершаемся');
// Вспомогательные функции
function coloring(what, status){
var elem = what;
if (status == 'good'){
elem.style.outline = '4px solid green';
}else if(status == 'medium'){
elem.style.outline = '4px solid yellow';
}else{
elem.style.outline = '4px solid red';
}
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment