Magento 2 - 类别列表按位置排序,子类别不起作用
Magento 2 - Category List Sort by Position with Sub Category Not Working
一些背景知识,我正在尝试做一个自定义类别列表,但目前类别似乎没有像我在管理员上看到的那样排序。
这是我到目前为止完成的代码
$current_store = $this->_storeManager->getStore();
$root_category_id = $this->_storeManager->getStore()->getRootCategoryId();
$collection = $this->_categoryCollectionFactory->create()
->addAttributeToSelect('*')
->addAttributeToFilter('is_active', 1)
->setStore($current_store);
return $collection->addAttributeToFilter('entity_id', array('nin' => $root_category_id))
->setOrder('position','ASC');
结果,当我试图回应它的 ID 时,如下所示
3
10
4
11
5
7
12
8
15
9
13
14
16
6
但是,从管理员那里,它没有正确反映顺序,下图是
我意识到的问题是,我有子类别,我试图回应上面代码中的查询,然后将其复制粘贴到 sql GUI 中,我意识到,位置是有点奇怪,但它确实有道理,因为它是一个子类别。
这是我在 sql GUI
上执行查询时的结果
所以,我试图实现的是对以上结果进行排序,以反映我在 Admin 上的设置。
有什么我错过的吗?我不确定去哪里找,因为我已经被困了大约 1-2 天,不确定什么是正确的关键字,我所做的几乎所有关键字都会到达 product sort 或 kind其中,不是 类别排序
提前致谢!
对于那些仍然需要与此问题相关的答案的人,这里是答案
...
$store_categories = $this->_categoryFactory->create();
$store_categories = $store_categories->load($this->_root_category_id)->getChildrenCategories();
foreach ($store_categories as $category) {
//get id
$category_id = $category->getId();
//get category model
$category = $this->getCategoryModel($category_id);
$sub_children = $this->getActiveChildCategories($category);
if (count($sub_children) > 0) {
$sub_categories = $this->getSubCategory($sub_children);
$categories = array_merge($categories, $sub_categories);
} else {
$categories[] = $category;
}
}
...
_categoryFactory
来自 Magento\Catalog\Model\CategoryFactory
。
差不多涵盖了我想要的,但不是我之前预期的那样,因为我认为它不是很有效。
PS - 我仍然是 Magento 2 的新手,所以,如果其他人有其他答案可能与我预期的非常相似,那么,我很乐意将其更改为已接受的答案。 :)
祝你好运!
一些背景知识,我正在尝试做一个自定义类别列表,但目前类别似乎没有像我在管理员上看到的那样排序。
这是我到目前为止完成的代码
$current_store = $this->_storeManager->getStore();
$root_category_id = $this->_storeManager->getStore()->getRootCategoryId();
$collection = $this->_categoryCollectionFactory->create()
->addAttributeToSelect('*')
->addAttributeToFilter('is_active', 1)
->setStore($current_store);
return $collection->addAttributeToFilter('entity_id', array('nin' => $root_category_id))
->setOrder('position','ASC');
结果,当我试图回应它的 ID 时,如下所示
3
10
4
11
5
7
12
8
15
9
13
14
16
6
但是,从管理员那里,它没有正确反映顺序,下图是
我意识到的问题是,我有子类别,我试图回应上面代码中的查询,然后将其复制粘贴到 sql GUI 中,我意识到,位置是有点奇怪,但它确实有道理,因为它是一个子类别。
这是我在 sql GUI
上执行查询时的结果
所以,我试图实现的是对以上结果进行排序,以反映我在 Admin 上的设置。 有什么我错过的吗?我不确定去哪里找,因为我已经被困了大约 1-2 天,不确定什么是正确的关键字,我所做的几乎所有关键字都会到达 product sort 或 kind其中,不是 类别排序
提前致谢!
对于那些仍然需要与此问题相关的答案的人,这里是答案
...
$store_categories = $this->_categoryFactory->create();
$store_categories = $store_categories->load($this->_root_category_id)->getChildrenCategories();
foreach ($store_categories as $category) {
//get id
$category_id = $category->getId();
//get category model
$category = $this->getCategoryModel($category_id);
$sub_children = $this->getActiveChildCategories($category);
if (count($sub_children) > 0) {
$sub_categories = $this->getSubCategory($sub_children);
$categories = array_merge($categories, $sub_categories);
} else {
$categories[] = $category;
}
}
...
_categoryFactory
来自 Magento\Catalog\Model\CategoryFactory
。
差不多涵盖了我想要的,但不是我之前预期的那样,因为我认为它不是很有效。
PS - 我仍然是 Magento 2 的新手,所以,如果其他人有其他答案可能与我预期的非常相似,那么,我很乐意将其更改为已接受的答案。 :)
祝你好运!