在Magento产品视图中从可配置产品中获取所有简单产品 [英] Get All simple product from a Configurable Product in Magento Product View
问题描述
如何获得与可配置产品关联的所有简单产品?我发现了相反的方法(从简单产品中获取可配置的产品),但这不是我所需要的.
我想显示所选产品的库存量(可配置属性).我最初的想法是打印所有数量的库存并使用jQuery控制显示.有什么主意吗?
在
中使用以下脚本
app/design/frontend/default/[your theme]/template/catalog/product/view/type/options/configurable.phtml
在脚本内部:
spConfig.getIdOfSelectedProduct = function () {
var existingProducts = new Object();
for (var i = this.settings.length - 1; i >= 0; i--) {
var selected = this.settings[i].options[this.settings[i].selectedIndex];
if (selected.config) {
for (var iproducts = 0; iproducts < selected.config.products.length; iproducts++) {
var usedAsKey = selected.config.products[iproducts] + "";
if (existingProducts[usedAsKey] == undefined) {
existingProducts[usedAsKey] = 1;
} else {
existingProducts[usedAsKey] = existingProducts[usedAsKey] + 1;
}
}
}
}
for (var keyValue in existingProducts) {
for (var keyValueInner in existingProducts) {
if (Number(existingProducts[keyValueInner]) < Number(existingProducts[keyValue])) {
delete existingProducts[keyValueInner];
}
}
}
var sizeOfExistingProducts = 0;
var currentSimpleProductId = "";
for (var keyValue in existingProducts) {
currentSimpleProductId = keyValue;
sizeOfExistingProducts = sizeOfExistingProducts + 1
}
if (sizeOfExistingProducts == 1) {
alert("Selected product is: " + currentSimpleProductId)
}
}
现在将onchange
事件添加到同一页面的下拉菜单中:
onchange = "spConfig.getIdOfSelectedProduct()"
完整说明 >
How can I get all the simple products associated with a configurable product? I found how to do the opposite (get a product configurable from a simple product) but that's not what I need.
I want to show how many units I have in stock for the selected product (configurable attribute). My initial idea is to print all quantities of stock and control the display with jQuery. Any idea?
use the following script in
app/design/frontend/default/[your theme]/template/catalog/product/view/type/options/configurable.phtml
Inside the script:
spConfig.getIdOfSelectedProduct = function () {
var existingProducts = new Object();
for (var i = this.settings.length - 1; i >= 0; i--) {
var selected = this.settings[i].options[this.settings[i].selectedIndex];
if (selected.config) {
for (var iproducts = 0; iproducts < selected.config.products.length; iproducts++) {
var usedAsKey = selected.config.products[iproducts] + "";
if (existingProducts[usedAsKey] == undefined) {
existingProducts[usedAsKey] = 1;
} else {
existingProducts[usedAsKey] = existingProducts[usedAsKey] + 1;
}
}
}
}
for (var keyValue in existingProducts) {
for (var keyValueInner in existingProducts) {
if (Number(existingProducts[keyValueInner]) < Number(existingProducts[keyValue])) {
delete existingProducts[keyValueInner];
}
}
}
var sizeOfExistingProducts = 0;
var currentSimpleProductId = "";
for (var keyValue in existingProducts) {
currentSimpleProductId = keyValue;
sizeOfExistingProducts = sizeOfExistingProducts + 1
}
if (sizeOfExistingProducts == 1) {
alert("Selected product is: " + currentSimpleProductId)
}
}
Now add onchange
event to your dropdown in same page :
onchange = "spConfig.getIdOfSelectedProduct()"
这篇关于在Magento产品视图中从可配置产品中获取所有简单产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!