1

Hi so I have a hierarchy of categories, the parent of which is Ranges

Each range may have subcategories several levels deep.

What I need to do is check to see if the current product belongs to a category which is in ranges.

For example the product may be in several categories as follows:

Tables > Coffee Tables

Chairs > Comfy Chairs

Ranges > Range Name > Range

If any category is found which is an ancestor (no matter how many levels deep) or ranges, I want the logic to return true.

Here's what I have so far...

<?php 
$_product = Mage::registry('current_product');
$categoryIds = $_product->getCategoryIds();
    if(count($categoryIds) ){
        foreach ($categoryIds as $cat) {

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

            if($_category->getParentId() == '683') {
                $_catname = $_category->getName();
                $_catimg = $_category->getImageUrl();
                $_caturl = $_category->getUrl();

                ?>
                <div class="range">
                    <img src="<?php echo $_catimg;?>" alt="Part of the <?php echo $_catname;?> range" />
                    <span>
                        Part of the
                        <span>
                            <?php echo $_catname;?>
                        </span>
                        Range<br />
                        <a class="btn btn-blue btn-large" href="<?php echo $_caturl;?>">
                            View All
                        </a>
                    </span>
                </div>


            <?php }
        }
    }?>

I think the problem is that getParentId only refers to the immediate parent, I want this to return true if 683 is an ancestor on any level.

Hope that makes sense,

  • You may recurse that algorithm – nicolallias Aug 3 '15 at 12:35
  • In which file you have tried this code? – saravanavelu Aug 3 '15 at 19:52
  • you need the 683's category details if condition return true – saravanavelu Aug 3 '15 at 20:00
1

Better move this check to your block. I'd recommend use smth like this:

    $flag = false;
    $productId = 33;
    $categoryNeedle = 84;
    $cats = Mage::getModel('catalog/product')->load($productId)->getCategoryIds();
    foreach ($cats as $cat) {
        $newCat = Mage::getModel('catalog/category')->load($cat);
        $arrayPath = explode('/', $newCat->getPath());
        if (in_array($categoryNeedle, $arrayPath)) {
            $flag = true;
        }
    }

If you look through database in path field you can find full path to this category.

enter image description here

| improve this answer | |

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.