【问题标题】:Adding new categories and subcategories - Magento添加新类别和子类别 - Magento
【发布时间】:2012-07-04 13:29:48
【问题描述】:

我需要的是从 .csv 文件中添加新的类别和子类别。我可以创建新的根类别,但我不知道如何制作子类别:

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

  $rootCategory['name'] = 'Reserved';
  $rootCategory['path'] = "1"; // for root category
  $rootCategory['display_mode'] = "PRODUCTS_AND_PAGE";
  $rootCategory['is_active'] = 1;

  $category->addData($rootCategory);
$parentCategory= $category; // doesn't work, but i want here to get this (root) category id
  try {
    $category->save();
  }
  catch (Exception $e){
    echo $e->getMessage();
  }

CSV 文件的格式如下:

root_cat_id;subcat_id;subcat_name;subsubcat_id;subsubcat_name;
ex.
1;2;Animal;3;Dog;

如何获取刚刚添加的类别的id,并添加与该类别相关的子类别?

提前致谢

【问题讨论】:

    标签: php magento categories


    【解决方案1】:

    要使一个类别成为另一个类别的子类别,请在保存之前将路径设置为父类别路径。

    $childCategory->setPath($parentCategory->getPath())
    

    子类别保存后(因此有ID),在调用save()后,ID会自动附加到路径属性中。
    Magento 还会自动为你设置 parent_id 和 level 属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-22
      • 2012-10-02
      • 1970-01-01
      • 1970-01-01
      • 2011-07-30
      • 1970-01-01
      • 2016-07-29
      • 1970-01-01
      相关资源
      最近更新 更多