首先,让我试着改写你的问题:
在每个自定义选项步骤中,如何根据前面步骤中的价格计算百分比,而不是基于产品基本价格?换句话说,你如何让Magento积累自定义期权价格而不是总结它们?
示例产品:
基本价格:0美元
尺码A:20美元
颜色X:10%
Magento的计算:基本价格的10%0 = 0.总价格20 .
期望的计算:基价和尺寸价格的10%= 20的10% . 总价22 .
Solution
要做到这一点,你必须覆盖Magento的自定义选项定价逻辑 . 必须在价格模型(用于服务器端计算)以及块和模板代码(用于客户端Javascript计算)中进行更改 .
在价格模型 Mage_Catalog_Model_Product_Type_Price 中,更改函数 _applyOptionsPrice 中的计算:
//$finalPrice += $group->getOptionPrice($quoteItemOption->getValue(), $basePrice);
$finalPrice += $group->getOptionPrice($quoteItemOption->getValue(), $finalPrice);
在 Mage_Catalog_Block_Product_View_Options 中,函数 getJsonConfig 在将其返回到模板之前计算基本价格的百分比 . 更改此项以获取返回到模板的实际百分比值:
//$_tmpPriceValues[$value->getId()] = Mage::helper('core')->currency($value->getPrice(true), false, false);
// Add fixed price or percentage (don't calculate percentage yet, done by JS live instead)
$_tmpPriceValues[$value->getId()]['pricing_value'] = ($value->getPriceType() != 'percent') ? Mage::helper('core')->currency($value->getPrice(true), false, false) : $value->getPrice();
$_tmpPriceValues[$value->getId()]['is_percentage'] = ($value->getPriceType() == 'percent');
现在修改 template/catalog/product/view/options.phtml 中的Javascript以累计计算即时百分比 . 在Javascript类 Product.Options 中,添加一个计算函数:
getPriceToAdd : function(optionvalue, oldprice) {
var value = parseFloat(optionvalue['pricing_value']);
if (optionvalue['is_percentage']) {
return Math.round(value*oldprice)/100;
} else {
return value;
}
}
并更改 reloadPrice 函数以使用新计算:
//price += parseFloat(config[optionId][element.getValue()]);
price += opConfig.getPriceToAdd(config[optionId][element.getValue()], price);
...
//price += parseFloat(this.config[optionId]);
price += opConfig.getPriceToAdd(this.config[optionId], price);
...
//price += parseFloat(this.config[optionId][selectOption.value]);
price += opConfig.getPriceToAdd(this.config[optionId][selectOption.value], price);
...
//price += parseFloat(this.config[optionId]);
price += opConfig.getPriceToAdd(this.config[optionId], price);
像往常一样,不要直接编辑核心文件 . 使用本地重写 . 请注意,您的新价格模型必须在每个相关产品类型的配置中声明:
mymodule/product_type_simple_price
...