get stock of selected simple in magento -


i wondering if possible stock of selected item ( i.e. in dropdown) in configurable product.

if example have shirt, multiple simples represent sizes. when select small, want stock of small shirts. if change selection medium, stock of medium shirts.

notice there no page refreshing.

i've done similar, extended mage_catalog_block_product_view_type_configurable, getjsonconfig() method, provides data dropdowns on configurable product.

something this:

class graphicalliance_stockvalues_block_catalog_product_view_type_configurable extends mage_catalog_block_product_view_type_configurable {    public function getjsonconfig()     {         $config = parent::getjsonconfig();         $config = mage::helper('core')->jsondecode($config);          foreach ($config['attributes'] $attid=>$attinfo) {           foreach ($attinfo['options'] $key=>$attoption) {             // stock value per product             $stocks = array();             foreach ($attoption['products'] $prod) {               $_product = mage::getmodel('catalog/product')->load($prod);               $_qty = mage::getmodel('cataloginventory/stock_item')->loadbyproduct($_product)->getqty();               $stocks[$prod] = (int) $_qty;             }             $config['attributes'][$attid]['options'][$key]['stock'] = $stocks;           }         }          return mage::helper('core')->jsonencode($config);     } } 

that makes sure stock value through frontend each variation, don't have refresh page. need modify javascript handler update stock display when size/colour selected.

in js/varien.configurable.js add function product prototpye object, this:

reloadstock: function(){         for(var i=this.settings.length-1;i>=0;i--){             var selected = this.settings[i].options[this.settings[i].selectedindex];             if(selected.config){                 var allowedproducts = selected.config.allowedproducts;                 if (allowedproducts.length==1 && selected.config.stock) {                   var productstock = parseint(selected.config.stock[allowedproducts[0]]);                   if (productstock==0) {                     $('product-stock').innerhtml = 'out of stock';                   } else if (productstock<=1) {                     $('product-stock').innerhtml = productstock + ' in stock';                   } else {                     $('product-stock').innerhtml = '';                   }                 }             }         }     }, 

note need element id 'product-stock' stock display text displayed.

you can call function necessary, example add configureelement function in same file, if remember correctly run on page load:

this.reloadstock(); 

look wherever reloadprice() being called , should go below it.

these code samples have been edited bit remove bits aren't relevant question, still syntactically correct.

happy further, hans


Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -