0

Looking at this page and code snippet to display qty on hand on a product page, I'd like to apply this to only certain categories - effectively filter this code snippet by category ID.

http://www.woolleydesign.net/2012/06/magento-show-inventory-levels-on-product-pages/ http://www.woolleydesign.net/wp-content/uploads/downloads/2012/06/default.txt

1
<?php if ($_product->isAvailable()): ?>
    <p class="availability in-stock"><?php echo $this->__('Qty on Hand:') ?> <span><!--<?php echo $this->__('In stock') ?>--><?= (int) Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty()?></span></p>

Replace above code with following:

    <?php if ($_product->isAvailable()): ?>
            <?php 
                  $cats = $_product->getCategoryIds();
                  $categories = array(1,2,3,...); // ENTER CERTAIN CATEGORY IDs for which you want to show the 'Qty on Hand:'
                  $flag = 0;
                  foreach ($cats as $category_id) {
                    if (in_array($category_id,$categories))
                      $flag = 1;
                  }
                  if (!$flag): ?>
                     <p class="availability in-stock"><?php echo $this->__('Qty on Hand:') ?> <span><!--<?php echo $this->__('In stock') ?>--><?= (int) Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty()?></span></p>
            <?php else: ?>
                     <p class="availability in-stock"><?php echo $this->__('Availability:') ?> <span><?php echo $this->__('In stock') ?></span></p>
            <?php endif; ?>

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.