我想在可配置产品的下拉列表中显示产品差价(选择一个选项),与magento 1.X相同,谢谢大家!
这是Magento 2中的known issue
用于选取列表的JavaScript数组在类的方法 getJsonConfig() 中生成
getJsonConfig()
app/code/Magento/ConfigurableProduct/Block/Product/View/Type/Configurable.php
在那里你看到两个数组被添加到 $config : attributes 和 optionPrices . attributes 似乎包含用于下拉列表的标签 . optionPrices 包含用户在选择其他选项(具有不同价格关联)后刷新价格的价格 .
$config
attributes
optionPrices
由于需要在JavaScript中计算价格差异值,因此很难在下拉列表中显示价格差异 .
一个相对容易实现的解决方案就是这样,但这将显示每个选项的最终价格,而不是价格差异:
创建在执行 getJsonConfig 后触发的Magento 2插件模块: afterGetJsonConfig()
getJsonConfig
afterGetJsonConfig()
然后使用 afterGetJsonConfig() 中的 getJsonConfig 的输出: $config ,此数组包含 attributes 和 priceOptions 数组等 .
priceOptions
首先jsondecode $config
然后遍历attributes数组以确定可用选项
对于每个选项,在 priceOptions 数组中查找匹配的价格 .
通过将价格(格式正确)连接到现有标签,更新属性数组中的每个选项标签 .
jsonencode $config
返回 $config
1 回答
这是Magento 2中的known issue
用于选取列表的JavaScript数组在类的方法
getJsonConfig()
中生成在那里你看到两个数组被添加到
$config
:attributes
和optionPrices
.attributes
似乎包含用于下拉列表的标签 .optionPrices
包含用户在选择其他选项(具有不同价格关联)后刷新价格的价格 .由于需要在JavaScript中计算价格差异值,因此很难在下拉列表中显示价格差异 .
一个相对容易实现的解决方案就是这样,但这将显示每个选项的最终价格,而不是价格差异:
创建在执行
getJsonConfig
后触发的Magento 2插件模块:afterGetJsonConfig()
然后使用
afterGetJsonConfig()
中的getJsonConfig
的输出:$config
,此数组包含attributes
和priceOptions
数组等 .首先jsondecode
$config
然后遍历attributes数组以确定可用选项
对于每个选项,在
priceOptions
数组中查找匹配的价格 .通过将价格(格式正确)连接到现有标签,更新属性数组中的每个选项标签 .
jsonencode
$config
返回
$config