【问题标题】:Creating programatically a category when Flat mode is enabled in Magento在 Magento 中启用平面模式时以编程方式创建类别
【发布时间】:2023-03-28 22:51:01
【问题描述】:

我想以编程方式在模块的数据文件夹中创建一个类别。平面类别选项已启用。

当我尝试像这样创建一个类别时:

$category
    ->setStoreId(0)
    ->setName('My category')
    ->setUrlKey('club-campaigns')
    ->setPath($rootCategory->getPath())
    ->setIsActive(1)
    ->setIsAnchor(1)
    ->setIncludeInMenu(1)
    ->addData($data)
    ->setCustomDesignApply(1)
    ->save();

我收到一条错误消息,指出 catalog_category_flat 不存在。好的,所以我知道平面类别信息保存在 catalog_category_flat_store_storenumber 表中。我查看了数据库,我有以下表格:

catalog_category_flat_store_1
catalog_category_flat_store_2
catalog_category_flat_store_3
catalog_category_flat_store_4
catalog_category_flat_store_5
catalog_category_flat_store_6

我想为商店 6 创建一个类别。很好,现在如果我这样做:

$category
    ->setStoreId(6)
    ->setName('My category')
    ->setUrlKey('club-campaigns')
    ->setPath($rootCategory->getPath())
    ->setIsActive(1)
    ->setIsAnchor(1)
    ->setIncludeInMenu(1)
    ->addData($data)
    ->setCustomDesignApply(1)
    ->save();

该类别的创建没有错误,它在 catalog_category_flat_store_6 中设置了信息,但如果我转到 admin>Manage Categories 并且看不到我创建的类别。

我认为,当我创建一个类别时,我应该设置 admin(0) 的商店 ID,这样我就可以在管理面板中看到它,但是我得到了上面的错误,如果我使用商店 6 创建,我不会'在管理员中看不到它。我真的被困住了。

如何以编程方式正确创建我的类别而不会出现问题?

【问题讨论】:

  • 重新索引有帮助吗?
  • 我会尝试重新索引,但我认为我应该在创建类别之前重新索引。因为我看到像 catalog_category_flat_store_X 这样的表是在重新索引过程之后创建的。

标签: magento categories flat


【解决方案1】:

动态创建类别:

$category = Mage::getModel('catalog/category');
$category->setStoreId(Mage::app()->getStore()->getId());

$cat['name'] = "Custom Category Name here";
$cat['path'] = "1/2/30"; //parent relationship..
$cat['description'] = "categorie's description";
$cat['is_active'] = 1;
$cat['is_anchor'] = 0; //for layered navigation
$cat['page_layout'] = 'two_columns_left';
$cat['url_key'] = "custom-category"; //url to access this category
$cat['image'] = "custom-category.jpg";

$category->addData($cat);
$category->save();

然后动态重新索引 catalog_category_flat:

$process = Mage::getSingleton('index/indexer')->getProcessByCode('catalog_category_flat');
$process->reindexEverything();

【讨论】:

  • 如何获取刚刚创建的类别ID?
  • $category->getId();通过使用此获取当前插入的类别 ID
猜你喜欢
  • 1970-01-01
  • 2012-03-08
  • 1970-01-01
  • 1970-01-01
  • 2014-10-19
  • 2016-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多