1

I have code which outputs category images, names and links so that I can use a static block to output all categories within a parent. Currently each image/title is linked even if the category being linked to has 0 products.

I only want the link to be active if the category it is linking to contains products (i.e. > 0). Otherwise there should be no link.

Can anyone please recommend an update to the code below to get this working?

    <?php
    $layer = Mage::getSingleton('catalog/layer');
    $_category   = $layer->getCurrentCategory();
    $_categories = $_category->getCollection()->addAttributeToSelect(array('url_key','name','image','all_children','is_anchor','description'))
    ->addAttributeToFilter('is_active', 1)
    ->addIdFilter($_category->getChildren())
    ->setOrder('position', 'ASC')
    ->joinUrlRewrite();
  ?>

<div class="row mt-10">
        <?php foreach ($_categories as $_category): ?>
                <?php if($_category->getIsActive()): ?>
                    <?php Mage::log($_category->debug(), null, 'mylogfile.log'); ?>
                    <div class="col-xs-12 col-sm-6 col-md-4" style="margin-bottom:10px;">
                        <div class="subcat clearfix" style="border:1px solid #333;">
                            <!--<a class="now-from-container" href="<?php echo $_category->getURL() ?>"></a>-->
                            <div class="subcat-image">
                                <a href="<?php echo $_category->getURL() ?>" title="<?php echo $this->htmlEscape($_category->getName()) ?>">-->
                                    <img src="<?php echo $this->htmlEscape($_category->getImageUrl()) ?>" width="690" alt="<?php echo $this->htmlEscape($_category->getName()) ?>" />
                                </a>
                            </div>
                            <div class="subcat-title-container">
                                <h3 style="background-color:#333;text-align:center;margin:0;padding:10px 0; font-size:22px;"><a style="color:#fff;" href="<?php echo $_category->getURL() ?>" title="<?php echo $this->htmlEscape($_category->getName()) ?>"><?php echo $this->htmlEscape($_category->getName()) ?></a></h3>
                            </div>
                        </div>
                    </div>
                <?php endif; ?>
        <?php endforeach; ?>
</div>
0

You should use this function getProductCount();

$hasProducts = Mage::getModel('catalog/category')->load($_category->getId())->getProductCount();

if($hasProducts > 0 ) {
......
}
else {
.....
}

so your final code should look like :

<?php
$layer = Mage::getSingleton('catalog/layer');
$_category   = $layer->getCurrentCategory();
$_categories = $_category->getCollection()->addAttributeToSelect(array('url_key','name','image','all_children','is_anchor','description'))
    ->addAttributeToFilter('is_active', 1)
    ->addIdFilter($_category->getChildren())
    ->setOrder('position', 'ASC')
    ->joinUrlRewrite();
?>

<div class="row mt-10">
    <?php foreach ($_categories as $_category): ?>
        <?php $hasProducts = Mage::getSingleton('catalog/category')->load($_category->getId())->getProductCount(); ?>
        <?php if($_category->getIsActive()): ?>
            <?php Mage::log($_category->debug(), null, 'mylogfile.log'); ?>
            <div class="col-xs-12 col-sm-6 col-md-4" style="margin-bottom:10px;">
                <div class="subcat clearfix" style="border:1px solid #333;">
                    <!--<a class="now-from-container" href="<?php echo // $_category->getURL() ?>"></a>-->
                    <div class="subcat-image">
                        <?php if ($hasProducts > 0) :?>
                            <a href="<?php echo $_category->getURL() ?>" title="<?php echo $this->htmlEscape($_category->getName()) ?>">
                                <img src="<?php echo $this->htmlEscape($_category->getImageUrl()) ?>" width="690" alt="<?php echo $this->htmlEscape($_category->getName()) ?>" />
                            </a>
                        <?php else: ?>
                            <img src="<?php echo $this->htmlEscape($_category->getImageUrl()) ?>" width="690" alt="<?php echo $this->htmlEscape($_category->getName()) ?>" />
                        <?php endif; ?>
                    </div>
                    <div class="subcat-title-container">
                        <h3 style="background-color:#333;text-align:center;margin:0;padding:10px 0; font-size:22px;"><a style="color:#fff;" href="<?php echo $_category->getURL() ?>" title="<?php echo $this->htmlEscape($_category->getName()) ?>"><?php echo $this->htmlEscape($_category->getName()) ?></a></h3>
                    </div>
                </div>
            </div>
        <?php endif; ?>
    <?php endforeach; ?>
</div>

i have not tested this code yet , but it should work

  • I get a blank page currently Naveed... also I think this will show nothing at all if a category has 0 products, but I simply don't want the anchor tag if a category has no products. – YorkieMagento Mar 27 '18 at 10:11
  • you can place this $hasProducts condtion around your anchor tag, if you want i can update code for you, do you want this anchor tag removed which is with in <div class="subcat-image"> ? – Naveed Asim Mar 27 '18 at 10:17
  • Hi Naveed that is correct. However, the code you have seems to get the product count from the parent category... rather than for the sub-categories/children. Thank you. – YorkieMagento Mar 27 '18 at 10:22
  • I have added condition around a tag, you are getting current category and using category/layered model and then its children, so iteration is on current category children from where i picket $_category->getId() which would be children category id in that case and will check if child has products or not and show tag on that basis. – Naveed Asim Mar 27 '18 at 10:35
  • Hi Naveed... thanks for your hard work, unfortunately I am still getting a blank page... – YorkieMagento Mar 27 '18 at 10:52
0

I managed to get this working using the below code:

 <?php
    $layer = Mage::getSingleton('catalog/layer');
    $_category   = $layer->getCurrentCategory();
    $_categories = $_category->getCollection()->addAttributeToSelect(array('url_key','name','image','all_children','is_anchor','description'))
    ->addAttributeToFilter('is_active', 1)
    ->addIdFilter($_category->getChildren())
    ->setOrder('position', 'ASC')
    ->joinUrlRewrite();
  ?>

<div class="row mt-10">
        <?php foreach ($_categories as $_category): ?>                  
                <?php $hasProducts = $_category->getProductCount(); ?>
                <?php if($_category->getIsActive()): ?>
                    <?php Mage::log($_category->debug(), null, 'mylogfile.log'); ?>
                    <div class="col-xs-12 col-sm-6 col-md-4" style="margin-bottom:10px;">
                        <div class="subcat clearfix" style="border:1px solid #333;">
                            <?php /* echo $hasProducts */ ?>
                            <!--<a class="now-from-container" href="<?php echo $_category->getURL() ?>"></a>-->
                            <div class="subcat-image">
                                <?php if ($hasProducts > 0) :?>
                                    <a href="<?php echo $_category->getURL() ?>" title="<?php echo $this->htmlEscape($_category->getName()) ?>">
                                        <img src="<?php echo $this->htmlEscape($_category->getImageUrl()) ?>" width="690" alt="<?php echo $this->htmlEscape($_category->getName()) ?>" />
                                    </a>
                                <?php else: ?>
                                    <img src="<?php echo $this->htmlEscape($_category->getImageUrl()) ?>" width="690" alt="<?php echo $this->htmlEscape($_category->getName()) ?>" />
                                <?php endif; ?>
                            </div>
                            <div class="subcat-title-container">
                                <h3 style="background-color:#333;text-align:center;margin:0;padding:10px 0; font-size:22px; color:#fff;"><!--<a style="color:#fff;" href="<?php echo $_category->getURL() ?>" title="<?php echo $this->htmlEscape($_category->getName()) ?>">--><?php echo $this->htmlEscape($_category->getName()) ?><!--</a>--></h3>
                            </div>
                        </div>
                    </div>
                <?php endif; ?>
        <?php endforeach; ?>
</div>

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.