Jul 202012
 
require_once 'app/Mage.php';
Mage::app('default'); // Default or your store view name.

//get a new category object
$category = Mage::getModel('catalog/category');
$category->setStoreId(0); // 0 = default/all store view. If you want to save data for a specific store view, replace 0 by Mage::app()->getStore()->getId().

//if update
if ($id) {
  $category->load($id);
}

$general['name'] = "My Category";
$general['path'] = "1/2/23"; // catalog path
$general['description'] = "Great My Category";
$general['meta_title'] = "My Category"; //Page title
$general['meta_keywords'] = "My , Category";
$general['meta_description'] = "Some description to be found by meta search robots. 2";
$general['landing_page'] = ""; //has to be created in advance, here comes id
$general['display_mode'] = "PRODUCTS_AND_PAGE"; //static block and the products are shown on the page
$general['is_active'] = 1;
$general['is_anchor'] = 0;
$general['page_layout'] = 'two_columns_left';

//$general['url_key'] = "cars";//url to be used for this category's page by magento.
//$general['image'] = "cars.jpg";


$category->addData($general);

try {
    $category->save();
    echo "Success! Id: ".$category->getId();
}
catch (Exception $e){
    echo $e->getMessage();
}

  5 Responses to “How to add category programmatically in magento 1.7.0 with custom field”

  1. Erm, I’m probably a moron but if I’m just now adding the category, how can I know in advance what category id the system will assign it to build the path string with?

  2. If running this from command line be sure to add umask(0); after the require_once();. Not sure exactly why this matters but it helped me.

  3. Where do I set the parent categoryId? In your example above I can’t see how to create the relations between the single categorys (parent/children)

  4. If you look at line number 14 :

    $general[‘path’] = “1/2/23″; // catalog path

    You need to add category IDS here

  5. Where did i add this code ?please ur help is required?

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>