Magento等级价格 - 买入价格中的等级价格声明为Y - javascript

时间:2012-11-22 00:06:03

标签: javascript magento

在Magento的1.6+版本中存在一个突出的错误,当选择一个选项时,等级价格的节省百分比默认为100%。其他贡献者建议从

的第747行更改product.js
for (var i = 0; i < this.tierPrices.length; i++) {

for (var i = 0; i > this.tierPrices.length; i++) {

这解决了%Saving的问题,但该代码块从未执行过。我绝不是Javascript专家,但是当选择选项时,此块似乎更新了层级价格和%节省。我想找到问题的根源,而不是“评论出来”。

根据我在Firebug中的调试,我注意到product.js中的等级价格类是错误的,因此,检索层级价格为0,这说明为什么%节省总是100%。 Firebug将价格显示为

class="tier-prices product-pricing">   
        Buy 10 for  
        <span class="price">$40.00</span>

而product.js正在尝试使用

检索对象
$$('.price.tier-' + i).each(function (el) {

如果您将上述内容更改为

$$('.tier-prices .price).each(function (el) {

检索层级价格,但对于产品的多个层级价格,无法单独引用它们。上面的“价格”类没有声明唯一标识符或迭代编号。

为等级价格宣布class =“price”在哪里?在tierprices.phtml的代码中,它看起来像这样

<?php echo $this->__('Buy %1$s for %2$s each', $_price['price_qty'], $_price['formated_price'])?>

2 个答案:

答案 0 :(得分:6)

我刚刚花了一些时间,因为在我将客户的Magento网站升级到1.7.0.2之后,它真的开始让我感到烦恼。

这有两个部分,我将说明位置和修复,但这些不是升级证明(为此您需要创建文件的副本并将它们放在特定于主题的文件夹中,虽然我不确定这个JS文件是否可能存在问题。)

1)查看修复

在档案/design/frontend/base/default/template/catalog/product/view/tierprices.phtml

您需要替换行32-34

$_product = $this->getProduct();
$_tierPrices = $this->getTierPrices();
$_finalPriceInclTax = $this->helper('tax')->getPrice($_product, $_product->getFinalPrice(), true);

使用以下代码:

$_product = $this->getProduct();
$_tierPrices = array();

foreach($this->getTierPrices() as $index => $info) {
    $_tierPrices[$index] = $info;
    $_tierPrices[$index]['formated_price'] = str_replace('class="price"', 'class="price tier-'.$index.'"', $info['formated_price']);
    $_tierPrices[$index]['formated_price_incl_tax'] = str_replace('class="price"', 'class="price tier-'.$index.' tier-'.$index.'-incl-tax"', $info['formated_price_incl_tax']);
}
$_finalPriceInclTax = $this->helper('tax')->getPrice($_product, $_product->getFinalPrice(), true);

这解决了正如您已经想到的那样没有正确呈现类的问题。 Here is where I found this code - 虽然它没有解决所有问题,因此JS改变了。

2)JS修复

在文件js/Varien/product.js中,您需要替换行757-769

$$('.benefit').each(function (el) {
    var parsePrice = function (html) {
        return parseFloat(/\d+\.?\d*/.exec(html));
    };
    var container = $(this.containers[3]) ? this.containers[3] : this.containers[0];
    var price = parsePrice($(container).innerHTML);
    var tierPrice = $$('.price.tier-' + i);
    tierPrice = tierPrice.length ? parseInt(tierPrice[0].innerHTML, 10) : 0;
    var $percent = Selector.findChildElements(el, ['.percent.tier-' + i]);
    $percent.each(function (el) {
        el.innerHTML = Math.ceil(100 - ((100 / price) * tierPrice));
    });
}, this);

有了这个:

//
// Code fixed to prevent the redundant inner loop and to use actual tiered pricing in calculation
// It also takes the optional price variants into consideration (eg: +£2 for a blue tshirt)
// Note: I've made this comment deliberately large, to keep the line numbers in sync
//
var parsePrice = function (html) {
    return parseFloat(/\d+\.?\d*/.exec(html));
};
var container = $(this.containers[3]) ? this.containers[3] : this.containers[0];
var price = parsePrice($(container).innerHTML);
$$('.percent.tier-' + i).each(function (el) {
    el.innerHTML = Math.ceil(100 - ((100 / price) * (this.tierPrices[i] + parseFloat(optionPrices))));
}, this);

我希望这可以在他们几个小时的生命中拯救至少一个人。

Ť

答案 1 :(得分:0)

部分class="price"是来自Mage_Directory_Model_Currency::formatPrecision()的结果,并在$this->formatPrice()时或在更深的模型图层中执行,价格格式化 与Mage::helper('core')->formatPrice();

您可以通过扩展(和重写)Mage_Directory_Model_Currency来添加唯一标识符,但要注意formatPrecision在任何地方都使用过。因此,您可以编写自己的帮助器/模型逻辑来格式化层价格。