Magento 1.7 – How to Change Category Meta Title and Description

magento-1.7

A lot of different people filled the the category's of magento, at the moment we have a lot of difference phrases in the meta discription of meta title.

I have written a script that update the category, but none of it worked.

Option 1:

            $_category->setMetaTitle($general['meta_title']);
            $_category->setMetaDescription($general['meta_description']);

Option 2:

        $_category->setData(array(
            'meta_title'    => $general['meta_title'],
            'meta_discription'  => $general['meta_description']
        )); 

Ofcourse started and end with :

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

Any ideas how i can update a category programmatically ?

As requested complete script:

<?php
 ini_set("memory_limit","2048M");
 set_time_limit(0);
 require 'app/Mage.php';
Mage::app()->setCurrentStore(8);

$_helper = Mage::helper('catalog/category');
$_categories = $_helper->getStoreCategories();
$currentCategory = Mage::registry('current_category');

 if (count($_categories) > 0):
foreach($_categories as $_category):
echo $_category->getName();
    echo "\n";

    $general['meta_title'] = trim($_category->getName()).' kopen? Bestel online bij Wortel'; //Page title
                    $general['meta_description'] = "Koop '.trim($_category->getName()).' bij Wortel. .
                            Het juiste advies voor '.trim($_category->getName()).'. Wortel maakt blij.";


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


                $_category->setMetaTitle($general['meta_title']);
                $_category->setMetaDescription($general['meta_description']);
        /*
                 $_category->setData(array(
                    'meta_title'    => $general['meta_title'],
                    'meta_discription'  => $general['meta_description']
                ));
*/
               $_category->save();

            // getMetaTitle

            $check = Mage::getModel('catalog/category')->load($_category->getId())->getMetaTitle();
        echo $check;
        echo "\n";

        endforeach;
 endif;

Best Answer

To limit the load on the database and the server you might want to prevent loading the whole category. Haven't tested this but it should work

ini_set("memory_limit","2048M");
set_time_limit(0);
require 'app/Mage.php';

Mage::app()->setCurrentStore(0);

$collection = Mage::getModel('catalog/category')->setStoreId(8)
   ->getCollection()
   ->addAttributeToSelect(array('name'));


if ($_collection->getSize()):
   foreach($_collection as $_category):

      $_category->setMetaTitle(trim($_category->getName()).' kopen? Bestel online bij Wortel');
      $_category->setMetaDescription("Koop ".trim($_category->getName())." bij Wortel. Het juiste advies voor ".trim($_category->getName()).". Wortel maakt blij.");
      $_category->getResource()->saveAttribute($_category, 'meta_title');
      $_category->getResource()->saveAttribute($_category, 'meta_description');

   endforeach;
 endif;