Magento 1.9 – Create and Update Categories Programmatically

categoryimportmagento-1.9

Hy All! I'm searching everywhere but i cant find a solution.

I'm trying to set display mode and cms block during category import..

Here my snippet

private function import_category(){
    global $cl, $_category;

    $categories = $this->base_cat.str_replace('//', '/', rtrim($this->build_cats($this->categoria_corrente['CategoryID'], $this->cat_tree,''),'/')); // these return the full path (/Product/Cat1/Cat2)

    $id = $cl->getIdFromPath($categories); // This return the category id if exists - it works!
    $parent_id = $cl->getIdFromPathParent($categories); // This return the parent category id if exists - it works!
    if($parent_id){
        $parent    = Mage::getModel('catalog/category')->load($parent_id);
        $category = Mage::getModel('catalog/category');
        if($store_id = $this->getStoreByCode($this->store)){ // get store by code works..
            $category->setStoreId($store_id);
            if ($id) {
              $category->load($id);
            }
            // $this refers to my class. this function is inside a bigger class.
            // $this->categoria_corrente is an array with current category data.
            $general['name'] = $this->categoria_corrente['Description'];
            $general['description'] = $this->categoria_corrente['Description'];
            $general['meta_title'] = $this->categoria_corrente['Description'];
            $general['meta_keywords'] = $this->categoria_corrente['Description'];
            $general['meta_description'] = $this->categoria_corrente['Description'];
            $general['display_mode'] = "PAGE"; // this DONT work :(
            $general['landing_page'] = "71";  // this DONT work :( - 71 is the correct static block.
            $general['is_active'] = 1;
            $general['is_anchor'] = 1;
            $general['include_in_menu'] = 0;
            $general['url_key'] = Mage::getModel('catalog/product_url')->formatUrlKey( $this->categoria_corrente['Description'] );
            $general['image'] = "";


            $category->addData($general);
            $category->setDisplayMode('PAGE'); // also this dont work...
            $category->setLandingPage(71);

            try {
                $category->save();
                if($id){
                    echo "<br>Category updated";
                } else {
                    echo "<br>New category";
                    $category->move($parent_id, null); // move under parent.. - works..
                    $cl = new NAMESPACE_MODULE_Model_Category_Finder; // i've to reindex all paths in my model..
                }

            }
            catch (Exception $e){
                echo $e->getMessage();
            }
        } 
    } 
}

Best Answer

To get this to work I had to set the storeid to 0

also I created the category, saved it, then added the values for landing page and display mode.

Hope that helps!

e.g.

     $category = Mage::getModel('catalog/category');
     $category->setStoreId(0);

     ....

      try {
              $category->save();
              echo "Succeeded <br /> ";
          }
          catch (Exception $e){
                echo "Failed <br />";
          }

          $category->setDisplayMode($data['general']['display_mode']);

          if(isset($data['general']['landing_page'])) $category->setLandingPage($data['general']['landing_page']);

          try {
              $category->save();
              echo "Succeeded <br /> ";
          }
          catch (Exception $e){
                echo "Failed <br />";
          }
Related Topic