iVela
iVela

Reputation: 1201

How can I show configurable product stock quantity in Magento?

I have made this code to show if some configurable product has or not stock. But I can't get the stock quantity anywhere in varien/product.js

updateStock: function(){
    var tempProducts = [];
    var allowedProducts = [];

    var idColor = jQuery('#attribute85').val();
    var idTalla = jQuery('#attribute127').val();

    // productos por color 
    var options = this.getAttributeOptions(85);
    for (var i = 0; i < options.length; i++ ) {
        if (options[i].id == idColor) {
            for (var j = 0; j < options[i].products.length; j++) {
                tempProducts.push(options[i].products[j]);
            }
        }
    }

    jQuery('#input-box-127 a').each(function(index) {
        // jQuery(this).find('.talla').removeClass('seleccionado');
        jQuery(this).find('div').addClass('sin_stock');

        idTalla = jQuery(this).attr('rel');
        options = spConfig.getAttributeOptions(127);
        for (var i = 0; i < options.length; i++ ) {
            if (options[i].id == idTalla) {
                for (var j = 0; j < options[i].products.length; j++) {
                    for (var index = 0; index < tempProducts.length; index++) {
                        if (tempProducts.indexOf(options[i].products[j]) > -1) {
                            //alert(jQuery(this).text());
                            jQuery(this).find('div').removeClass('sin_stock');
                        }
                    }
                }
            }
        }
    });

    return allowedProducts.length > 0;
}, 

Is there any variable in magento where I can get the configurable product stock?

Upvotes: 1

Views: 7827

Answers (2)

Hidayet Ok
Hidayet Ok

Reputation: 111

You can get sub products list and count all sub product quantities as configurable product's quantity...

$product=Mage::getModel("catalog/product");         
$prod=$product->load($prod_id);
    $conf = Mage::getModel('catalog/product_type_configurable')->setProduct($prod);
    $col = $conf->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions();

    $total_qty=0;
    foreach($col as $sprod){
        $sprod=$product->load($sprod->getId());
        $qty = intval(Mage::getModel('cataloginventory/stock_item')->loadByProduct($sprod)->getQty());
        $total_qty+=$qty;
    }       

Upvotes: 8

Andrew
Andrew

Reputation: 12809

In Magento there is NO stock level for a configurable product.

Each child product / variation has it's own stock level so you would need to check the stock of the variation currently selected.

Upvotes: 0

Related Questions