From df33eb0eff7150fd3764153b026c4af803ff04b5 Mon Sep 17 00:00:00 2001 From: Alex Dong Date: Tue, 11 May 2021 11:50:37 +0700 Subject: [PATCH] Update Sort #8 --- Block/Categories.php | 20 +++++++++++--------- Block/Cmspage.php | 22 +++++++++++++++------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/Block/Categories.php b/Block/Categories.php index 27ca505..790441e 100644 --- a/Block/Categories.php +++ b/Block/Categories.php @@ -128,7 +128,6 @@ public function getCurrentCategory() public function getCategories() { - $category = $this->getCurrentCategory(); if(!$category) return; @@ -136,14 +135,17 @@ public function getCategories() if ($this->isExcluded($categoryId)) return; - $sortAttribute = $this->getSortAttribute(); - $model = $this->categoryFactory->create(); - $categories = $model->getCollection() - ->addAttributeToSelect(['name', 'url_key', 'url_path', 'image','description']) - // ->addAttributeToFilter('include_in_menu', 1) - ->addAttributeToFilter('parent_id', $categoryId) - ->addAttributeToSort($sortAttribute) - ->addIsActiveFilter(); + $sortAttribute = $this->getSortAttribute(); + $categories = $this->categoryFactory->create()->getCollection() + ->addAttributeToSelect(['name', 'url_key', 'url_path', 'image','description']) + ->addAttributeToFilter('parent_id', $categoryId) + ->addIsActiveFilter(); + + if($sortAttribute == "position") { + $categories->addAttributeToSort('level'); + } + + $categories->addAttributeToSort($sortAttribute); return $categories; } diff --git a/Block/Cmspage.php b/Block/Cmspage.php index 6f64c8d..b9f7f02 100644 --- a/Block/Cmspage.php +++ b/Block/Cmspage.php @@ -13,6 +13,9 @@ class Cmspage extends Categories { + + const DEFAULT_CACHE_TAG = 'MAGEPOW_CATEGORIES_CMS'; + const XML_PATH = 'home_page'; public function getLayout() @@ -54,13 +57,18 @@ public function getCategories() { $categoryIds = $this->getCategorySelect(); if(!$categoryIds) return; - $sortAttribute = $this->getSortAttribute(); - $model = $this->categoryFactory->create(); - $categories = $model->getCollection() - ->addAttributeToSelect(['name', 'url_key', 'url_path', 'image', 'description']) - ->addAttributeToSort($sortAttribute) - ->addIdFilter($categoryIds) - ->addIsActiveFilter(); + + $sortAttribute = $this->getSortAttribute(); + $categories = $this->categoryFactory->create()->getCollection() + ->addAttributeToSelect(['name', 'url_key', 'url_path', 'image', 'description']) + ->addIdFilter($categoryIds) + ->addIsActiveFilter(); + + if($sortAttribute == "position") { + $categories->addAttributeToSort('level'); + } + + $categories->addAttributeToSort($sortAttribute); return $categories; }