Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

magento

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?

like image 776
Denis Spalenza Avatar asked Sep 05 '12 17:09

Denis Spalenza


4 Answers

Use this below code

Code to get the the full product information (where 3 is the configurable product Id)

$product = Mage::getModel('catalog/product')->load(3); 
$childProducts = Mage::getModel('catalog/product_type_configurable')
                    ->getUsedProducts(null,$product);

foreach($childProducts as $child) {
    print_r($child->getName());  // You can use any of the magic get functions on this object to get the value
}

Another code to get the Children Product Ids

$childProducts = Mage::getModel('catalog/product_type_configurable')
                    ->getChildrenIds(3);

Hope this helps!!

like image 65
Great Indian Brain Avatar answered Oct 15 '22 12:10

Great Indian Brain


A configurable product can have multiple other products associated to it.

Here is the code to fetch all the children products that are associated with a configurable product.

Here goes the code :)

/**
 * Load product by product id
 */
$product = Mage::getModel('catalog/product')->load(YOUR_PRODUCT_ID);

/**
 * Get child products id and such (only ids)
 */
$childIds = Mage::getModel('catalog/product_type_configurable')->getChildrenIds($product->getId());

/**
 * Get children products (all associated children products data)
 */
$childProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null,$product);

Source: http://blog.chapagain.com.np/magento-how-to-get-all-associated-children-product-of-a-configurable-product/

like image 42
Abid Hussain Avatar answered Oct 15 '22 12:10

Abid Hussain


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

like image 13
Hardik Avatar answered Oct 15 '22 11:10

Hardik


I got it. Thanks for the replies.

<?php if($_product->getTypeId() == "configurable"): ?>
    <?php $_configurable = $_product->getTypeInstance()->getUsedProductIds(); ?>
    <?php foreach ($_configurable as $_config): ?>
        <?php $_simpleproduct = Mage::getModel('catalog/product')->load($_config); ?>
        <?php //Magic php with a $_simpleproduct. ?>
    <?php endforeach; ?>
<?php endif; ?>
like image 5
Denis Spalenza Avatar answered Oct 15 '22 11:10

Denis Spalenza