Некодим логотипНекодим
МодификацииПубликацииНекодИИмЦены
Некодим логотипНекодим

© 2020-2026 Некодим

Готовые модификации для вашего сайта. Улучшайте функциональность и дизайн без программирования.

Компания

  • Контакты
  • Публичная оферта
  • Политика конфиденциальности
  • Согласие на обработку данных
  • Лицензионное соглашение
  • Реферальная программа

Полезное

  • Модификации
  • Публикации
  • Каталог блоков Tilda
  • MCP для агентов
  • Журнал изменений
  • Доска почёта

Личный кабинет

  • Вход
  • Регистрация
  • Цены
Журнал измененийИстория GitФайлыTilda MCP
Каталог файлов

tilda-products-1.0.js

Код файла
Версия от 13.10.2026, 07:01:03 МСК
Оригинал на CDNСнимок 56c3b845Полный текст
1/*2 Устанавливает цену в блоке товара при добавлении параметров3 При добавлении нескольких параметров добавляет обработчик для select и option4 Выбрав определенный параметр изменяет цену у продукта5*/6 7/**8 * Init products9 *10 * @param {string} recId - rec id11 * @returns {undefined} - exit function12 */13
История файла и сравнение версий
  • 13.10.2026, 07:01:03 · 56c3b845
14function t_prod__init(recId) {
15 var rec = document.querySelector('#rec' + recId);
16 if (!rec) return;
17
18 var productElements = rec.querySelectorAll('.js-product');
19
20 Array.prototype.forEach.call(productElements, function (productElement) {
21 if (productElement.querySelector('.js-product-option, .js-product-multioption')) {
22 t_prod__initProduct(productElement);
23 }
24 });
25}
26
27/**
28 * Init product card
29 *
30 * @param {string} recId - rec id
31 * @param {Node} productElement - product card
32 */
33function t_prod__initProduct(productElement, options) {
34 let element = productElement;
35 if (typeof element === 'string') {
36 element = options;
37 }
38 t_prod__initPrice(element);
39 t_prod__addEvents__options(element);
40 t_prod__updatePrice(element, options);
41}
42
43/**
44 * Init price
45 *
46 * @param {Node} productElement - product card
47 */
48function t_prod__initPrice(productElement) {
49 let element = productElement;
50 // TODO: jqeury in js
51 if (!(element instanceof Element)) element = element[0];
52 if (!element) return;
53
54 var productPrice = element.querySelector('.js-product-price');
55
56 if (!productPrice) {
57 element.insertAdjacentHTML('beforeend', '<div style="display:none;" class="js-product-price"></div>');
58 productPrice = element.querySelector('.js-product-price');
59 }
60
61 var priceValue = productPrice.textContent;
62 var productDef = productPrice.getAttribute('data-product-price-def');
63 var productDefStr = productPrice.getAttribute('data-product-price-def-str');
64
65 // Базовую цену запоминаем один раз. Пустая строка тоже остаётся: у товара нет своей цены, её даёт опция.
66 // Если прочитать цену из карточки ещё раз, надбавка опции прибавится повторно.
67 if (productDef !== null || productDefStr !== null) return;
68
69 if (priceValue) {
70 var price = t_prod__cleanPrice(priceValue);
71 productPrice.setAttribute('data-product-price-def', price);
72 productPrice.setAttribute('data-product-price-def-str', priceValue.replace(/"/g, ''));
73 } else {
74 productPrice.setAttribute('data-product-price-def', 0);
75 }
76}
77
78/**
79 * Update the price when changing
80 *
81 * @param {Node} productElement - product card
82 * @returns {undefined} - exit function
83 */
84function t_prod__updatePrice(productElement, options) {
85 let element = productElement;
86 // TODO: jqeury in js
87 if (!(element instanceof Element)) element = element[0];
88 if (!element) return;
89
90 var productPrice = element.querySelector('.js-product-price');
91 var productSelects = element.querySelectorAll('.js-product-option-variants');
92 var totalPrice;
93 var add = 0;
94
95 if (!productPrice) return;
96
97 var priceContainer = productPrice.parentNode;
98 const hasContainerClass = priceContainer?.classList.value.split(/\s+/).some(cls => cls.endsWith('__price'));
99 priceContainer = hasContainerClass ? priceContainer : null;
100
Следующие 100 строк