Magento 2 How to show static block based on current category id
Anyone can help me to show show static block based on current category id
In this following file
Magento_Swatches/templates/product/view/renderer.phtml
I tried this code to get current id
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$category = $objectManager->get('MagentoFrameworkRegistry')->registry('current_category');//get current category
echo $category->getId();
echo $category->getName();
Its returns with this error Fatal error: Uncaught Error: Call to a member function getId()
Magento 2.1.6
magento2
bumped to the homepage by Community♦ 44 secs ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
Anyone can help me to show show static block based on current category id
In this following file
Magento_Swatches/templates/product/view/renderer.phtml
I tried this code to get current id
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$category = $objectManager->get('MagentoFrameworkRegistry')->registry('current_category');//get current category
echo $category->getId();
echo $category->getName();
Its returns with this error Fatal error: Uncaught Error: Call to a member function getId()
Magento 2.1.6
magento2
bumped to the homepage by Community♦ 44 secs ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
Why are you using that template? Are you using that phtml (Magento_Swatches/templates/product/view/renderer.phtml
) because it is the location that you are looking for the static block to show up? Or do you need some of the swatch logic to in that call to the static block?
– circlesix
Jun 16 '17 at 21:57
Yes i need to show size chart .
– MagEGY
Jun 16 '17 at 22:19
add a comment |
Anyone can help me to show show static block based on current category id
In this following file
Magento_Swatches/templates/product/view/renderer.phtml
I tried this code to get current id
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$category = $objectManager->get('MagentoFrameworkRegistry')->registry('current_category');//get current category
echo $category->getId();
echo $category->getName();
Its returns with this error Fatal error: Uncaught Error: Call to a member function getId()
Magento 2.1.6
magento2
Anyone can help me to show show static block based on current category id
In this following file
Magento_Swatches/templates/product/view/renderer.phtml
I tried this code to get current id
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$category = $objectManager->get('MagentoFrameworkRegistry')->registry('current_category');//get current category
echo $category->getId();
echo $category->getName();
Its returns with this error Fatal error: Uncaught Error: Call to a member function getId()
Magento 2.1.6
magento2
magento2
edited Dec 6 '18 at 5:49
Murtuza Zabuawala
12.5k73362
12.5k73362
asked Jun 16 '17 at 21:49
MagEGYMagEGY
101113
101113
bumped to the homepage by Community♦ 44 secs ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 44 secs ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
Why are you using that template? Are you using that phtml (Magento_Swatches/templates/product/view/renderer.phtml
) because it is the location that you are looking for the static block to show up? Or do you need some of the swatch logic to in that call to the static block?
– circlesix
Jun 16 '17 at 21:57
Yes i need to show size chart .
– MagEGY
Jun 16 '17 at 22:19
add a comment |
Why are you using that template? Are you using that phtml (Magento_Swatches/templates/product/view/renderer.phtml
) because it is the location that you are looking for the static block to show up? Or do you need some of the swatch logic to in that call to the static block?
– circlesix
Jun 16 '17 at 21:57
Yes i need to show size chart .
– MagEGY
Jun 16 '17 at 22:19
Why are you using that template? Are you using that phtml (
Magento_Swatches/templates/product/view/renderer.phtml
) because it is the location that you are looking for the static block to show up? Or do you need some of the swatch logic to in that call to the static block?– circlesix
Jun 16 '17 at 21:57
Why are you using that template? Are you using that phtml (
Magento_Swatches/templates/product/view/renderer.phtml
) because it is the location that you are looking for the static block to show up? Or do you need some of the swatch logic to in that call to the static block?– circlesix
Jun 16 '17 at 21:57
Yes i need to show size chart .
– MagEGY
Jun 16 '17 at 22:19
Yes i need to show size chart .
– MagEGY
Jun 16 '17 at 22:19
add a comment |
1 Answer
1
active
oldest
votes
While it might be tempting to put this is a template override, it's really better to make a module to get this kind of functionality. Using the object manager is a bad idea here, as it's hard to know how long Magento will keep it around, and i think you are running into issues with the scope of the category id on the product page that is making it so it's not working the way you would expect.
You could do something like this:
app/code/Vendor/SizeChart/Block/SizeChart.php
namespace VendorSizeChartBlock;
use MagentoFrameworkViewElementTemplate;
use MagentoBackendBlockTemplateContext;
use MagentoFrameworkRegistry;
class SizeChart extends Template
{
protected $_registry;
public function __construct(
Context $context,
Registry $registry,
array $data =
)
{
$this->_registry = $registry;
parent::__construct($context, $data);
}
public function getCurrentCategory()
{
return $this->_registry->registry('current_category');
}
}
?>
This is create a block that you can get the Magento registry from, extend from the Template class and return a method that had the current category object, all while avoiding the object manager.
app/code/Vendor/SizeChart/view/frontend/layout/catalog_product_view.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="product.info.options.wrapper">
<block class="VendorSizeChartBlockSizeChart" name="custom-size-chart" after="-" template="Vendor_SizeChart::size-chart.phtml"/>
</referenceBlock>
</body>
</page>
From that block class, you can get a custom template and place it where you need it on the product page using just xml, no core template overrides needed. You might have to play with the placement by finding the right block to reference, here i'm using product.info.options.wrapper
as it seems close to where you wanted it.
app/code/Vendor/SizeChart/view/frontend/templates/size-chart.phtml
<?php $currentCategory = $block->getCurrentCategory()->getId(); ?>
<?php if ($currentCategory = '26') : ?>
<div class="size-chart">
<?php echo $block->getLayout()->createBlock('MagentoCmsBlockBlock')->setBlockId('size-chart')->toHtml();?>
</div>
<?php endif; ?>
Of course you will have to set the category value based on what you are trying to achieve. I'm using '26' here just for my testing, you might find different needs, just check in the admin what category numbers you need. From there i just call in a cms block named size-chart
, where you can put any content you need.
You just have to register the module and run setup:upgrade
in the cli.
app/code/Vendor/SizeChart/etc/module.xml
<?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="Vendor_SizeChart" setup_version="1.0.0" />
</config>
app/code/CustomerParadigm/SizeChart/registration.php
<?php
MagentoFrameworkComponentComponentRegistrar::register(
MagentoFrameworkComponentComponentRegistrar::MODULE,
'Vendor_SizeChart',
__DIR__
);
Just a note, I took the block idea from here: How to Get Product Current Category Name in Magento 2 in Product View Page and adapted it to what you are trying to do with it.
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%2f179498%2fmagento-2-how-to-show-static-block-based-on-current-category-id%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
While it might be tempting to put this is a template override, it's really better to make a module to get this kind of functionality. Using the object manager is a bad idea here, as it's hard to know how long Magento will keep it around, and i think you are running into issues with the scope of the category id on the product page that is making it so it's not working the way you would expect.
You could do something like this:
app/code/Vendor/SizeChart/Block/SizeChart.php
namespace VendorSizeChartBlock;
use MagentoFrameworkViewElementTemplate;
use MagentoBackendBlockTemplateContext;
use MagentoFrameworkRegistry;
class SizeChart extends Template
{
protected $_registry;
public function __construct(
Context $context,
Registry $registry,
array $data =
)
{
$this->_registry = $registry;
parent::__construct($context, $data);
}
public function getCurrentCategory()
{
return $this->_registry->registry('current_category');
}
}
?>
This is create a block that you can get the Magento registry from, extend from the Template class and return a method that had the current category object, all while avoiding the object manager.
app/code/Vendor/SizeChart/view/frontend/layout/catalog_product_view.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="product.info.options.wrapper">
<block class="VendorSizeChartBlockSizeChart" name="custom-size-chart" after="-" template="Vendor_SizeChart::size-chart.phtml"/>
</referenceBlock>
</body>
</page>
From that block class, you can get a custom template and place it where you need it on the product page using just xml, no core template overrides needed. You might have to play with the placement by finding the right block to reference, here i'm using product.info.options.wrapper
as it seems close to where you wanted it.
app/code/Vendor/SizeChart/view/frontend/templates/size-chart.phtml
<?php $currentCategory = $block->getCurrentCategory()->getId(); ?>
<?php if ($currentCategory = '26') : ?>
<div class="size-chart">
<?php echo $block->getLayout()->createBlock('MagentoCmsBlockBlock')->setBlockId('size-chart')->toHtml();?>
</div>
<?php endif; ?>
Of course you will have to set the category value based on what you are trying to achieve. I'm using '26' here just for my testing, you might find different needs, just check in the admin what category numbers you need. From there i just call in a cms block named size-chart
, where you can put any content you need.
You just have to register the module and run setup:upgrade
in the cli.
app/code/Vendor/SizeChart/etc/module.xml
<?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="Vendor_SizeChart" setup_version="1.0.0" />
</config>
app/code/CustomerParadigm/SizeChart/registration.php
<?php
MagentoFrameworkComponentComponentRegistrar::register(
MagentoFrameworkComponentComponentRegistrar::MODULE,
'Vendor_SizeChart',
__DIR__
);
Just a note, I took the block idea from here: How to Get Product Current Category Name in Magento 2 in Product View Page and adapted it to what you are trying to do with it.
add a comment |
While it might be tempting to put this is a template override, it's really better to make a module to get this kind of functionality. Using the object manager is a bad idea here, as it's hard to know how long Magento will keep it around, and i think you are running into issues with the scope of the category id on the product page that is making it so it's not working the way you would expect.
You could do something like this:
app/code/Vendor/SizeChart/Block/SizeChart.php
namespace VendorSizeChartBlock;
use MagentoFrameworkViewElementTemplate;
use MagentoBackendBlockTemplateContext;
use MagentoFrameworkRegistry;
class SizeChart extends Template
{
protected $_registry;
public function __construct(
Context $context,
Registry $registry,
array $data =
)
{
$this->_registry = $registry;
parent::__construct($context, $data);
}
public function getCurrentCategory()
{
return $this->_registry->registry('current_category');
}
}
?>
This is create a block that you can get the Magento registry from, extend from the Template class and return a method that had the current category object, all while avoiding the object manager.
app/code/Vendor/SizeChart/view/frontend/layout/catalog_product_view.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="product.info.options.wrapper">
<block class="VendorSizeChartBlockSizeChart" name="custom-size-chart" after="-" template="Vendor_SizeChart::size-chart.phtml"/>
</referenceBlock>
</body>
</page>
From that block class, you can get a custom template and place it where you need it on the product page using just xml, no core template overrides needed. You might have to play with the placement by finding the right block to reference, here i'm using product.info.options.wrapper
as it seems close to where you wanted it.
app/code/Vendor/SizeChart/view/frontend/templates/size-chart.phtml
<?php $currentCategory = $block->getCurrentCategory()->getId(); ?>
<?php if ($currentCategory = '26') : ?>
<div class="size-chart">
<?php echo $block->getLayout()->createBlock('MagentoCmsBlockBlock')->setBlockId('size-chart')->toHtml();?>
</div>
<?php endif; ?>
Of course you will have to set the category value based on what you are trying to achieve. I'm using '26' here just for my testing, you might find different needs, just check in the admin what category numbers you need. From there i just call in a cms block named size-chart
, where you can put any content you need.
You just have to register the module and run setup:upgrade
in the cli.
app/code/Vendor/SizeChart/etc/module.xml
<?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="Vendor_SizeChart" setup_version="1.0.0" />
</config>
app/code/CustomerParadigm/SizeChart/registration.php
<?php
MagentoFrameworkComponentComponentRegistrar::register(
MagentoFrameworkComponentComponentRegistrar::MODULE,
'Vendor_SizeChart',
__DIR__
);
Just a note, I took the block idea from here: How to Get Product Current Category Name in Magento 2 in Product View Page and adapted it to what you are trying to do with it.
add a comment |
While it might be tempting to put this is a template override, it's really better to make a module to get this kind of functionality. Using the object manager is a bad idea here, as it's hard to know how long Magento will keep it around, and i think you are running into issues with the scope of the category id on the product page that is making it so it's not working the way you would expect.
You could do something like this:
app/code/Vendor/SizeChart/Block/SizeChart.php
namespace VendorSizeChartBlock;
use MagentoFrameworkViewElementTemplate;
use MagentoBackendBlockTemplateContext;
use MagentoFrameworkRegistry;
class SizeChart extends Template
{
protected $_registry;
public function __construct(
Context $context,
Registry $registry,
array $data =
)
{
$this->_registry = $registry;
parent::__construct($context, $data);
}
public function getCurrentCategory()
{
return $this->_registry->registry('current_category');
}
}
?>
This is create a block that you can get the Magento registry from, extend from the Template class and return a method that had the current category object, all while avoiding the object manager.
app/code/Vendor/SizeChart/view/frontend/layout/catalog_product_view.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="product.info.options.wrapper">
<block class="VendorSizeChartBlockSizeChart" name="custom-size-chart" after="-" template="Vendor_SizeChart::size-chart.phtml"/>
</referenceBlock>
</body>
</page>
From that block class, you can get a custom template and place it where you need it on the product page using just xml, no core template overrides needed. You might have to play with the placement by finding the right block to reference, here i'm using product.info.options.wrapper
as it seems close to where you wanted it.
app/code/Vendor/SizeChart/view/frontend/templates/size-chart.phtml
<?php $currentCategory = $block->getCurrentCategory()->getId(); ?>
<?php if ($currentCategory = '26') : ?>
<div class="size-chart">
<?php echo $block->getLayout()->createBlock('MagentoCmsBlockBlock')->setBlockId('size-chart')->toHtml();?>
</div>
<?php endif; ?>
Of course you will have to set the category value based on what you are trying to achieve. I'm using '26' here just for my testing, you might find different needs, just check in the admin what category numbers you need. From there i just call in a cms block named size-chart
, where you can put any content you need.
You just have to register the module and run setup:upgrade
in the cli.
app/code/Vendor/SizeChart/etc/module.xml
<?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="Vendor_SizeChart" setup_version="1.0.0" />
</config>
app/code/CustomerParadigm/SizeChart/registration.php
<?php
MagentoFrameworkComponentComponentRegistrar::register(
MagentoFrameworkComponentComponentRegistrar::MODULE,
'Vendor_SizeChart',
__DIR__
);
Just a note, I took the block idea from here: How to Get Product Current Category Name in Magento 2 in Product View Page and adapted it to what you are trying to do with it.
While it might be tempting to put this is a template override, it's really better to make a module to get this kind of functionality. Using the object manager is a bad idea here, as it's hard to know how long Magento will keep it around, and i think you are running into issues with the scope of the category id on the product page that is making it so it's not working the way you would expect.
You could do something like this:
app/code/Vendor/SizeChart/Block/SizeChart.php
namespace VendorSizeChartBlock;
use MagentoFrameworkViewElementTemplate;
use MagentoBackendBlockTemplateContext;
use MagentoFrameworkRegistry;
class SizeChart extends Template
{
protected $_registry;
public function __construct(
Context $context,
Registry $registry,
array $data =
)
{
$this->_registry = $registry;
parent::__construct($context, $data);
}
public function getCurrentCategory()
{
return $this->_registry->registry('current_category');
}
}
?>
This is create a block that you can get the Magento registry from, extend from the Template class and return a method that had the current category object, all while avoiding the object manager.
app/code/Vendor/SizeChart/view/frontend/layout/catalog_product_view.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="product.info.options.wrapper">
<block class="VendorSizeChartBlockSizeChart" name="custom-size-chart" after="-" template="Vendor_SizeChart::size-chart.phtml"/>
</referenceBlock>
</body>
</page>
From that block class, you can get a custom template and place it where you need it on the product page using just xml, no core template overrides needed. You might have to play with the placement by finding the right block to reference, here i'm using product.info.options.wrapper
as it seems close to where you wanted it.
app/code/Vendor/SizeChart/view/frontend/templates/size-chart.phtml
<?php $currentCategory = $block->getCurrentCategory()->getId(); ?>
<?php if ($currentCategory = '26') : ?>
<div class="size-chart">
<?php echo $block->getLayout()->createBlock('MagentoCmsBlockBlock')->setBlockId('size-chart')->toHtml();?>
</div>
<?php endif; ?>
Of course you will have to set the category value based on what you are trying to achieve. I'm using '26' here just for my testing, you might find different needs, just check in the admin what category numbers you need. From there i just call in a cms block named size-chart
, where you can put any content you need.
You just have to register the module and run setup:upgrade
in the cli.
app/code/Vendor/SizeChart/etc/module.xml
<?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="Vendor_SizeChart" setup_version="1.0.0" />
</config>
app/code/CustomerParadigm/SizeChart/registration.php
<?php
MagentoFrameworkComponentComponentRegistrar::register(
MagentoFrameworkComponentComponentRegistrar::MODULE,
'Vendor_SizeChart',
__DIR__
);
Just a note, I took the block idea from here: How to Get Product Current Category Name in Magento 2 in Product View Page and adapted it to what you are trying to do with it.
answered Jun 20 '17 at 16:41
circlesixcirclesix
2,87221547
2,87221547
add a comment |
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%2f179498%2fmagento-2-how-to-show-static-block-based-on-current-category-id%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
Why are you using that template? Are you using that phtml (
Magento_Swatches/templates/product/view/renderer.phtml
) because it is the location that you are looking for the static block to show up? Or do you need some of the swatch logic to in that call to the static block?– circlesix
Jun 16 '17 at 21:57
Yes i need to show size chart .
– MagEGY
Jun 16 '17 at 22:19