稍微有点背景,我正在尝试做一个定制的类别列表,但目前看来,这个类别并不像我在Admin上看到的那样排序。
这是我迄今所做的代码
$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中,然后我意识到,这个位置有点奇怪,但是,这是有意义的,因为它是一个子类别。下面是在sql上执行查询的结果

因此,我试图实现的是对以上结果进行排序,以反映我在行政管理方面所做的工作。我错过了什么吗?我不知道该去哪里,因为我被困在了1-2天,不知道什么是合适的关键字,几乎我做的所有关键字都会到达产品排序或类似的,而不是类别排序。
提前谢谢!
发布于 2020-12-22 06:36:21
对于那些还需要一些关于这个问题的答案的人来说,下面是答案
...
$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,所以,如果其他人有其他答案,这可能是我所期待的,那么,我很高兴地改变它作为接受的答案。:)
祝你好运!
https://stackoverflow.com/questions/65301952
复制相似问题