Saturday, June 18, 2016

Change inventory on select configurable options

=> Please follow the below steps for the inventory options changes.

Step 1 :   \app\design\frontend\rwd\default\template\catalog\product\view\type\options\configurable.phtml file in replace below code

<?php
$_product    = $this->getProduct();
$_attributes = Mage::helper('core')->decorateArray($this->getAllowAttributes());
$_jsonConfig = $this->getJsonConfig();
$_renderers = $this->getChild('attr_renderers')->getSortedChildren();
?>
<?php if ($_product->isSaleable() && count($_attributes)):?>
    <dl>
    <?php foreach($_attributes as $_attribute): ?>
        <?php
        $_rendered = false;
        foreach ($_renderers as $_rendererName):
            $_renderer = $this->getChild('attr_renderers')->getChild($_rendererName);
            if (method_exists($_renderer, 'shouldRender') && $_renderer->shouldRender($_attribute, $_jsonConfig)):
                $_renderer->setProduct($_product);
                $_renderer->setAttributeObj($_attribute);
                echo $_renderer->toHtml();
                $_rendered = true;
                break;
            endif;
        endforeach;

        if (!$_rendered):
        ?>
        <dt><label class="required"><em>*</em><?php echo $_attribute->getLabel() ?></label></dt>
        <dd<?php if ($_attribute->decoratedIsLast){?> class="last"<?php }?>>
            <div class="input-box">
                <select name="super_attribute[<?php echo $_attribute->getAttributeId() ?>]" onchange = "spConfig.getIdOfSelectedProduct()" id="attribute<?php echo $_attribute->getAttributeId() ?>" class="required-entry super-attribute-select">
                    <option><?php echo $this->__('Choose an Option...') ?></option>
                  </select>
              </div>
        </dd>
        <?php endif; ?>
    <?php endforeach; ?>
    </dl>
 
<script type="text/javascript">
        var spConfig = new Product.Config(<?php echo $this->getJsonConfig() ?>);

spConfig.getIdOfSelectedProduct = function (mId) {
var sId = "myid"+mId;
var mId = "attribute"+mId;
var s = jQuery("select#"+mId+" option:selected").text();

if (s.indexOf('+$') >= 0)
{
s = s.substring(0, s.indexOf('+$'));
}
else if (s.indexOf('-$') >= 0)
{
s = s.substring(0, s.indexOf('-$'));
}
else
{
s = s;
}

jQuery("#"+sId).html(s);

//this.settings[i].attributeId =  'attribute'+attributeId;

var existingProducts = new Object();
//console.log(this);
    for (var i = this.settings.length - 1; i >= 0; i--) {
        var selected = this.settings[i].options[this.settings[i].selectedIndex];

if (selected.config) {
console.log("SpConfig : "+selected.config.products.length);
            for (var iproducts = 0; iproducts < selected.config.products.length; iproducts++) {
                var usedAsKey = selected.config.products[iproducts] + "";
//console.log("UsedasKey : "+usedAsKey);
                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
    }
console.log(sizeOfExistingProducts);

    if (sizeOfExistingProducts == 1 ) {
currentSimpleProductId;
jQuery.ajax({
type: "POST",
url: "<?php echo $this->getBaseUrl().'sku.php'?>",
data:"id="+currentSimpleProductId,
success: function(msg)
{
jQuery("#product-sku").html(msg);

//jQuery("#product-sku").show();

}
});
    }
}

    </script>

    <?php echo $this->getChildHtml('after') ?>
<?php endif;?>

Step 2 : Create A File sku.php in magento root folder 

<?php
include_once 'app/Mage.php';
Mage::init();

$id = $_POST['id'];
$obj = Mage::getModel('catalog/product')->load($id);
$stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($obj);
echo $stock->getQty();
?>

Step 3 :  app\design\frontend\rwd\default\template\catalog\product\view.phtml file in add div 

<div id ="product-sku"> </div>



0 comments:

Post a Comment