Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to get total quantity left of different color in configurable product ?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 630
    Comment on it

    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)

    1. <?php if($_product->getTypeId() == "configurable"):
    2.  
    3. $ids = $_product->getTypeInstance()->getUsedProductIds(); ?>
    4. <ul>
    5. <?php foreach ($ids as $id) :
    6. $simpleproduct = Mage::getModel('catalog/product')->load($id); ?>
    7. <?php $total = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($simpleproduct)->getQty(); ?>
    8.  
    9. <li><?php echo $simpleproduct->getAttributeText('color')." - ". ("Only ".$total." Left!"); ?></li>
    10.  
    11. <?php endforeach; ?>
    12.  
    13. </ul>
    14. <?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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: