【问题标题】:Magento: Tier Pricing 'Bug' in bundle.js for "Price as Configured" on Bundle productsMagento:Bundle.js 中针对 Bundle 产品的“配置价格”的层级定价“错误”
【发布时间】:2014-03-06 23:52:32
【问题描述】:

我很难弄清楚如何让捆绑页面的“配置价格”部分更新为层级定价。

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

现在我正在浏览 /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;
        }
    }

到目前为止,我可以说它可以设置:

price = this.config.options[optionId].selections[selectionId].price

现在通常如果 tierPrice 返回一个对象,它应该能够遍历它并找到层价格(至少这是代码似乎应该做的)。但是,它永远不会进入实际设置层价格的 for 循环。在控制台中,我可以通过以下方式直接访问等级价格:

bundle.config.options['301'].selections['1066'].tierPrice['32000-5']

但是,Magento 代码依赖于能够调用 .....tierPrice[0] 而不是 ....tierPrice['32000-5'] 来获取每个单独的层定价对象。调用 ....tierPrice[0] 返回 undefined 并且 tierPrice.length 返回 undefined。

为什么我不能使用 for 循环访问嵌套数组?我在这里有什么选择?

谢谢!

【问题讨论】:

    标签: javascript php magento


    【解决方案1】:

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

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

    为此,Mage_Catalog_Model_Product_Type_Price 类必须使用以下方法重写:

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

    getTierPrice() 方法在任何地方都没有使用,据我所知,字符串键是相关的,所以这比将生成的 JSON 分解并重新创建更优雅。

    我写了一个小扩展来修复这个问题以及另一个捆绑层定价错误,你可以从https://github.com/sgh-it/bundletierprices获得它

    延伸阅读: http://www.schmengler-se.de/en/2014/10/magento-buendelprodukte-staffelpreise-der-einfachen-produkte-nutzen/

    【讨论】:

    • 在一个奇怪的事件中,我实际上刚刚遇到了您的模块解决的第二个问题(捆绑包中的层级定价),并且现在在谷歌搜索带我回来后使用它来解决这个问题到这个线程。干得好!
    【解决方案2】:

    我主要使用 Magento 1.7 并且:

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

    我打算在这里为您提供帮助,因为我知道使用捆绑包可能会很复杂。我没有使用分级定价,所以我在我的开发服务器上进行了设置,并开始逐步执​​行您上面列出的 bundle.js 函数。

    我发现了两件事让我感到困惑:

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

    这里帮助你的是我的bundle JSON对象定义中的一个sn-p,即来自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>
    

    片段:

    var bundle = new Product.Bundle({"options":{"837":{"selections":{"4205":{"qty":1,"customQty":"1","price":52,"priceInclTax":52,"priceExclTax":52,"priceValue":0,"priceType":"0","tierPrice":[{"price_id":"4","website_id":"0","all_groups":"1","cust_group":32000,"price":29.99,"price_qty":"2.0000","website_price":"29.9900"},{"price_id":"5","website_id":"0","all_groups":"1","cust_group":32000,"price":18.88,"price_qty":"3.0000","website_price":"18.8800"},{"price_id":"6","website_id":"0","all_groups":"1","cust_group":32000,"price":7.77,"price_qty":"4.0000","website_price":"7.7700"}], ...
    

    你的像这样吗?

    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 尝试应用显示价格含税或不含税时,您的等级价格是否被覆盖?

    (而且似乎没有默认方式知道 tierPrice 是含税还是不含税。我在这里闻到了一个错误)。

    实际上,您可能会对这个错误报告感兴趣。http://www.magentocommerce.com/bug-tracking/issue?issue=11477(需要登录)] 并且有 some other bugs tracked on tier pricing 这将有助于解释 Magento 1.8 中的代码更新

    更新:我指的是 Magento 1.7 中的 bundle.js

    我可以在 Magento 1.8 中看到 bundle.js 已经改进,以考虑 tierPrice 和 tierPrice 包括和不包括税-但还必须伴随不同的捆绑 JSON 对象(以保存 tierPrice[i].priceInclTax; 的字段和值和tierPrice[i].priceExclTax;)

    所以,我们现在可能有一个答案:

    a) 升级到 Magento 1.8

    b) 在文件 'app/core/Mage/Bundle/Block/Catalog /Product/View/Type/Bundle.php',通过在 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_Bundle_Block_Catalog_Product_View_Type_Bundle 并使用更新后的 getJsonConfig() 创建自己的新 bundle.php

    我没有在 1.7 和 1.8 的这些文件之间运行差异,这值得做,看看是否有其他变化。

    所有这一切都假设您可以深入了解为什么拥有 tierPrice['32000-5'] 因此,正如我上面提到的,将我的捆绑 JSON 对象声明与您的进行比较,或者将您的粘贴到此处以供其他人检查。

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

    马拉奇。

    【讨论】:

    • 您好 Malachy,感谢您的帖子 - 我实际上找到了一个只涉及更改 bundle.js 文件的修复程序 - 看起来它比您的建议更简单 - 我现在就发布它。
    【解决方案3】:

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

    所以我弄清楚了为什么它不起作用。 Magento 代码尝试使用以下方法遍历对象:

    var(i=0;i<object.length;i++)
    

    这不适用于对象。我将代码更改为在 selectionPrice: function(optionId, selectionId) 方法中使用 for (key in object) 遍历对象:

    if (this.config.priceType == '0') {
            price = this.config.options[optionId].selections[selectionId].price;
            tierPrice = this.config.options[optionId].selections[selectionId].tierPrice;
    
            // Ensures that tierPrice is set 
            // If a selection has no tier pricing it would return an empty array
    
            if (tierPrice.length != 0){
    
            // Iterate through tier Pricing until you reach correct quantity break
            // then set price.    
    
            for (key in tierPrice){
              if (tierPrice.hasOwnProperty(key)){
                if(tierPrice[key].price_qty <= qty && tierPrice[key].price <= price) {
                    price = tierPrice[key].price;
    
                }
            }
            }
            }
    

    然后您需要在方法的底部添加条件 tierPrice.length == 0 否则您的层级定价将再次被覆盖:

    // 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;
        }
    

    这似乎对我有用——它现在成功地更新了“配置的价格”以考虑层级定价,并且仍然成功地更新了所有其他定价。如果你有一个 Magento 商店,它有 weee 模块/税收规则,那么它可能会大惊小怪,但对于我的商店配置来说,它工作得很好。

    希望有帮助!

    【讨论】:

    • 为了帮助其他访问 thia Q&A 的人,此修复程序适用于哪个版本的 Magento?你是1.5吗? 1.6?
    • 您可能需要编辑 for..in 循环以使用此语法,因为 Douglas Crockford ` for (variable in object) { if (object.hasOwnProperty(variable)) { statements } }`根据记录,含税和不含税的价格不仅与 WEEE 相关,还包括销售税,因此这是一个简单的补丁,适用于不含税的层级价格。我认为最好的解决方案是升级到 Magento 1.8
    • 感谢您对 hasOwnProperty 的提醒!该补丁对我有用,因为我们在结帐之前不显示税金,因此当您在捆绑页面上时,您总是会看到不含税的价格。升级到 1.8 也是一个不错的解决方案,但我仍然有点担心从 1.7.0.2(我当前的版本)迁移,以防万一哈哈!
    • 哇。我的代码实际上是 1.7.0.0。 Magento 是否在再次改进之前让这个捆绑层定价变得更糟?
    猜你喜欢
    • 2015-06-23
    • 2021-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-13
    • 1970-01-01
    相关资源
    最近更新 更多