Ответы пользователя по тегу Magento
  • В селекте отсутствует значение по умолчанию. Как исправить?

    Demisang
    @Demisang
    Backend PHP-developer
    Никто не ответил, потому что решение находится в последней строке вопроса, где весь код в одну строчку написан...
    Нужно добавить в него строку в методе **createDropdown**:
    jQuery("select#qty").append("<option value=''>Выберите кол-во товаров...</option>")

    Чтобы получилось так:
    <script> require(['jquery', 'Magento_Catalog/js/price-utils'], function ($, priceUtils) {
        'use strict';
        var tierPrices = <?php echo json_encode($allTiers) ?>;
        var createDropdown = function () {
            jQuery("select#qty").append("<option value=''>Выберите кол-во товаров...</option>")
            for (var i = 0; i < tierPrices.length; i++) {
                var obj = tierPrices[i];
                jQuery("select#qty").append("<option value='" + Number(obj.price_qty) + "'>Buy " + Number(obj.price_qty) + " For " + priceUtils.formatPrice(obj.price) + " each</option>")
            }
        };
        var getPrice = function (qty) {
            qty = Number(qty);
            var i = tierPrices.length;
            while (i--) {
                if (qty >= tierPrices[i]['price_qty']) {
                    return tierPrices[i]['price'];
                }
            }
            return null;
        };
        var updatePrice = function (price) {
            var newPrice = priceUtils.formatPrice(price);
            jQuery('.price-final_price .price').html(newPrice);
        };
        var updatePriceHtml = function (amount) {
            var price = getPrice(amount);
            if (price !== null) {
                updatePrice(price);
            }
        };
        jQuery('select#qty').change(function () {
            if (tierPrices.length > 0) {
                updatePriceHtml(this.value);
            }
        });
        setTimeout(function () {
            if (tierPrices.length > 0) {
                createDropdown();
                updatePriceHtml(jQuery('select#qty').val());
            }
        }, 700);
    });
    </script>
    Ответ написан
    Комментировать