PHP
- 4 ответа
- 0 вопросов
2
Вклад в тег
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>