Skip to content

Commit

Permalink
Merge pull request #8 from vvasiloud/master
Browse files Browse the repository at this point in the history
Subcategory depth level select & Current category tree option added
  • Loading branch information
SilvanLaroo authored Jun 1, 2017
2 parents 3c856c7 + 8e8580a commit ddabdbc
Show file tree
Hide file tree
Showing 15 changed files with 219 additions and 34 deletions.
44 changes: 36 additions & 8 deletions Block/Sidebar.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ class Sidebar extends Template

/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection */
protected $_productCollectionFactory;

/** @var \Magento\Catalog\Helper\Output */
private $helper;

/**
* @param Template\Context $context
* @param \Magento\Catalog\Helper\Category $categoryHelper
Expand All @@ -57,8 +57,10 @@ 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,
$data = [ ]
)
{
Expand All @@ -68,15 +70,16 @@ public function __construct(
$this->_categoryFactory = $categoryFactory;
$this->_productCollectionFactory = $productCollectionFactory;
$this->helper = $helper;
$this->_dataHelper = $dataHelper;

parent::__construct($context, $data);
}

/*
* Get owner name
* @return string
*/

/**
* Get all categories
*
Expand All @@ -98,8 +101,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;

Expand All @@ -117,6 +122,26 @@ public function getSelectedRootCategory()
'sebwite_sidebar/general/category'
);

if ( $category == 'current_category_children'){
$currentCategory = $this->_coreRegistry->registry('current_category');
if($currentCategory){
return $currentCategory->getId();
}
return 1;
}

if ( $category == 'current_category_parent_children'){
$currentCategory = $this->_coreRegistry->registry('current_category');
if($currentCategory){
$topLevelParent = $currentCategory->getPath();
$topLevelParentArray = explode("/", $topLevelParent);
if(isset($topLevelParent)){
return $topLevelParentArray[2];
}
}
return 1;
}

if ( $category === null )
{
return 1;
Expand All @@ -133,7 +158,7 @@ public function getSelectedRootCategory()
* @return string
*/
public function getChildCategoryView($category, $html = '', $level = 1)
{
{
// Check if category has children
if ( $category->hasChildren() )
{
Expand Down Expand Up @@ -180,11 +205,13 @@ public function getChildCategoryView($category, $html = '', $level = 1)

/**
* Retrieve subcategories
*
* DEPRECATED
*
* @param $category
*
* @return array
*/

public function getSubcategories($category)
{
if ( $this->categoryFlatConfig->isFlatEnabled() && $category->getUseFlatResource() )
Expand All @@ -194,6 +221,7 @@ public function getSubcategories($category)

return $category->getChildrenCategories();
}


/**
* Get current category
Expand Down Expand Up @@ -249,4 +277,4 @@ public function getCategoryUrl($category)
{
return $this->_categoryHelper->getCategoryUrl($category);
}
}
}
88 changes: 88 additions & 0 deletions Helper/Data.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php
/**
* Class:Categories
* Sebwite\Sidebar\Model\Config\Source
*
* @author Vasilis Vasiloudis
* @package Sebwite\Sidebar
* @copyright Copyright (c) 2016, vvasiloud. All rights reserved
*/
namespace Sebwite\Sidebar\Helper;

use Magento\Framework\Module\ModuleListInterface;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
const XML_PATH_ENABLED = 'general/enabled';
const XML_PATH_CATEGORY = 'general/category';
const XML_PATH_CATEGORY_DEPTH_LEVEL = 'general/categorydepth';


/**
* @var ModuleListInterface
*/
protected $_moduleList;

/**
* @param \Magento\Framework\App\Helper\Context $context
* @param ModuleListInterface $moduleList
*/
public function __construct(\Magento\Framework\App\Helper\Context $context, ModuleListInterface $moduleList
) {
$this->_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
);
}

}
3 changes: 3 additions & 0 deletions Model/Config/Source/Categories.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public function toOptionArray()
foreach($storeCategories as $category) {
$resultArray[$category->getId()] = $category->getName();
}

$resultArray['current_category_children'] = __('Current Category Children');
$resultArray['current_category_parent_children'] = __('Current Category Parent Children');

return $resultArray;
}
Expand Down
43 changes: 43 additions & 0 deletions Model/Config/Source/Depth.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php namespace Sebwite\Sidebar\Model\Config\Source;

/**
* Class:Categories
* Sebwite\Sidebar\Model\Config\Source
*
* @author Vasilis Vasiloudis
* @package Sebwite\Sidebar
* @copyright Copyright (c) 2016, vvasiloud. All rights reserved
*/
class Depth implements \Magento\Framework\Option\ArrayInterface {

/**
* Return array of options as value-label pairs
*
* @return array Format: array(array('value' => '<value>', 'label' => '<label>'), ...)
*/
public function toOptionArray()
{
return [
[
'value' => 1,
'label' => __('1'),
],
[
'value' => 2,
'label' => __('2'),
],
[
'value' => 3,
'label' => __('3'),
],
[
'value' => 4,
'label' => __('4'),
],
[
'value' => 5,
'label' => __('5'),
],
];
}
}
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -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`
Expand All @@ -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
`<block class="Sebwite\Sidebar\Block\Sidebar" name="category-sidebar" template="Sebwite_Sidebar::sidebar.phtml" />`
* 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)
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 11 additions & 1 deletion etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,21 @@
<group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="0"
showInStore="0">
<label>Sidebar settings</label>
<field id="category" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="0"
<field id="enabled" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="0"
showInStore="0">
<label>Is Enabled</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="category" translate="label" type="select" sortOrder="20" showInDefault="1" showInWebsite="0"
showInStore="0">
<label>Select Sidebar Category</label>
<source_model>Sebwite\Sidebar\Model\Config\Source\Categories</source_model>
</field>
<field id="categorydepth" translate="label" type="select" sortOrder="30" showInDefault="1" showInWebsite="0"
showInStore="0">
<label>Select Category Depth</label>
<source_model>Sebwite\Sidebar\Model\Config\Source\Depth</source_model>
</field>
</group>
</section>
</system>
Expand Down
11 changes: 11 additions & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../Store/etc/config.xsd">
<default>
<sidebar>
<general>
<enabled>1</enabled>
<categorydepth>1</categorydepth>
</general>
</sidebar>
</default>
</config>
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Sebwite_Sidebar" setup_version="2.0.2"/>
<module name="Sebwite_Sidebar" setup_version="2.0.3"/>
</config>
2 changes: 1 addition & 1 deletion view/frontend/layout/default.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
<script type="text/javascript" src="Sebwite_Sidebar::js/module.js"/>
<!--<script type="text/javascript" src="Sebwite_Sidebar::js/module.js"/>-->
<css src="Sebwite_Sidebar::css/module.css" />
<css src="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" src_type="url" />
</head>
Expand Down
2 changes: 1 addition & 1 deletion view/frontend/page_layout/2columns-left.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<update handle="2columns-left"/>

<referenceContainer name="columns">
<container name="div.sidebar.main" htmlTag="div" htmlClass="c-sidebar c-sidebar--categories">
<container name="sidebar.main" htmlTag="div" htmlClass="c-sidebar c-sidebar--categories">
<block class="Sebwite\Sidebar\Block\Sidebar" name="category-sidebar" template="Sebwite_Sidebar::sidebar.phtml"/>
</container>
</referenceContainer>
Expand Down
7 changes: 7 additions & 0 deletions view/frontend/requirejs-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var config = {
map: {
'*': {
sidebarmodule: 'Sebwite_Sidebar/js/module'
}
}
};
6 changes: 5 additions & 1 deletion view/frontend/templates/sidebar.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ $brands = $block->getBrands();
<?php echo $block->getChildCategoryView($category); ?>
</li>
<?php endforeach; ?>
</ul>
</ul>
<script type="text/javascript">
/* Simply invoke iffe of sidebarmodule */
require(['jquery','sidebarmodule'], function ($) {});
</script>
Loading

0 comments on commit ddabdbc

Please sign in to comment.