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.???

1 Answer 1

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') ?> 
24
  • 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. Commented Aug 20, 2019 at 13:40
  • also Enzo Perrotta, if i use the second code - nothing happen, it's like begin, no change. Commented Aug 20, 2019 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)): ?> Commented Aug 20, 2019 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? Commented Aug 20, 2019 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 Commented Aug 20, 2019 at 13:57

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.