doubo7131 2011-08-28 18:20
浏览 25

Magento 1.5x以编程方式添加新类别并返回类别ID

I was wondering how to create new categories programmatically in magento 1.5xx. Also I need to return the category ID back once its been created.

  • 写回答

1条回答 默认 最新

  • dongzouban9871 2011-08-30 08:45
    关注

    I wrote an initial post here which was completely wrong - it's not as easy to find the answer to this on Google as it should be!

    Anyway, here's some code I'm using in Magento 1.5.0.1

    -- The parent category you want to work with.
    $parentCategory = $this->getParentCategory(); 
    -- This method needs to refer to the static block you want to display if you're using one.
    $categoryBlockId = $this->getCategoryBlockId(); 
    
    $category = Mage::getModel( 'catalog/category' );
    $category->setStoreId( $storeId );
    $category->setName( $categoryName ); -- The name of the category
    $category->setUrlKey( $categoryUrlkey ); -- The category's URL identifier
    $category->setIsActive( 1 ); -- Is it enabled?
    $category->setIsAnchor( 0 ); -- I think this relates to whether it shows in navigation.
    -- Display mode can be 'PRODUCTS_AND_PAGE', 'PAGE', or (I think) 'PRODUCTS'
    $category->setDisplayMode( $displayMode ); 
    $category->setPath( $parentCategory->getPath() ); -- Important you get this right.
    -- This is required if DisplayMode is 'PRODUCTS_AND_PAGE' or 'PAGE'.
    $category->setLandingPage( $categoryBlockId ); 
    $category->setPageTitle( 'Your Page Title' );
    $category->save()
    
    $categoryId = $category->getId();
    

    There are other properties you can use as well (they correlate to the form fields when adding a category in the admin panel) but I don't have examples for them unfortunately. There are good posts out there online - I used them to create the above - but you might need to dig deeply via Google to find them.

    I hope this helps and that you didn't read my first answer which wouldn't help.

    Cheers, Zac

    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法