Easy PHP category help

Discussion in 'PHP' started by jtaylo49, Apr 26, 2012.

  1. #1
    So in Magento they use php to dynamically call the product categories in the left menu. However, some categories are empty as of right now and I want to use code pull the category count and only display categories with product in them, but if product is added later, the code will allow the category to be displayed. It should be easy but I am not a php coder. I have pasted the code which calls the categories and an article that explains how to this knowing the category ids, but i want it to be dynamic. Any help would be greatly appreciated!

    http://prattski.com/2008/12/17/magento-display-only-if-there-are-products-in-the-category/

    <?php
    /**
     * Magento Enterprise Edition
     *
     * NOTICE OF LICENSE
     *
     * This source file is subject to the Magento Enterprise Edition License
     * that is bundled with this package in the file LICENSE_EE.txt.
     * It is also available through the world-wide-web at this URL:
     * http://www.magentocommerce.com/license/enterprise-edition
     * If you did not receive a copy of the license and are unable to
     * obtain it through the world-wide-web, please send an email
     * to license@magentocommerce.com so we can send you a copy immediately.
     *
     * DISCLAIMER
     *
     * Do not edit or add to this file if you wish to upgrade Magento to newer
     * versions in the future. If you wish to customize Magento for your
     * needs please refer to http://www.magentocommerce.com for more information.
     *
     * @category    design
     * @package     enterprise_default
     * @copyright   Copyright (c) 2009 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
     * @license     http://www.magentocommerce.com/license/enterprise-edition
     */
    ?>
    <?php
    /**
     * Category left navigation
     *
     * @see Mage_Catalog_Block_Navigation
     */
    ?>
    <?php
    if (!Mage::registry('current_category')) {
        return;
    }
    $_categories = $this->getCurrentChildCategories();
    $_count      = is_array($_categories)?count($_categories):$_categories->count();
    ?>
    <?php if($_count): ?>
    <div class="block block-layered-nav">
        <div class="block-title">
            <h2><?php echo $this->__('Browse By') ?></h2>
        </div>
        <div class="block-content">
            <dl>
                <dt><?php echo $this->__('Category') ?></dt>
                <dd>
                    <ol>
                    <?php foreach ($_categories as $_category): ?>
                        <?php if($_category->getIsActive()): ?>
                        <li>
                            <a href="<?php echo $this->getCategoryUrl($_category) ?>"<?php if ($this->isCategoryActive($_category)): ?> class="current"<?php endif; ?>><?php echo $this->htmlEscape($_category->getName()) ?></a> (<?php echo $_category->getProductCount() ?>)
                        </li>
                        <?php endif; ?>
                    <?php endforeach ?>
                    </ol>
                </dd>
            </dl>
        </div>
    </div>
    <?php endif; ?>
    
    PHP:
     
    jtaylo49, Apr 26, 2012 IP
  2. e-abi

    e-abi Member

    Messages:
    122
    Likes Received:
    1
    Best Answers:
    3
    Trophy Points:
    38
    #2
    Replace:

     <?php if($_category->getIsActive()): ?>
    PHP:
    with this:

     <?php if($_category->getIsActive() && $_category->getProductCount() > 0): ?>
    PHP:
    And now it should only render categories which have products in it.
     
    e-abi, Apr 26, 2012 IP
  3. jtaylo49

    jtaylo49 Peon

    Messages:
    66
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    dont you have to do something with the if statement to say if count > 0 to display it?
     
    jtaylo49, Apr 26, 2012 IP
  4. jtaylo49

    jtaylo49 Peon

    Messages:
    66
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    nevermind i see that now
     
    jtaylo49, Apr 26, 2012 IP
  5. jtaylo49

    jtaylo49 Peon

    Messages:
    66
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    thanks so much! it worked!
     
    jtaylo49, Apr 26, 2012 IP
  6. heloraghu

    heloraghu Member

    Messages:
    133
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    46
    #6
    tags and wrapper tags are very easy and very useful in php
     
    heloraghu, Apr 29, 2012 IP
  7. Technary

    Technary Member

    Messages:
    429
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    33
    #7
    Best Answer. I also wanted to tell you the same code. Cheers!
     
    Technary, Apr 29, 2012 IP
  8. webxtech

    webxtech Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    If any Question About php and other development So please Connect here OR Visit hare webxtechnology.com
    Thanks
     
    webxtech, May 3, 2012 IP