如何将类别移动到包含所有子类别的其他类别?
我尝试过以下解决方案:
$nodeId = 2269;
$parentId = 2268;
$tree = Mage::getResourceModel('catalog/category_tree')->load();
$node = $tree->getNodeById($nodeId);
$parentNode = $tree->getNodeById($parentId);
$parentChildren = explode(',', $parentNode->getChildren());
$afterId = array_pop($parentChildren);
$prevNode = $tree->getNodeById($afterId);
if (!$prevNode || !$prevNode->getId()) {
$prevNode = null;
}
$tree->move($node, $parentNode, $prevNode);
但是我的结果有点扭曲。如果我移动到根类别移动有效,但如果我移动到子类别,我会得到错误的结果并消失类别。
这些是数据库中字段路径的值:
Old: 1/2/3/2175/2269
New: 1/2/3/2175/2226/2268/2269
Correct: 1/2/3/2226/2268/2269
答案 0 :(得分:16)
解决方案很简单,这个解决方案不会弄乱路径。但是感觉这种方法比较慢。
$categoryId = 2269;
$parentId = 2268;
$category = Mage::getModel('catalog/category')->load($categoryId);
$category->move($parentId, null);
答案 1 :(得分:1)
$parent = Mage::getModel('catalog/category')->load('REPLACE_WITH_PARENT_ID');
$category = Mage::getModel('catalog/category');
$category->addData(array('name' => 'YOUR_CATEGORY_NAME', 'path' => $parent->getPath()));
$category->save();