Show link only if category has products Magento 1.9
-
05-01-2021 - |
Question
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>
Solution 2
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>
OTHER TIPS
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
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange