I have problem with getting attribute value in new_grid.phtml. If i make it like this:
<?php $_product = $block->getProduct()
echo $_product->getData('attr_name') ?>
Page generating to this place, and after just blank page.
Please show me other option, or maybe i have mistakes in my code.
Here is the place in new_grid.phtml:
<div class="product-img-block">
<a href="<?php /* @escapeNotVerified */ echo $block->getProductUrl($_item) ?>" class="product-item-photo">
<?php echo $block->getImage($_item, $image)->toHtml(); ?>
/* my code: */
<?php $_product = $block->getProduct() ?>
<img src="<?php echo $_product->getData('attr_name') ?>" />
</a>
it is code from custom_home.xml
<container name="header.container" as="header_container" label="Page Header Container" htmlTag="header" htmlClass="page-header" before="main.content"/>
<block class="MagentoFrameworkViewElementTemplate" name="test_file" template="Magento_Theme::html/front-slider.phtml" before="main.content"/>
<container name="main.content" htmlTag="main" htmlId="maincontent" htmlClass="page-main">
<container name="columns.top" label="Before Main Columns"/>
<container name="columns" htmlTag="div" htmlClass="columns">
<container name="main" label="Main Content Container" htmlTag="div" htmlClass="column main"/>
</container>
</container>
In front-slider.phtml i get the new_grid.phtml by code:
<?php echo $this->getLayout()->createBlock("MagentoCatalogBlockProductWidgetNewWidget")->setDisplayType("all_products")->setProductsCount("6")->setTemplate("product/widget/new/content/new_grid.phtml")->toHtml(); ?>
Errors for TBI answer:
1 exception(s):
Exception 0 (MagentoFrameworkExceptionLocalizedException): Area code is already setException 0 (MagentoFrameworkExceptionLocalizedException): Area code is already set
0 /chroot/home/host/domain.com/html/app/design/frontend/central/central-theme/Magento_Catalog/templates/product/widget/new/content/new_grid.phtml(52): MagentoFrameworkAppState->setAreaCode(‘frontend’)
1 /chroot/home/host/domain.com/html/vendor/magento/framework/View/TemplateEngine/Php.php(59): include(‘/chroot/home/au…’)
2 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Element/Template.php(255): MagentoFrameworkViewTemplateEnginePhp->render(Object(MagentoCatalogBlockProductWidgetNewWidgetInterceptor), ‘/chroot/home/au…’, Array)
3 /chroot/home/host/domain.com/html/var/generation/Magento/Catalog/Block/Product/Widget/NewWidget/Interceptor.php(518): MagentoFrameworkViewElementTemplate->fetchView(‘/chroot/home/au…’)
4 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Element/Template.php(279): MagentoCatalogBlockProductWidgetNewWidgetInterceptor->fetchView(‘/chroot/home/au…’)
5 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Element/AbstractBlock.php(659): MagentoFrameworkViewElementTemplate->_toHtml()
6 /chroot/home/host/domain.com/html/var/generation/Magento/Catalog/Block/Product/Widget/NewWidget/Interceptor.php(830): MagentoFrameworkViewElementAbstractBlock->toHtml()
7 /chroot/home/host/domain.com/html/app/design/frontend/central/central-theme/Magento_Theme/templates/html/front-slider.phtml(81): MagentoCatalogBlockProductWidgetNewWidgetInterceptor->toHtml()
8 /chroot/home/host/domain.com/html/vendor/magento/framework/View/TemplateEngine/Php.php(59): include(‘/chroot/home/au…’)
9 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Element/Template.php(255): MagentoFrameworkViewTemplateEnginePhp->render(Object(MagentoFrameworkViewElementTemplate), ‘/chroot/home/au…’, Array)
10 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Element/Template.php(279): MagentoFrameworkViewElementTemplate->fetchView(‘/chroot/home/au…’)
11 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Element/AbstractBlock.php(659): MagentoFrameworkViewElementTemplate->_toHtml()
12 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Layout.php(542): MagentoFrameworkViewElementAbstractBlock->toHtml()
13 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Layout.php(518): MagentoFrameworkViewLayout->_renderBlock(‘test_file’)
14 /chroot/home/host/domain.com/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(206): MagentoFrameworkViewLayout->renderNonCachedElement(‘test_file’)
15 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Layout.php(472): MagentoFrameworkViewLayoutInterceptor->renderNonCachedElement(‘test_file’)
16 /chroot/home/host/domain.com/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(193): MagentoFrameworkViewLayout->renderElement(‘test_file’, true)
17 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Layout.php(569): MagentoFrameworkViewLayoutInterceptor->renderElement(‘test_file’)
18 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Layout.php(520): MagentoFrameworkViewLayout->_renderContainer(‘page.wrapper’)
19 /chroot/home/host/domain.com/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(206): MagentoFrameworkViewLayout->renderNonCachedElement(‘page.wrapper’)
20 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Layout.php(472): MagentoFrameworkViewLayoutInterceptor->renderNonCachedElement(‘page.wrapper’)
21 /chroot/home/host/domain.com/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(193): MagentoFrameworkViewLayout->renderElement(‘page.wrapper’, true)
22 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Layout.php(569): MagentoFrameworkViewLayoutInterceptor->renderElement(‘page.wrapper’)
23 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Layout.php(520): MagentoFrameworkViewLayout->_renderContainer(‘root’)
24 /chroot/home/host/domain.com/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(206): MagentoFrameworkViewLayout->renderNonCachedElement(‘root’)
25 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Layout.php(472): MagentoFrameworkViewLayoutInterceptor->renderNonCachedElement(‘root’)
26 /chroot/home/host/domain.com/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(193): MagentoFrameworkViewLayout->renderElement(‘root’, true)
27 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Layout.php(938): MagentoFrameworkViewLayoutInterceptor->renderElement(‘root’)
28 /chroot/home/host/domain.com/html/vendor/magento/framework/Interception/Interceptor.php(146): MagentoFrameworkViewLayout->getOutput()
29 /chroot/home/host/domain.com/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(494): MagentoFrameworkViewLayoutInterceptor->___callPlugins(‘getOutput’, Array, Array)
30 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Result/Page.php(243): MagentoFrameworkViewLayoutInterceptor->getOutput()
31 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Result/Layout.php(164): MagentoFrameworkViewResultPage->render(Object(MagentoFrameworkAppResponseHttpInterceptor))
32 /chroot/home/host/domain.com/html/vendor/magento/framework/Interception/Interceptor.php(74): MagentoFrameworkViewResultLayout->renderResult(Object(MagentoFrameworkAppResponseHttpInterceptor))
33 /chroot/home/host/domain.com/html/vendor/magento/framework/Interception/Chain/Chain.php(70): MagentoFrameworkViewResultPageInterceptor->___callParent(‘renderResult’, Array)
34 /chroot/home/host/domain.com/html/vendor/magento/framework/Interception/Chain/Chain.php(63): MagentoFrameworkInterceptionChainChain->invokeNext(‘MagentoFramewo…’, ‘renderResult’, Object(MagentoFrameworkViewResultPageInterceptor), Array, ‘result-varnish-…’)
35 /chroot/home/host/domain.com/html/vendor/magento/module-page-cache/Model/Controller/Result/VarnishPlugin.php(74): MagentoFrameworkInterceptionChainChain->MagentoFrameworkInterceptionChain{closure}(Object(MagentoFrameworkAppResponseHttpInterceptor))
36 /chroot/home/host/domain.com/html/vendor/magento/framework/Interception/Chain/Chain.php(67): MagentoPageCacheModelControllerResultVarnishPlugin->aroundRenderResult(Object(MagentoFrameworkViewResultPageInterceptor), Object(Closure), Object(MagentoFrameworkAppResponseHttpInterceptor))
37 /chroot/home/host/domain.com/html/vendor/magento/framework/Interception/Interceptor.php(138): MagentoFrameworkInterceptionChainChain->invokeNext(‘MagentoFramewo…’, ‘renderResult’, Object(MagentoFrameworkViewResultPageInterceptor), Array, ‘result-builtin-…’)
38 /chroot/home/host/domain.com/html/vendor/magento/module-page-cache/Model/Controller/Result/BuiltinPlugin.php(67): MagentoFrameworkViewResultPageInterceptor->MagentoFrameworkInterception{closure}(Object(MagentoFrameworkAppResponseHttpInterceptor))
39 /chroot/home/host/domain.com/html/vendor/magento/framework/Interception/Interceptor.php(142): MagentoPageCacheModelControllerResultBuiltinPlugin->aroundRenderResult(Object(MagentoFrameworkViewResultPageInterceptor), Object(Closure), Object(MagentoFrameworkAppResponseHttpInterceptor))
40 /chroot/home/host/domain.com/html/var/generation/Magento/Framework/View/Result/Page/Interceptor.php(130): MagentoFrameworkViewResultPageInterceptor->___callPlugins(‘renderResult’, Array, Array)
41 /chroot/home/host/domain.com/html/vendor/magento/framework/App/Http.php(139): MagentoFrameworkViewResultPageInterceptor->renderResult(Object(MagentoFrameworkAppResponseHttpInterceptor))
42 /chroot/home/host/domain.com/html/vendor/magento/framework/App/Bootstrap.php(258): MagentoFrameworkAppHttp->launch()
43 /chroot/home/host/domain.com/html/index.php(39): MagentoFrameworkAppBootstrap->run(Object(MagentoFrameworkAppHttp))
44 {main}
On this code:
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$appState = $objectManager->get('MagentoFrameworkAppState');
$appState->setAreaCode('frontend');
$registry = $objectManager->get('MagentoFrameworkRegistry');
$_product= $registry->registry('current_product');
$attribute_code = 'attr_name';
echo $_product->getResource()->getAttribute($attribute_code)-
>getFrontend()->getValue($_product);
7
Answers
First, please verify if you have added your custom attribute in proper attribute set and assigned that attribute set to product for which you are adding this code and then finally use below code to get its value.
Thanks
First you need to check for product data available or not if you got proper array then you just need to call product attribute from below code.
As per your comment above please try this just change product id and attribute code as i have mention above
I feel product object not available in your code. Product object only on product view page in current product. On other page you have to create.
Try adaptive this code to yours
If you dont know name of attribute you may use $_item->getData() and get all of list data for current item.
I struggled with a custom attribute in my own grid.phtml file. After a long time, I realised there is an attribute setting "use in product-isting" which was set to "No". Make sure this is set to yes.