1

lets take a look at some code real quick and see if we can collectively diagnose the error residing within this code.

    <span>
    <?php
    $_category = array_unique($_category);
    foreach ($_category as $cat) {
            var_dump($cat);
            $category = $collection->load($cat);
            var_dump($category->getName());
            if($category->getName()) {
                echo '<a href="' . $category->getUrl() . '">More <strong>Of the ' . $category->getName() . '</strong></a>';
                echo 'test'
            }
            unset($category);
        }
    ?>
    </span>&nbsp;

The above code is used to display category URLs along within their respective names to the user. Easy enough, right? However -- many of our products are shared between sites and as a result some category getNames do not resolve in one of our much smaller stores. This isn't an issue anywhere else, as far as I can tell, but here. When a the if($category->getName) boolean is found to be true for a category, but the next one is found to be false it will repeatedly output the previously echo'd statement.

So it will look something like this:

input:

$_category={123,124,125,126} // only 124 is resolving to a name.

output:

string(3)"123"NULLstring(3)"124"string(4)"same"

More of the same

teststring(3)"125"string(4)"same"

More of the same

teststring(3)"126"string(4)"same"

More of the same

test

After further testing it seems that the $category variable isn't changing after each iteration of the for look. I've even tried to unset($category) after every iteration with no success.

  • Do not load anything inside a loop if possible, it will make your site slower since the load is a resource consuming operation. Try gathering your collection before you loop through the object instead. – mbalparda Jan 6 '15 at 19:59
5

Hm, the code looks weird... You are trying to load a category by ID from some collection. It's pretty weird... So, look, what I would have done (example):

echo '<pre>';
$categoryIds = array(1,2,3,4,5);
$collection = Mage::getModel('catalog/category')->getCollection();
$collection
    ->addAttributeToSelect("*")
    ->addFieldToFilter('entity_id', array('in', $categoryIds));
foreach ($collection as $category) {
    echo $category->getName() . PHP_EOL;
}

There are could be mistakes but my idea following:

  1. get the category collection
  2. add filtering by your IDs
  3. load it and get category models.

Or another scenario (not so fast)

  1. "foreach" category IDs
  2. and in "each" get new category object instance via Mage::getModel('catalog/category')->load($yourCategoryId)
| improve this answer | |
2

it simple issue you need load Model factory class for each times.

// add this

$collection=Mage::getModel('catalog/category');

before

 $category = $collection->load($cat);
| improve this answer | |
  • reloading the collection every time seems to work, however, it isn't very efficient. Thanks though! – easymoden00b Jan 6 '15 at 20:13

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.