Magento:捆绑产品中“配置价格”的bundle.js中的Tier Pricing'Bug'
我想要弄清楚如何让Bundle页面中的“Price as Configured”部分以定价更新。Magento:捆绑产品中“配置价格”的bundle.js中的Tier Pricing'Bug'
现在,如果我在捆绑产品中有复选框选择,并且单击复选框将其添加到捆绑包中,则“配置价格”会更新为产品的完整价格,而不是层级定价该选择的默认数量在层级定价领域内)。
现在,我经历的/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的解决方案,遗憾的是需要进行更换的核心文件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
我主要使用Magento的1.7:
[跳到下面我的回答肉的更新。总结:我们学到了什么?我认为你使用的是Magento 1.5。但即使是Magento 1.7也不够好。在Magento 1.8之前,您尝试使用的功能尚未完全修复错误。]
我着手帮助你,因为我知道使用bundle可以有多复杂。我没有使用层级定价,因此我在开发服务器上设置了它,并开始逐步完成上面列出的bundle.js
函数。
我发现,让我困惑了两两件事:
一)我tierPrice[]
是使用索引数组0,1,2(我设置为通过Magento管理三个层次):
这里来帮助你是从我的包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>
段:
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"}], ...
你的看起来像这样吗?
二)所以我的开发服务器上面的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;
}
//...
因此,到最后,我的“价格作为配置的”最终不正确了。
您认为如何?你能弄清楚为什么你的捆绑对象是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
更新:我已经提到bundle.js从Magento的1.7
我可以在Magento看到1.8捆绑.js已被改进以考虑tierPrice和tierPrice包括和不含税 - 但也必须伴随不同的捆绑JSON对象(以保存字段和值为tierPrice[i].priceInclTax;
和tierPrice[i].priceExclTax;
)
因此,我们可能会现在回答:
一)升级到Magento的1.8
或
B)手动更新只是你的主题bundle.js
从bundle.js
Magento的1.8 代码和为JSON对象,文件中的“应用程序/核心/法师/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
并创建您自己的新bundle.php
与此更新getJsonConfig()
我还没有运行之间的差异这些文件从1.7和1.8这是值得做的,看看是否有其他改变。
所有这一切都假设您可以深入了解为什么您有tierPrice ['32000-5'],如上所述,比较我的包JSON对象声明与您的或粘贴在这里供其他人检查。
我们学到了什么?我认为你使用的是Magento 1.5。但即使是Magento 1.7也不够好。在Magento 1.8之前,您尝试使用的功能尚未完全修复错误。
Malachy。
马拉奇您好,感谢后 - 事实上,我想通出一个修正,只是涉及到更改bundle.js文件 - 它看起来像比您的建议更简单 - 我现在将它发布。 –
这是我回答我自己的问题!
所以我想出了为什么它不工作。 Magento代码尝试使用以下对象迭代:
var(i=0;i<object.length;i++)
这不适用于对象。我改变了代码通过对象与迭代在selectionPrice(在对象键):功能(optionId,selectionId)方法:
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? – Malachy
您可能想要编辑for..in循环以使用此语法,因为[Douglas Crockford](http://javascript.crockford.com/code.html) 'for(object variable){ if( object.hasOwnProperty(变量)){ 声明 } }' 为了记录在案,包括和不含税价格不仅关系到WEEE它包括营业税了,所以这是一个三层的价格是不是一个简单的补丁复杂的税收。 我认为最好的解决方案是升级到Magento 1.8 – Malachy
感谢您对hasOwnProperty的单挑!该补丁适用于我,因为我们在结账之前不会显示税金,因此,当您在捆绑页面上时,您将始终看到没有任何税费的价格。升级到1.8也是一个不错的解决方案,但是我仍然有点担心从1.7.0.2(我现在的版本)开始移动,以防万一破坏哈哈! –
在一次奇怪的事件转变中,我实际上刚刚遇到了模块解决的第二个问题(使用捆绑定价),现在在Google搜索将我带回此线程后,现在正在使用它来解决此问题。干得不错! –