Question at our web agency while creating a Shopware online shop: How can you prevent the category menu from being displayed when opening the OffCanvas menu in the mobile views (viewports "s" and "xs") for categories without subcategories, forcing the user to click again to return to the main navigation? This behavior, which is included by Shopware in the standard installation, is implemented using a plugin ("swSubCategoryNav") that relies on the attribute "[data-subcategory-nav="true"]". This behavior is defined in the file
/themes/Frontend/Responsive/frontend/_public/src/js/jquery.shopware-responsive.js
There you will find the line
.addPlugin('*[data-subcategory-nav="true"]', 'swSubCategoryNav', ['xs', 's'])
, which ensures that the subcategory menu is expanded first for the two specified viewports, even if no subcategories are available. So what needs to be done? We can override / extend the plugin with our own JavaScript file, or we can directly modify the value of the "data-subcategory-nav" attribute in the template. The latter makes more sense if, for example, we want to control the display depending on whether subcategories are available or not. We find the underlying element in the following file (extending the parent file in our own theme):
/themes/Frontend/YOUR_THEME/frontend/index/sidebar.tpl
Here, we look for the following section
data-subcategory-nav="true"
and change it to
data-subcategory-nav="{if $childrenCount > 0}true{else}false{/if}"
Of course, that is only half the story, because we first need to make the $childrenCount variable available. This is done above the element by replacing
{* if sCategoryContent is not available use sArticle.categoryID *}
{if isset($sCategoryContent) && $sCategoryContent.id}
{$subCategoryId = $sCategoryContent.id}
{elseif isset($sArticle) && $sArticle.categoryID}
{$subCategoryId = $sArticle.categoryID}
{elseif isset($sCustomPage) && $sCustomPage.id}
{$subCategoryId = $sCustomPage.id}
{else}
{$subCategoryId = 0}
{/if}
with
{* if sCategoryContent is not available use sArticle.categoryID *}
{$childrenCount = 0}
{* if sCategoryContent is not available use sArticle.categoryID *}
{if isset($sCategoryContent) && $sCategoryContent.id}
{$subCategoryId = $sCategoryContent.id}
{$childrenCount = $sCategoryContent.childrenCount}
{elseif isset($sArticle) && $sArticle.categoryID}
{$subCategoryId = $sArticle.categoryID}
{elseif isset($sCustomPage) && $sCustomPage.id}
{$subCategoryId = $sCustomPage.id}
{else}
{$subCategoryId = 0}
{/if}
This ensures that the subcategory menu is only displayed for the two viewports when subcategories are actually available. Seen at our web agency while creating a Shopware shop with Shopware 5.5.6 Professional Edition.