0

I search everywhere but cannot find a good answer.

In my 1.9 magento rwd theme, I set for single product to display the stock qty after stock status, but when i put some simple products to a grouped product type then in the table of grouped products I can see the correct stock qty and in main window i see and is display for total group stock qty = 0

This is my code:

rwd\default\template\catalog\product\view\addtocart.phtml

<?php if($_product->isSaleable()): ?>
    <br></br>
    <?php if ($this->displayProductStockStatus()): ?>
    <?php if ($_product->isAvailable()): ?>
        <p class="availability in-stock"><?php echo $this->__('Cantitate disponibila :') ?> <span><!--<?php echo $this->__('In stock') ?>--><?= (int) Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty()?></span> <?php echo $this->__('buc.') ?></p>
    <?php else: ?>
        <p class="availability out-of-stock"><?php echo $this->__('Cantitate disponibila :') ?> <span><!--<?php echo $this->__('In stock') ?>--><?= (int) Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty()?></span> <?php echo $this->__('buc.') ?>
        </p>
        <?php echo $this->__('Availability:') ?> <span><?php echo $this->__('Out of stock') ?></span></p>
        <p class="availability out-of-stock"><span></br><?php echo $this->__() ?><span style="color:#0b9817;text-align:center;">Stoc Furnizor</span></br><span style="color:#de4231;text-align:center;">Comanda telefonic 0785.737.227</span></p>
    <?php endif; ?>
    <?php endif; ?>
    <?php echo $this->__('COD PRODUS: ') ?><?php echo $_product->getSku() ?><br/>
    <?php echo $_product->getAttributeText('transport_01') ?>

Please see the image: enter image description here

Can someone help me or to display total stock of asociated products or to hide only the main stock code.???

0

the configurable product in Magento doesn't store the sum of all the stocks related to its simple associated products.

A quick win solution could be to hide the main stock qty by changing the initial condition from: <?php if ($this->displayProductStockStatus()): ?>

to

<?php if ($this->displayProductStockStatus() && ($_product-> getTypeId() != Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE)): ?>

Alternatively, you should sum all the stock quantities for every simple product

This for configurable products

$totalStock = 0;
foreach ($_product->getTypeInstance(true)->getUsedProducts(null, $_product) as $simpleProduct) {
    $simpleStock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($simpleProduct)->getQty();
    $totalStock += $simpleStock;
}

This for grouped products

$totalStock = 0;
foreach ($_product->getTypeInstance(true)->getAssociatedProducts($_product) as $simpleProduct) {
    $simpleStock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($simpleProduct)->getQty();
    $totalStock += $simpleStock;
}

But performance-wise this is not optimal, as it loads a product model for each associated product.

So, your final code could be

<?php if($_product->isSaleable()): ?>
    <?php if ($this->displayProductStockStatus()): ?>
        <?php
        $productStock = 0;
        if ($_product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE) {
            foreach ($_product->getTypeInstance(true)->getUsedProducts(null, $_product) as $simpleProduct) {
                $simpleStock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($simpleProduct)->getQty();
                $productStock += $simpleStock;
            }
        } elseif ($_product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_GROUPED) {
            foreach ($_product->getTypeInstance(true)->getAssociatedProducts($_product) as $simpleProduct) {
                $simpleStock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($simpleProduct)->getQty();
                $productStock += $simpleStock;
            }
        } else {
            $productStock = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty();
        }
        ?>
        <?php if ($_product->isAvailable()): ?>
            <p class="availability in-stock"><?php echo $this->__('Cantitate disponibila :') ?> <span><!--<?php echo $this->__('In stock') ?>--><?= $productStock ?></span> <?php echo $this->__('buc.') ?></p>
        <?php else: ?>
            <p class="availability out-of-stock"><?php echo $this->__('Cantitate disponibila :') ?> <span><!--<?php echo $this->__('In stock') ?>--><?= $productStock?></span> <?php echo $this->__('buc.') ?></p>
            <?php echo $this->__('Availability:') ?> <span><?php echo $this->__('Out of stock') ?></span></p>
            <p class="availability out-of-stock"><span></br><?php echo $this->__() ?><span style="color:#0b9817;text-align:center;">Stoc Furnizor</span></br><span style="color:#de4231;text-align:center;">Comanda telefonic 0785.737.227</span></p>
        <?php endif; ?>
    <?php endif; ?>
    <?php echo $this->__('COD PRODUS: ') ?><?php echo $_product->getSku() ?><br/>
    <?php echo $_product->getAttributeText('transport_01') ?>
|improve this answer|||||
  • Enzo Perrotta - if i use: <?php if($_product->isSaleable() && ($_product->getProductType() != Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE): ?> the i will broke my theme, because disapear all butons and righr bar. – Ciprian Poiana Aug 20 '19 at 13:40
  • also Enzo Perrotta, if i use the second code - nothing happen, it's like begin, no change. – Ciprian Poiana Aug 20 '19 at 13:44
  • My fault. Try hiding the stock status by replacing <?php if ($this->displayProductStockStatus()): ?> with <?php if ($this->displayProductStockStatus() && ($_product->getProductType() != Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE)): ?> – Enzo Perrotta Aug 20 '19 at 13:47
  • i replace the code and is accepted by theme, but don't change anything. Is like in my initial photo. Any other idea? – Ciprian Poiana Aug 20 '19 at 13:54
  • Also, the second snippet of code contained a mistake...I apologize, now it's fixed. Have a look if it works as intended – Enzo Perrotta Aug 20 '19 at 13:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.