From 3aef31be2e6dcec3db51b01a4bd5fe87f3fa2edf Mon Sep 17 00:00:00 2001 From: vvasiloud Date: Wed, 7 Sep 2016 22:00:32 +0300 Subject: [PATCH 1/9] Readme update --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 42cc8ba..ca83870 100755 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Magento 2.0 Category Sidebar extension ![Alt text](header.jpg?raw=true "Magento2 Category sidebar") -This extension will add the ability to show one of your root categories in a sidebar. The root category can be selected from the Magento2 admin config page. +This extension will add the ability to show category tree from a parent or current category in a sidebar. The category can be selected from the Magento2 admin config page. ## Installation with composer * Include the repository: `composer require sebwite/magento2-category-sidebar` @@ -17,9 +17,9 @@ This extension will add the ability to show one of your root categories in a sid * Clear cache ## Configuration -* Select the root category you want to use from the config page from the admin panel -* You should implement the block `Sebwite\Sidebar\Block\Sidebar` in your theme to make this extension work. Example -`` +* Select the root category or current category option you want to use from the config page from the admin panel +* Select children depth level +* Categories will appear in col.left sidebar of the theme --- [![Alt text](https://www.sebwite.nl/wp-content/themes/sebwite/assets/images/logo-sebwite.png "Sebwite.nl")](https://sebwite.nl) \ No newline at end of file From f4ccb9647a6ee2f56ce322cbd8574f59be293f5b Mon Sep 17 00:00:00 2001 From: vvasiloud Date: Wed, 7 Sep 2016 22:00:47 +0300 Subject: [PATCH 2/9] Version update --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 8a5f8c3..a42e551 100755 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "sebwite/magento2-category-sidebar", "description": "Magento 2.0 sidebar", "type": "magento2-module", - "version": "1.0.1", + "version": "2.0.3", "license": [ "OSL-3.0", "AFL-3.0" From 64a3bdbb49b5b60c118236881f60a1ad95d52ea4 Mon Sep 17 00:00:00 2001 From: vvasiloud Date: Wed, 7 Sep 2016 22:01:04 +0300 Subject: [PATCH 3/9] Version Update --- etc/module.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/etc/module.xml b/etc/module.xml index 3381246..bd6dc49 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -1,4 +1,4 @@ - + \ No newline at end of file From a3bc4b8bb765427925e2da3f69484d8399351121 Mon Sep 17 00:00:00 2001 From: vvasiloud Date: Wed, 7 Sep 2016 22:02:27 +0300 Subject: [PATCH 4/9] Feature: Depth level select & Current category tree option --- Block/Sidebar.php | 28 ++++++++-- Helper/Data.php | 88 ++++++++++++++++++++++++++++++ Model/Config/Source/Categories.php | 2 + Model/Config/Source/Depth.php | 43 +++++++++++++++ etc/adminhtml/system.xml | 12 +++- etc/config.xml | 11 ++++ 6 files changed, 177 insertions(+), 7 deletions(-) create mode 100644 Helper/Data.php create mode 100644 Model/Config/Source/Depth.php create mode 100644 etc/config.xml diff --git a/Block/Sidebar.php b/Block/Sidebar.php index a30668b..c4c2b54 100644 --- a/Block/Sidebar.php +++ b/Block/Sidebar.php @@ -38,9 +38,12 @@ class Sidebar extends Template /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection */ protected $_productCollectionFactory; + /** @var \Magento\Framework\Registry */ + protected $registry; + /** @var \Magento\Catalog\Helper\Output */ private $helper; - + /** * @param Template\Context $context * @param \Magento\Catalog\Helper\Category $categoryHelper @@ -59,8 +62,11 @@ public function __construct( \Magento\Framework\Registry $registry, \Magento\Catalog\Model\Indexer\Category\Flat\State $categoryFlatState, \Magento\Catalog\Model\CategoryFactory $categoryFactory, + \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollectionFactory, \Magento\Catalog\Helper\Output $helper, + \Sebwite\Sidebar\Helper\Data $dataHelper, + \Magento\Framework\Registry $registry, $data = [ ] ) { @@ -70,15 +76,17 @@ public function __construct( $this->_categoryFactory = $categoryFactory; $this->_productCollectionFactory = $productCollectionFactory; $this->helper = $helper; + $this->_dataHelper = $dataHelper; + $this->registry = $registry; parent::__construct($context, $data); } - + /* * Get owner name * @return string */ - + /** * Get all categories * @@ -100,8 +108,10 @@ public function getCategories($sorted = false, $asCollection = false, $toLoad = * Check if parent node of the store still exists */ $category = $this->_categoryFactory->create(); + + $categoryDepthLevel = $this->_dataHelper->getCategoryDepthLevel(); - $storeCategories = $category->getCategories($this->getSelectedRootCategory(), $recursionLevel = 1, $sorted, $asCollection, $toLoad); + $storeCategories = $category->getCategories($this->getSelectedRootCategory(), $recursionLevel = $categoryDepthLevel, $sorted, $asCollection, $toLoad); $this->_storeCategories[ $cacheKey ] = $storeCategories; @@ -119,6 +129,10 @@ public function getSelectedRootCategory() 'sebwite_sidebar/general/category' ); + if ( $category == 'current_category_children'){ + return $this->registry->registry('current_category')->getId(); + } + if ( $category === null ) { return 1; @@ -135,7 +149,7 @@ public function getSelectedRootCategory() * @return string */ public function getChildCategoryView($category, $html = '', $level = 1) - { + { // Check if category has children if ( $category->hasChildren() ) { @@ -182,11 +196,12 @@ public function getChildCategoryView($category, $html = '', $level = 1) /** * Retrieve subcategories - * + * * @param $category * * @return array */ + public function getSubcategories($category) { if ( $this->categoryFlatConfig->isFlatEnabled() && $category->getUseFlatResource() ) @@ -196,6 +211,7 @@ public function getSubcategories($category) return $category->getChildren(); } + /** * Get current category diff --git a/Helper/Data.php b/Helper/Data.php new file mode 100644 index 0000000..299ba5d --- /dev/null +++ b/Helper/Data.php @@ -0,0 +1,88 @@ +_moduleList = $moduleList; + parent::__construct($context); + } + + /** + * @param $xmlPath + * @param string $section + * + * @return string + */ + public function getConfigPath( + $xmlPath, + $section = 'sebwitete_sidebar' + ) { + return $section . '/' . $xmlPath; + } + + /** + * Check if enabled + * + * @return string|null + */ + public function isEnabled() + { + return $this->scopeConfig->getValue( + $this->getConfigPath(self::XML_PATH_ENABLED), + \Magento\Store\Model\ScopeInterface::SCOPE_STORE + ); + } + + /** + * Get sidebar category + * + * @return string|null + */ + public function getSidebarCategory() + { + return $this->scopeConfig->getValue( + $this->getConfigPath(self::XML_PATH_CATEGORY), + \Magento\Store\Model\ScopeInterface::SCOPE_STORE + ); + } + + /** + * Get category depth level + * + * @return string|null + */ + public function getCategoryDepthLevel() + { + return $this->scopeConfig->getValue( + $this->getConfigPath(self::XML_PATH_CATEGORY_DEPTH_LEVEL), + \Magento\Store\Model\ScopeInterface::SCOPE_STORE + ); + } + +} \ No newline at end of file diff --git a/Model/Config/Source/Categories.php b/Model/Config/Source/Categories.php index 7a26f0b..b0b5c2b 100644 --- a/Model/Config/Source/Categories.php +++ b/Model/Config/Source/Categories.php @@ -55,6 +55,8 @@ public function toOptionArray() foreach($storeCategories as $category) { $resultArray[$category->getId()] = $category->getName(); } + + $resultArray['current_category_children'] = __('Current Category Children'); return $resultArray; } diff --git a/Model/Config/Source/Depth.php b/Model/Config/Source/Depth.php new file mode 100644 index 0000000..14ce0a1 --- /dev/null +++ b/Model/Config/Source/Depth.php @@ -0,0 +1,43 @@ + '', 'label' => '