-
How to get total quantity left of different color in configurable product ?
over 8 years ago
over 8 years ago
Hello Readers,
This tutorial is going explain "How to get total quantity left of simple products with different color in configurable product".
Lets see how can we do it:
paste the below code in product view file (i.e catalog/product/view.phtml)
<?php if($_product->getTypeId() == "configurable"):
$ids = $_product->getTypeInstance()->getUsedProductIds(); ?>
<ul>
<?php foreach ($ids as $id) :
$simpleproduct = Mage::getModel('catalog/product')->load($id); ?>
<?php $total = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($simpleproduct)->getQty(); ?>
<li><?php echo $simpleproduct->getAttributeText('color')." - ". ("Only ".$total." Left!"); ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<?php if($_product->getTypeId() == "configurable"):
$ids = $_product->getTypeInstance()->getUsedProductIds(); ?>
<ul>
<?php foreach ($ids as $id) :
$simpleproduct = Mage::getModel('catalog/product')->load($id); ?>
<?php $total = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($simpleproduct)->getQty(); ?>
<li><?php echo $simpleproduct->getAttributeText('color')." - ". ("Only ".$total." Left!"); ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<?php if($_product->getTypeId() == "configurable"):
In if condition, we checked whether the product is configurable or not. If it is, then we have called getTypeInstance()->getUsedProductIds() method (In short way, this is called function to function calling). Using this method, we can get all the ids of simple products linked with configurable products.
$simpleproduct = Mage::getModel('catalog/product')->load($id); ?>
The above statement will load all simple products in foreach() loop to create all object of simple products using getModel() method.
By using $simpleproduct as object we called getQty() & getAttributeText('color') method to get total quantity left of simple products with different color which is associated with configurable product.
0 Comment(s)