Javascript Magento:分层定价';Bug';在bundle.js中表示;“配置价格”;关于丛积

Javascript Magento:分层定价';Bug';在bundle.js中表示;“配置价格”;关于丛积,javascript,php,magento,Javascript,Php,Magento,我很难弄清楚如何获得捆绑包页面中的“配置价格”部分,以使用分层定价进行更新 现在,如果我在捆绑产品中选择了一个复选框,并单击该复选框将其添加到捆绑产品中,“配置价格”将更新为产品的完整价格,而不是分层定价(选择的默认数量在分层定价范围内) 现在,我正在浏览/skin/frontend/base/default/js/bundle.js文件,在selectionPrice方法下,我看到了以下内容: selectionPrice: function(optionId, selectionId) {

我很难弄清楚如何获得捆绑包页面中的“配置价格”部分,以使用分层定价进行更新

现在,如果我在捆绑产品中选择了一个复选框,并单击该复选框将其添加到捆绑产品中,“配置价格”将更新为产品的完整价格,而不是分层定价(选择的默认数量在分层定价范围内)

现在,我正在浏览/skin/frontend/base/default/js/bundle.js文件,在selectionPrice方法下,我看到了以下内容:

selectionPrice: function(optionId, selectionId) {

.................

    if (this.config.priceType == '0') {
        price = this.config.options[optionId].selections[selectionId].price;
        tierPrice = this.config.options[optionId].selections[selectionId].tierPrice;

        for (var i=0; i < tierPrice.length; i++) {
            if (Number(tierPrice[i].price_qty) <= qty && Number(tierPrice[i].price) <= price) {
                price = tierPrice[i].price;
            }
        }
    } else {
        selection = this.config.options[optionId].selections[selectionId];
        if (selection.priceType == '0') {
            price = selection.priceValue;
        } else {
            price = (this.config.basePrice*selection.priceValue)/100;
        }
    }
现在通常情况下,如果tierPrice返回一个对象,它应该能够遍历该对象并找到层价格(至少代码似乎是这么做的)。然而,它从未进入实际设置一个分层价格的for循环。在控制台中,我可以通过执行以下操作直接访问分层价格:

bundle.config.options['301'].selections['1066'].tierPrice['32000-5']
但是,Magento代码取决于是否能够调用….tierPrice[0],而不是….tierPrice['32000-5'],以获取每个单独的层定价对象。调用…tierPrice[0]返回未定义,而tierPrice.length返回未定义

为什么我不能使用for循环访问嵌套数组?我的选择是什么


谢谢

我主要使用Magento 1.7和:

[跳到下面的更新,了解我的答案。摘要:我们学到了什么?我认为您使用的是Magento 1.5。但即使是Magento 1.7也不够好。您尝试使用的功能直到Magento 1.8才完全修复错误。]

我开始在这里帮助您,因为我知道使用捆绑包有多复杂。我没有使用分层定价,所以我在开发服务器上设置了分层定价,并开始逐步使用上面列出的
bundle.js
函数

我发现了两件让我困惑的事情:

a) 我的
tierPrice[]
是一个索引为0,1,2的数组(我通过Magento管理员设置了三层):

这里帮助您的是我的bundle JSON对象定义中的一个片段,即
app/design/frontend/themename/default/template/bundle/catalog/product/view/type/bundle/bundle.phtml

 <script type="text/javascript">
  //<![CDATA[
      var bundle = new Product.Bundle(<?php echo $this->getJsonConfig() ?>);
  //]]>
 </script>
你的像这样吗

b) 因此,在我的开发服务器上,在上面的for循环之后,
bundle.js
正确识别了一个层价格,但在代码的后面,可变价格会根据显示的价格(含税或不含税)重置,即以下代码:

//file: skin/frontend/theme/default/js/bundle.js
//function: selectionPrice()
//...
selection = this.config.options[optionId].selections[selectionId];
if (selection.priceInclTax !== undefined) {
    priceInclTax = selection.priceInclTax;
    price = selection.priceExclTax !== undefined ? selection.priceExclTax : selection.price;
} else {
    priceInclTax = price;
}
//...
因此,最终,我的“配置价格”也不正确

你觉得怎么样?你能找出你的bundle对象为什么要做
tierPrice['32000-5']
而不是
tierPrice[0]
?当
bundle.js
尝试应用含税或不含税显示价格时,您的层价格是否被覆盖

(而且似乎没有默认的方法来知道价格是含税还是不含税。我在这里嗅到了一个bug)

实际上,这个bug报告可能会引起您的兴趣。(需要登录)],并有助于解释Magento 1.8中的代码更新

//file app/core/Mage/Bundle/Block/Catalog/Product/View/Type/Bundle.php
//class Mage_Bundle_Block_Catalog_Product_View_Type_Bundle
//function getJsonConfig()
//...
                $tierPrices = $_selection->getTierPrice();
                foreach ($tierPrices as &$tierPriceInfo) {
                    $tierPriceInfo['price'] = $coreHelper->currency($tierPriceInfo['price'], false, false);
                    $tierPriceInfo['priceInclTax'] = $taxHelper->getPrice($_selection, $tierPriceInfo['price'], true);
                    $tierPriceInfo['priceExclTax'] = $taxHelper->getPrice($_selection, $tierPriceInfo['price']);
                }
                unset($tierPriceInfo); // break the reference with the last element
//...
更新:我一直在引用Magento 1.7中的bundle.js

我可以看到,在Magento 1.8 bundle.js中,已经对tierPrice和tierPrice(包括和不包括税)进行了改进,但这还必须附带一个不同的bundle JSON对象(用于保存
tierPrice[I].priceInclTax;
tierPrice[I].priceExclTax;
)的字段和值)

因此,我们现在可能有一个答案:

a) 升级至Magento 1.8

//file app/core/Mage/Bundle/Block/Catalog/Product/View/Type/Bundle.php
//class Mage_Bundle_Block_Catalog_Product_View_Type_Bundle
//function getJsonConfig()
//...
                $tierPrices = $_selection->getTierPrice();
                foreach ($tierPrices as &$tierPriceInfo) {
                    $tierPriceInfo['price'] = $coreHelper->currency($tierPriceInfo['price'], false, false);
                    $tierPriceInfo['priceInclTax'] = $taxHelper->getPrice($_selection, $tierPriceInfo['price'], true);
                    $tierPriceInfo['priceExclTax'] = $taxHelper->getPrice($_selection, $tierPriceInfo['price']);
                }
                unset($tierPriceInfo); // break the reference with the last element
//...

b) 手动更新主题的
bundle.js
,使用
bundle.js
Magento 1.8中的代码,并在文件“app/core/Mage/bundle/Block/Catalog/Product/View/Type/bundle.php”中对JSON对象在1.7和1.8之间进行检查

Magento 1.7

//file app/core/Mage/Bundle/Block/Catalog/Product/View/Type/Bundle.php
//class Mage_Bundle_Block_Catalog_Product_View_Type_Bundle
//function getJsonConfig()
//...
                $tierPrices = $_selection->getTierPrice();
                foreach ($tierPrices as &$tierPriceInfo) {
                    $tierPriceInfo['price'] = $coreHelper->currency($tierPriceInfo['price'], false, false);
                }
                unset($tierPriceInfo); // break the reference with the last element
//...
Magento 1.8

//file app/core/Mage/Bundle/Block/Catalog/Product/View/Type/Bundle.php
//class Mage_Bundle_Block_Catalog_Product_View_Type_Bundle
//function getJsonConfig()
//...
                $tierPrices = $_selection->getTierPrice();
                foreach ($tierPrices as &$tierPriceInfo) {
                    $tierPriceInfo['price'] = $coreHelper->currency($tierPriceInfo['price'], false, false);
                    $tierPriceInfo['priceInclTax'] = $taxHelper->getPrice($_selection, $tierPriceInfo['price'], true);
                    $tierPriceInfo['priceExclTax'] = $taxHelper->getPrice($_selection, $tierPriceInfo['price']);
                }
                unset($tierPriceInfo); // break the reference with the last element
//...
因此,如果要手动更新旧版本的Magento,则需要扩展类
Mage\u Bundle\u Block\u Catalog\u Product\u View\u Type\u Bundle
,并使用此更新的
getJsonConfig()
创建自己的新
Bundle.php

我还没有在这些1.7和1.8版本的文件之间运行差异,这是值得做的,以查看是否还有其他更改

所有这些都假设您可以找到tierPrice['32000-5']的根本原因,因此,正如我前面提到的,将我的捆绑JSON对象声明与您的捆绑JSON对象声明进行比较,或者将您的捆绑JSON对象声明粘贴到此处供其他人检查

我们学到了什么?我认为您正在使用Magento 1.5。但即使是Magento 1.7也不够好。在Magento 1.8之前,您尝试使用的功能尚未完全修复bug


马拉奇。

这是我在回答我自己的问题

所以我明白了为什么它不起作用。Magento代码尝试使用以下命令遍历对象:

var(i=0;i<object.length;i++)
这似乎对我起到了作用-它现在成功地更新了“按配置的价格”以说明分层定价,并且仍然成功地更新了所有其他定价。如果你有一个Magento商店有weee模块/税收规则,那么它可能会引起骚动,但对于我的商店配置来说,它工作得很好


希望有帮助

作为基于JavaScript的解决方案的替代方案,不幸的是需要替换核心文件
bundle.js
我有一个基于PHP的解决方案

首先,问题是这个对象不应该是一个对象。我们可以干预JSON生成,并确保它实际上是一个数组

为此,必须使用以下方法重写class
Mage\u Catalog\u Model\u Product\u Type\u Price

public function getTierPrice($qty = null, $product)
{
    $tierPrice = parent::getTierPrice($qty, $product);
    if (is_array($tierPrice)) {
        return array_values($tierPrice);
    }
    return $tierPrice;
}
getTierPrice()
方法没有在任何地方使用,在我看来,字符串键与此相关,因此这比分解生成的JSON并重新创建它更优雅

我写了一个小扩展来修复这个问题
// Check that priceInclTax AND tierPrice are not set
    if (selection.priceInclTax !== undefined && tierPrice.length == 0) {

        priceInclTax = selection.priceInclTax;
        price = selection.priceExclTax !== undefined ? selection.priceExclTax : selection.price;

    }

    else {
        priceInclTax = price;
    }
public function getTierPrice($qty = null, $product)
{
    $tierPrice = parent::getTierPrice($qty, $product);
    if (is_array($tierPrice)) {
        return array_values($tierPrice);
    }
    return $tierPrice;
}