Magento-以编程方式移动类别 [英] Magento - Move a category programmatically

查看:53
本文介绍了Magento-以编程方式移动类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将一个具有所有子类别的类别移到另一个类别?

How do I move a category to another category with all child categories?

我尝试了以下解决方案:

I have tried the following solution:

$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);

但是我的结果有点扭曲.如果我移至根目录类别,则此方法有效,但如果移至子目录类别,则结果将有错误,并且类别将消失.

However my result is somewhat twisted. If I move to the root-category the move works, but if I move to a child-category I get faulty results and disappearing categories.

这些是数据库中字段路径的值:

These are the values of the field path in the database:

Old: 1/2/3/2175/2269
New: 1/2/3/2175/2226/2268/2269
Correct: 1/2/3/2226/2268/2269

推荐答案

该解决方案非常简单,它不会弄乱路径.但是,感觉这种方法比较慢.

The solution was quite simple, this one doesn't mess up the paths. However it feels like this method is slower.

$categoryId = 2269;
$parentId = 2268;

$category = Mage::getModel('catalog/category')->load($categoryId);
$category->move($parentId, null);

这篇关于Magento-以编程方式移动类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆