I am trying to display a custom attribute in the backend of magento on the category pages where the category levels are 2nd level.

I found an tutorial online which enabled me to add a custom attribute to the category pages. The code that I am currently using is:

require_once "app/Mage.php";


$installer = new Mage_Sales_Model_Mysql4_Setup;

$attribute  = array(
    'type' => 'text',
    'label'=> 'Out of stock msg',
    'input' => 'text',
    'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'visible' => true,
    'required' => false,
    'user_defined' => true,
    'default' => "",
    'group' => "General Information"

$installer->addAttribute('catalog_category.level', 'out_of_stk', $attribute);


Any ideas as to how I go about only applying this to second level categories? Thanks

1 Answer 1


Do not bother that it is filled somewhere else.

Only use the values from level 2 you can simply find level 2 category by

// Walk categories
foreach ($product->getCategoryCollection() as $category) {
    // Find level 2 category
    while ($category->getLevel() > 2) {
        $category = $category->getParentCategory();

    // If found, stop searching
    if ($category->getLevel() == 2) {

$outOfStockMessage = '';
if ($category->getLevel() == 2) {
    // Set message from category
    $outOfStockMessage = $category->getData('out_of_stck');

echo $outOfStock;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.