Get All simple product from a Configurable Product in Magento Product View

后端 未结 6 1561
轮回少年
轮回少年 2021-01-31 09:55

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 w

6条回答
  •  感情败类
    2021-01-31 10:19

    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()"
    

    Full description

提交回复
热议问题