Magento 2 - Best way to check if the current category is child
I want to change the design of my store depends of the categories and subcategories.
I need to check if the current category is subcategory and if is subcategory then display something there.
I made this before in Magento 1.9 with the getLevel function:
$isLevel3 = $_category->getLevel() == 3;
if ($isLevel3) {
how I can get the Level in Magento 2 version?
Thank you
Edit:
I get the subcategories level in Magento 2 like this:
$_category = $block->getCurrentCategory();
$categoryFactory = $objectManager->create('MagentoCatalogModelResourceModelCategoryCollectionFactory');
$categories = $categoryFactory->create()
->addAttributeToSelect('*')
->setStore($store)
->addFieldToFilter('is_active', '1')
->addFieldToFilter('include_in_menu', '1')
->addAttributeToFilter('level',3);
$last = count($categories);
$_category = $block->getCurrentCategory();
foreach ($categories as $ficategory):
if($ficategory->getLevel() == 3) {
$mydiv = "subcategory";
}
endforeach;
But how I can check if the current category is Level 3?
magento2 php layout category magento2.2
add a comment |
I want to change the design of my store depends of the categories and subcategories.
I need to check if the current category is subcategory and if is subcategory then display something there.
I made this before in Magento 1.9 with the getLevel function:
$isLevel3 = $_category->getLevel() == 3;
if ($isLevel3) {
how I can get the Level in Magento 2 version?
Thank you
Edit:
I get the subcategories level in Magento 2 like this:
$_category = $block->getCurrentCategory();
$categoryFactory = $objectManager->create('MagentoCatalogModelResourceModelCategoryCollectionFactory');
$categories = $categoryFactory->create()
->addAttributeToSelect('*')
->setStore($store)
->addFieldToFilter('is_active', '1')
->addFieldToFilter('include_in_menu', '1')
->addAttributeToFilter('level',3);
$last = count($categories);
$_category = $block->getCurrentCategory();
foreach ($categories as $ficategory):
if($ficategory->getLevel() == 3) {
$mydiv = "subcategory";
}
endforeach;
But how I can check if the current category is Level 3?
magento2 php layout category magento2.2
add a comment |
I want to change the design of my store depends of the categories and subcategories.
I need to check if the current category is subcategory and if is subcategory then display something there.
I made this before in Magento 1.9 with the getLevel function:
$isLevel3 = $_category->getLevel() == 3;
if ($isLevel3) {
how I can get the Level in Magento 2 version?
Thank you
Edit:
I get the subcategories level in Magento 2 like this:
$_category = $block->getCurrentCategory();
$categoryFactory = $objectManager->create('MagentoCatalogModelResourceModelCategoryCollectionFactory');
$categories = $categoryFactory->create()
->addAttributeToSelect('*')
->setStore($store)
->addFieldToFilter('is_active', '1')
->addFieldToFilter('include_in_menu', '1')
->addAttributeToFilter('level',3);
$last = count($categories);
$_category = $block->getCurrentCategory();
foreach ($categories as $ficategory):
if($ficategory->getLevel() == 3) {
$mydiv = "subcategory";
}
endforeach;
But how I can check if the current category is Level 3?
magento2 php layout category magento2.2
I want to change the design of my store depends of the categories and subcategories.
I need to check if the current category is subcategory and if is subcategory then display something there.
I made this before in Magento 1.9 with the getLevel function:
$isLevel3 = $_category->getLevel() == 3;
if ($isLevel3) {
how I can get the Level in Magento 2 version?
Thank you
Edit:
I get the subcategories level in Magento 2 like this:
$_category = $block->getCurrentCategory();
$categoryFactory = $objectManager->create('MagentoCatalogModelResourceModelCategoryCollectionFactory');
$categories = $categoryFactory->create()
->addAttributeToSelect('*')
->setStore($store)
->addFieldToFilter('is_active', '1')
->addFieldToFilter('include_in_menu', '1')
->addAttributeToFilter('level',3);
$last = count($categories);
$_category = $block->getCurrentCategory();
foreach ($categories as $ficategory):
if($ficategory->getLevel() == 3) {
$mydiv = "subcategory";
}
endforeach;
But how I can check if the current category is Level 3?
magento2 php layout category magento2.2
magento2 php layout category magento2.2
edited 2 hours ago
Robert
asked 3 hours ago
RobertRobert
910734
910734
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
$catLevel = $ficategory->getLevel(); //3
if($catLevel == 3) {
Your need here.
}
1
I fix it, thank you so much
– Robert
2 hours ago
add a comment |
Play a little bit around this:
public function __constructor(
MagentoFrameworkAppRequestHttp $request,
MagentoFrameworkRegistry $registry
) {
$this->request = $request;
$this->registry = $registry;
}
public function isCurentCategorySubCategory() {
if ($request->getFullActionName() !== "catalog_category_view") {
throw new Exception('This is not category view page')
}
if ($registry->registry('current_category')->getLevel() === '3') {
return true;
}
return false;
}
I believe you have to do something like that
Thank you, will help me a lot if you can give me an example without a module, I work in a small phtml file.
– Robert
2 hours ago
phtml - is view layer. You shouldn't put the logic there... For that purpose, you should use view model php class. yireo.com/blog/2017-08-12-viewmodels-in-magento-2
– Stepan Furman
2 hours ago
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "479"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f264111%2fmagento-2-best-way-to-check-if-the-current-category-is-child%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$catLevel = $ficategory->getLevel(); //3
if($catLevel == 3) {
Your need here.
}
1
I fix it, thank you so much
– Robert
2 hours ago
add a comment |
$catLevel = $ficategory->getLevel(); //3
if($catLevel == 3) {
Your need here.
}
1
I fix it, thank you so much
– Robert
2 hours ago
add a comment |
$catLevel = $ficategory->getLevel(); //3
if($catLevel == 3) {
Your need here.
}
$catLevel = $ficategory->getLevel(); //3
if($catLevel == 3) {
Your need here.
}
answered 2 hours ago
PЯINCƏPЯINCƏ
8,11331143
8,11331143
1
I fix it, thank you so much
– Robert
2 hours ago
add a comment |
1
I fix it, thank you so much
– Robert
2 hours ago
1
1
I fix it, thank you so much
– Robert
2 hours ago
I fix it, thank you so much
– Robert
2 hours ago
add a comment |
Play a little bit around this:
public function __constructor(
MagentoFrameworkAppRequestHttp $request,
MagentoFrameworkRegistry $registry
) {
$this->request = $request;
$this->registry = $registry;
}
public function isCurentCategorySubCategory() {
if ($request->getFullActionName() !== "catalog_category_view") {
throw new Exception('This is not category view page')
}
if ($registry->registry('current_category')->getLevel() === '3') {
return true;
}
return false;
}
I believe you have to do something like that
Thank you, will help me a lot if you can give me an example without a module, I work in a small phtml file.
– Robert
2 hours ago
phtml - is view layer. You shouldn't put the logic there... For that purpose, you should use view model php class. yireo.com/blog/2017-08-12-viewmodels-in-magento-2
– Stepan Furman
2 hours ago
add a comment |
Play a little bit around this:
public function __constructor(
MagentoFrameworkAppRequestHttp $request,
MagentoFrameworkRegistry $registry
) {
$this->request = $request;
$this->registry = $registry;
}
public function isCurentCategorySubCategory() {
if ($request->getFullActionName() !== "catalog_category_view") {
throw new Exception('This is not category view page')
}
if ($registry->registry('current_category')->getLevel() === '3') {
return true;
}
return false;
}
I believe you have to do something like that
Thank you, will help me a lot if you can give me an example without a module, I work in a small phtml file.
– Robert
2 hours ago
phtml - is view layer. You shouldn't put the logic there... For that purpose, you should use view model php class. yireo.com/blog/2017-08-12-viewmodels-in-magento-2
– Stepan Furman
2 hours ago
add a comment |
Play a little bit around this:
public function __constructor(
MagentoFrameworkAppRequestHttp $request,
MagentoFrameworkRegistry $registry
) {
$this->request = $request;
$this->registry = $registry;
}
public function isCurentCategorySubCategory() {
if ($request->getFullActionName() !== "catalog_category_view") {
throw new Exception('This is not category view page')
}
if ($registry->registry('current_category')->getLevel() === '3') {
return true;
}
return false;
}
I believe you have to do something like that
Play a little bit around this:
public function __constructor(
MagentoFrameworkAppRequestHttp $request,
MagentoFrameworkRegistry $registry
) {
$this->request = $request;
$this->registry = $registry;
}
public function isCurentCategorySubCategory() {
if ($request->getFullActionName() !== "catalog_category_view") {
throw new Exception('This is not category view page')
}
if ($registry->registry('current_category')->getLevel() === '3') {
return true;
}
return false;
}
I believe you have to do something like that
answered 2 hours ago
Stepan FurmanStepan Furman
6041518
6041518
Thank you, will help me a lot if you can give me an example without a module, I work in a small phtml file.
– Robert
2 hours ago
phtml - is view layer. You shouldn't put the logic there... For that purpose, you should use view model php class. yireo.com/blog/2017-08-12-viewmodels-in-magento-2
– Stepan Furman
2 hours ago
add a comment |
Thank you, will help me a lot if you can give me an example without a module, I work in a small phtml file.
– Robert
2 hours ago
phtml - is view layer. You shouldn't put the logic there... For that purpose, you should use view model php class. yireo.com/blog/2017-08-12-viewmodels-in-magento-2
– Stepan Furman
2 hours ago
Thank you, will help me a lot if you can give me an example without a module, I work in a small phtml file.
– Robert
2 hours ago
Thank you, will help me a lot if you can give me an example without a module, I work in a small phtml file.
– Robert
2 hours ago
phtml - is view layer. You shouldn't put the logic there... For that purpose, you should use view model php class. yireo.com/blog/2017-08-12-viewmodels-in-magento-2
– Stepan Furman
2 hours ago
phtml - is view layer. You shouldn't put the logic there... For that purpose, you should use view model php class. yireo.com/blog/2017-08-12-viewmodels-in-magento-2
– Stepan Furman
2 hours ago
add a comment |
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f264111%2fmagento-2-best-way-to-check-if-the-current-category-is-child%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown