Сейчас узнаем, какой подарок выпадет именно Вам
Крутите колесо
Как добавить скидку в корзине за самовывоз или on-line оплату в Tilda
Explore fresh & juicy vegetables
100% Organic Vegetables
BUY
Fresh vegetables from the garden — every day! Our store offers a wide selection of seasonal and traditional vegetables at competitive prices. Go shopping!
0
0

Как добавить скидку в корзине за самовывоз в Tilda

1
Создали скидку в каталоге по скрину ниже
2
Добавили код в блок Т123 на страницу или в footer (страницы и каталога)
В коде заменили подписи на свои

//Название скидки в системе
let dnDelivery = 'Самовывоз 10%';

//Название доставки в корзине
let cartNameDelivery = 'Самовывоз';

//Подпись для подсказки в корзине
let discountHint = 'Скидка за самовывоз 10%';
Библиотека для примера
Скидка за самовывоз
<!-- СКИДКА ЗА САМОВЫВОЗ -->
<script>
(function () {

    // Название скидки в системе
    let dnDelivery = 'Самовывоз 10%';

    // Название доставки в корзине
    let cartNameDelivery = 'Самовывоз';

    // Подпись для подсказки в корзине
    let discountHint = 'Скидка за самовывоз 10%';

    let originalDiscount = null;
    let pickupDiscountId = null;
    let lastState = '';

    function getDelivery() {
        const deliveryInput = document.querySelector(
            'input.t-radio_delivery:checked'
        );
        if (!deliveryInput) {
            return '';
        }
        return String(deliveryInput.value || '').trim();
    }

    function findPickupDiscount() {
        if (!Array.isArray(window.t_cart__discounts)) {
            return null;
        }
        return window.t_cart__discounts.find(function (discount) {
            return discount.name === dnDelivery;
        }) || null;
    }

    function saveOriginalDiscount() {
        if (originalDiscount) {
            return true;
        }
        const discount = findPickupDiscount();
        if (!discount) {
            return false;
        }
        originalDiscount = JSON.parse(JSON.stringify(discount));
        pickupDiscountId = Number(discount.id);
        return true;
    }

    function updateDiscountHint() {
        setTimeout(function () {
            const descriptions = document.querySelectorAll(
                '.t706__cartwin-discounts__description li'
            );
            descriptions.forEach(function (description) {
                description.innerText = discountHint;
            });
        }, 100);
    }

    function removePickupDiscount() {
        if (
            !window.tcart ||
            !Array.isArray(window.tcart.products) ||
            pickupDiscountId === null
        ) {
            return;
        }

        window.t_cart__discounts = (
            window.t_cart__discounts || []
        ).filter(function (discount) {
            return Number(discount.id) !== pickupDiscountId;
        });

        window.tcart.products.forEach(function (product) {
            if (Number(product.discountid) === pickupDiscountId) {
                product.amount_withdiscount = product.amount;
                product.price_withdiscount = product.price;
                delete product.discountid;
            }
        });

        window.tcart.prodamount_withdyndiscount = window.tcart.prodamount;
        window.tcart.amount = window.tcart.prodamount;

        if (typeof window.tcart__reDrawTotal === 'function') {
            window.tcart__reDrawTotal();
        }
    }

    function applyPickupDiscount() {
        if (
            !window.tcart ||
            !Array.isArray(window.tcart.products) ||
            !originalDiscount ||
            pickupDiscountId === null
        ) {
            return;
        }

        window.t_cart__discounts = (
            window.t_cart__discounts || []
        ).filter(function (discount) {
            return Number(discount.id) !== pickupDiscountId;
        });

        window.t_cart__discounts.push(
            JSON.parse(JSON.stringify(originalDiscount))
        );

        window.tcart.products.forEach(function (product) {
            const appliesToProduct =
                Array.isArray(originalDiscount.rules?.uids) &&
                originalDiscount.rules.uids.includes(String(product.uid));

            if (!appliesToProduct) {
                return;
            }

            const amount = Number(product.amount) || 0;
            const quantity = Number(product.quantity) || 1;
            const discountPercent =
                Number(originalDiscount.rules?.diff?.['1']) || 10;

            const discountAmount =
                Math.round(amount * discountPercent / 100 * 100) / 100;
            const newAmount = amount - discountAmount;

            product.discountid = pickupDiscountId;
            product.amount_withdiscount = newAmount;
            product.price_withdiscount =
                Math.round((newAmount / quantity) * 100) / 100;
        });

        window.tcart.amount = window.tcart.products.reduce(
            function (sum, product) {
                return (
                    sum +
                    (
                        Number(product.amount_withdiscount) ||
                        Number(product.amount) ||
                        0
                    )
                );
            },
            0
        );

        window.tcart.prodamount_withdyndiscount = window.tcart.amount;

        if (typeof window.tcart__reDrawTotal === 'function') {
            window.tcart__reDrawTotal();
        }

        updateDiscountHint();
    }

    function update() {
        const delivery = getDelivery();

        if (
            !delivery ||
            !window.tcart ||
            !Array.isArray(window.tcart.products) ||
            !window.tcart.products.length
        ) {
            return;
        }

        if (!originalDiscount) {
            if (!saveOriginalDiscount()) {
                return;
            }
        }

        const isPickup = delivery === cartNameDelivery;

        const discountActive =
            Array.isArray(window.t_cart__discounts) &&
            window.t_cart__discounts.some(function (discount) {
                return Number(discount.id) === pickupDiscountId;
            });

        const productHasDiscount =
            window.tcart.products.some(function (product) {
                return Number(product.discountid) === pickupDiscountId;
            });

        const currentState =
            (isPickup ? 'pickup' : 'other') +
            ':' +
            discountActive +
            ':' +
            productHasDiscount;

        if (currentState === lastState) {
            if (isPickup) {
                updateDiscountHint();
            }
            return;
        }

        lastState = currentState;

        if (isPickup) {
            applyPickupDiscount();
        } else {
            removePickupDiscount();
        }
    }

    const initTimer = setInterval(
        function () {
            if (
                window.tcart &&
                Array.isArray(window.tcart.products) &&
                window.tcart.products.length &&
                Array.isArray(window.t_cart__discounts)
            ) {
                update();
            }
        },
        250
    );

    document.addEventListener(
        'change',
        function (event) {
            if (
                event.target &&
                event.target.matches('input.t-radio_delivery')
            ) {
                lastState = '';
                setTimeout(update, 100);
            }
        },
        true
    );

    setTimeout(update, 1000);

})();
</script>
Библиотека для примера
Скидка за оплаты on-line

//Название скидки в системе
let dnPayment = 'Скидка за оплату 15%';

//Название оплаты в корзине
let cartNamePayment = 'Наличными при получении';

//Подпись для подсказки в корзине
let discountHint = 'Скидка за оплату on-line 15%';
<!-- СКИДКА ЗА ONLINE ОПЛАТУ -->
<script>
(function () {

    // Название скидки в системе
    let dnPayment = 'Скидка за оплату 15%';

    // Название оплаты без скидки
    let cartNamePayment = 'Наличными при получении';

    // Подпись для подсказки в корзине
    let discountHint = 'Скидка за оплату on-line 15%';

    let originalDiscount = null;
    let paymentDiscountId = null;
    let lastState = '';

    function getPayment() {
        const paymentInput = document.querySelector(
            'input.t-radio_payment:checked'
        );
        if (!paymentInput) {
            return '';
        }
        const parent = paymentInput.parentElement;
        if (!parent) {
            return '';
        }
        return String(parent.innerText || '').trim();
    }

    function findPaymentDiscount() {
        if (!Array.isArray(window.t_cart__discounts)) {
            return null;
        }
        return window.t_cart__discounts.find(function (discount) {
            return discount.name === dnPayment;
        }) || null;
    }

    function saveOriginalDiscount() {
        if (originalDiscount) {
            return true;
        }
        const discount = findPaymentDiscount();
        if (!discount) {
            return false;
        }
        originalDiscount = JSON.parse(JSON.stringify(discount));
        paymentDiscountId = Number(discount.id);
        return true;
    }

    function updateDiscountHint() {
        setTimeout(function () {
            const descriptions = document.querySelectorAll(
                '.t706__cartwin-discounts__description li'
            );
            descriptions.forEach(function (description) {
                description.innerText = discountHint;
            });
        }, 100);
    }

    function removePaymentDiscount() {
        if (
            !window.tcart ||
            !Array.isArray(window.tcart.products) ||
            paymentDiscountId === null
        ) {
            return;
        }

        window.t_cart__discounts = (
            window.t_cart__discounts || []
        ).filter(function (discount) {
            return Number(discount.id) !== paymentDiscountId;
        });

        window.tcart.products.forEach(function (product) {
            if (Number(product.discountid) === paymentDiscountId) {
                product.amount_withdiscount = product.amount;
                product.price_withdiscount = product.price;
                delete product.discountid;
            }
        });

        window.tcart.prodamount_withdyndiscount = window.tcart.prodamount;
        window.tcart.amount = window.tcart.prodamount;

        if (typeof window.tcart__reDrawTotal === 'function') {
            window.tcart__reDrawTotal();
        }
    }

    function applyPaymentDiscount() {
        if (
            !window.tcart ||
            !Array.isArray(window.tcart.products) ||
            !originalDiscount ||
            paymentDiscountId === null
        ) {
            return;
        }

     
        window.t_cart__discounts = (
            window.t_cart__discounts || []
        ).filter(function (discount) {
            return Number(discount.id) !== paymentDiscountId;
        });

     
        window.t_cart__discounts.push(
            JSON.parse(JSON.stringify(originalDiscount))
        );

        window.tcart.products.forEach(function (product) {
            const appliesToProduct =
                Array.isArray(originalDiscount.rules?.uids) &&
                originalDiscount.rules.uids.includes(String(product.uid));

            if (!appliesToProduct) {
                return;
            }

            const amount = Number(product.amount) || 0;
            const quantity = Number(product.quantity) || 1;

          
            const discountPercent =
                Number(originalDiscount.rules?.diff?.['1']) || 15;

            const discountAmount =
                Math.round(amount * discountPercent / 100 * 100) / 100;
            const newAmount = amount - discountAmount;

            product.discountid = paymentDiscountId;
            product.amount_withdiscount = newAmount;
            product.price_withdiscount =
                Math.round((newAmount / quantity) * 100) / 100;
        });

  
        window.tcart.amount = window.tcart.products.reduce(
            function (sum, product) {
                return (
                    sum +
                    (
                        Number(product.amount_withdiscount) ||
                        Number(product.amount) ||
                        0
                    )
                );
            },
            0
        );

        window.tcart.prodamount_withdyndiscount = window.tcart.amount;

     
        if (typeof window.tcart__reDrawTotal === 'function') {
            window.tcart__reDrawTotal();
        }

      
        updateDiscountHint();
    }

    function update() {
        const payment = getPayment();

        if (
            !payment ||
            !window.tcart ||
            !Array.isArray(window.tcart.products) ||
            !window.tcart.products.length
        ) {
            return;
        }

       
        if (!originalDiscount) {
            if (!saveOriginalDiscount()) {
                return;
            }
        }

      
        const isOnlinePayment = payment !== cartNamePayment;

        const discountActive =
            Array.isArray(window.t_cart__discounts) &&
            window.t_cart__discounts.some(function (discount) {
                return Number(discount.id) === paymentDiscountId;
            });

        const productHasDiscount =
            window.tcart.products.some(function (product) {
                return Number(product.discountid) === paymentDiscountId;
            });

        const currentState =
            (isOnlinePayment ? 'online' : 'cash') +
            ':' +
            discountActive +
            ':' +
            productHasDiscount;

        if (currentState === lastState) {
          
            if (isOnlinePayment) {
                updateDiscountHint();
            }
            return;
        }

        lastState = currentState;

        if (isOnlinePayment) {
            applyPaymentDiscount();
        } else {
            removePaymentDiscount();
        }
    }

   
    const initTimer = setInterval(
        function () {
            if (
                window.tcart &&
                Array.isArray(window.tcart.products) &&
                window.tcart.products.length &&
                Array.isArray(window.t_cart__discounts)
            ) {
                update();
            }
        },
        250
    );


    document.addEventListener(
        'change',
        function (event) {
            if (
                event.target &&
                event.target.matches('input.t-radio_payment')
            ) {
                lastState = '';
                setTimeout(update, 100);
            }
        },
        true
    );


    setTimeout(update, 1000);

})();
</script>
Made on
Tilda