【发布时间】:2018-09-05 20:21:45
【问题描述】:
我想为 Shopify 中的快速查看功能添加基本价格,但我无法使用 jquery 正确处理所有内容。我想根据 productPrice 计算 basePase。问题是,计算没有显示在 HTML 中。看来,我错过了一些东西。仍然是空的。在另一种情况下(Shopify Liquid Code),这种修改非常简单。在这里,它似乎更复杂。有没有人对此有想法/解决方案?感谢您的帮助!
Shopify.doNotTriggerClickOnThumb = false;
var selectCallbackQuickview = function(variant, selector) {
var productItem = jQuery('.quick-view .product-item');
addToCart = productItem.find('.add-to-cart-btn'),
productPrice = productItem.find('.price'),
comparePrice = productItem.find('.compare-price'),
basePrice = productItem.find('.base-price'),
totalPrice = productItem.find('.total-price span');
// Regardless of stock, update the product price
productPrice.html(Shopify.formatMoney(variant.price, "{{ shop.money_format }}"));
// Also update and show the product's compare price if necessary
if ( variant.compare_at_price > variant.price ) {
comparePrice
.html(Shopify.formatMoney(variant.compare_at_price, "{{ shop.money_format }}"))
.show();
productPrice.addClass('on-sale');
} else {
comparePrice.hide();
productPrice.removeClass('on-sale');
}
BASEPRICE IS CALCULATED HERE:
if ( productPrice > 20 ){
if ( productPrice > 90) {
basePrice = productPrice / 75 / 12 * 100;
}
basePrice = productPrice / 75 / 6 * 100;
else {
basePrice = productPrice / 75 * 100;
}
/*recaculate total price*/
//try pattern one before pattern 2
var regex = /([0-9]+[.|,][0-9]+[.|,][0-9]+)/g;
var unitPriceTextMatch = jQuery('.quick-view .price').text().match(regex);
if (!unitPriceTextMatch) {
regex = /([0-9]+[.|,][0-9]+)/g;
unitPriceTextMatch = jQuery('.quick-view .price').text().match(regex);
}
if (unitPriceTextMatch) {
var unitPriceText = unitPriceTextMatch[0];
var unitPrice = unitPriceText.replace(/[.|,]/g,'');
var quantity = parseInt(jQuery('.quick-view input[name=quantity]').val());
var totalPrice = unitPrice * quantity;
var totalPriceText = Shopify.formatMoney(totalPrice, window.money_format);
regex = /([0-9]+[.|,][0-9]+[.|,][0-9]+)/g;
if (!totalPriceText.match(regex)) {
regex = /([0-9]+[.|,][0-9]+)/g;
}
totalPriceText = totalPriceText.match(regex)[0];
var regInput = new RegExp(unitPriceText, "g");
var totalPriceHtml = jQuery('.quick-view .price').html().replace(regInput ,totalPriceText);
jQuery('.quick-view .total-price span').html(totalPriceHtml);
}
/*end of price calculation*/
};
<div class="prices">
<span class="compare-price"></span>
<span class="price"></span>
<span class="base-price">BASEPRICE SHOULD BE SEEN HERE</span>
</div>
【问题讨论】:
-
什么是底价?大多数商店只坚持一个价格,如果他们想提供某种交易的假象,可能会在价格上进行大幅度的比较。
-
如果您销售一瓶葡萄酒(其中通常有 0.75 升) - 底价是 1 升的价格 - 在欧盟,您必须让客户选择比较价格与“基本价格”。所以各种瓶子,0.75、0.5L都可以轻松比较。基本价格 = 每 1 升产品的成本。 - 我已经在所有网站上实现了它,但现在我也想在“产品快速查看”功能中使用它,我不能使用 Liquid Code :) 我是一个 jQuery 新手 :D