0

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;
  • can you post the whole script and can you define how you ascertained it didn't work. – Sander Mangel Feb 20 '15 at 14:11
  • First of all don't use setData(array). Use addData(array). Are you trying to change the values for a specific store view or for default settings? – Marius Feb 20 '15 at 14:22
3

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;
  • PHP Fatal error: Call to undefined method Mage_Catalog_Model_Resource_Category_Flat::saveAttribute() in /Library/WebServer/Documents/wortel/erik.php on line 26 – Evrijn Feb 20 '15 at 14:34
  • Please turn of the flat category data and try again – Sander Mangel Feb 20 '15 at 14:39
  • Yeah! Great :) with flat off it works. – Evrijn Feb 20 '15 at 14:40
  • @SanderMangel I have changed category meta title, after I paste my category URL to Facebook, here the old meta tile will get and display, many i know how many days taken to update my category meta title tag updates. – Gem Mar 15 '18 at 12:17

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.