在 new_grid.phtml magento 中获取产品属性值
Get product attribute value in new_grid.phtml magento
我在 new_grid.phtml 中获取属性值时遇到问题。如果我这样做:
<?php $_product = $block->getProduct()
echo $_product->getData('attr_name') ?>
页面生成到这个地方,之后就是空白页。
请告诉我其他选项,或者我的代码有错误。
这是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>
代码来自custom_home.xml
<container name="header.container" as="header_container" label="Page Header Container" htmlTag="header" htmlClass="page-header" before="main.content"/>
<block class="Magento\Framework\View\Element\Template" 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>
前面-slider.phtml 我通过代码得到new_grid.phtml:
<?php echo $this->getLayout()->createBlock("Magento\Catalog\Block\Product\Widget\NewWidget")->setDisplayType("all_products")->setProductsCount("6")->setTemplate("product/widget/new/content/new_grid.phtml")->toHtml(); ?>
TBI 答案错误:
1 exception(s):
Exception 0 (Magento\Framework\Exception\LocalizedException): Area code is already set
Exception 0 (Magento\Framework\Exception\LocalizedException): 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): Magento\Framework\App\State->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): Magento\Framework\View\TemplateEngine\Php->render(Object(Magento\Catalog\Block\Product\Widget\NewWidget\Interceptor), '/chroot/home/au...', Array)
3 /chroot/home/host/domain.com/html/var/generation/Magento/Catalog/Block/Product/Widget/NewWidget/Interceptor.php(518): Magento\Framework\View\Element\Template->fetchView('/chroot/home/au...')
4 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Element/Template.php(279): Magento\Catalog\Block\Product\Widget\NewWidget\Interceptor->fetchView('/chroot/home/au...')
5 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Element/AbstractBlock.php(659): Magento\Framework\View\Element\Template->_toHtml()
6 /chroot/home/host/domain.com/html/var/generation/Magento/Catalog/Block/Product/Widget/NewWidget/Interceptor.php(830): Magento\Framework\View\Element\AbstractBlock->toHtml()
7 /chroot/home/host/domain.com/html/app/design/frontend/central/central-theme/Magento_Theme/templates/html/front-slider.phtml(81): Magento\Catalog\Block\Product\Widget\NewWidget\Interceptor->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): Magento\Framework\View\TemplateEngine\Php->render(Object(Magento\Framework\View\Element\Template), '/chroot/home/au...', Array)
10 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Element/Template.php(279): Magento\Framework\View\Element\Template->fetchView('/chroot/home/au...')
11 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Element/AbstractBlock.php(659): Magento\Framework\View\Element\Template->_toHtml()
12 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Layout.php(542): Magento\Framework\View\Element\AbstractBlock->toHtml()
13 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Layout.php(518): Magento\Framework\View\Layout->_renderBlock('test_file')
14 /chroot/home/host/domain.com/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(206): Magento\Framework\View\Layout->renderNonCachedElement('test_file')
15 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Layout.php(472): Magento\Framework\View\Layout\Interceptor->renderNonCachedElement('test_file')
16 /chroot/home/host/domain.com/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(193): Magento\Framework\View\Layout->renderElement('test_file', true)
17 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Layout.php(569): Magento\Framework\View\Layout\Interceptor->renderElement('test_file')
18 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Layout.php(520): Magento\Framework\View\Layout->_renderContainer('page.wrapper')
19 /chroot/home/host/domain.com/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(206): Magento\Framework\View\Layout->renderNonCachedElement('page.wrapper')
20 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Layout.php(472): Magento\Framework\View\Layout\Interceptor->renderNonCachedElement('page.wrapper')
21 /chroot/home/host/domain.com/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(193): Magento\Framework\View\Layout->renderElement('page.wrapper', true)
22 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Layout.php(569): Magento\Framework\View\Layout\Interceptor->renderElement('page.wrapper')
23 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Layout.php(520): Magento\Framework\View\Layout->_renderContainer('root')
24 /chroot/home/host/domain.com/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(206): Magento\Framework\View\Layout->renderNonCachedElement('root')
25 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Layout.php(472): Magento\Framework\View\Layout\Interceptor->renderNonCachedElement('root')
26 /chroot/home/host/domain.com/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(193): Magento\Framework\View\Layout->renderElement('root', true)
27 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Layout.php(938): Magento\Framework\View\Layout\Interceptor->renderElement('root')
28 /chroot/home/host/domain.com/html/vendor/magento/framework/Interception/Interceptor.php(146): Magento\Framework\View\Layout->getOutput()
29 /chroot/home/host/domain.com/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(494): Magento\Framework\View\Layout\Interceptor->___callPlugins('getOutput', Array, Array)
30 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Result/Page.php(243): Magento\Framework\View\Layout\Interceptor->getOutput()
31 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Result/Layout.php(164): Magento\Framework\View\Result\Page->render(Object(Magento\Framework\App\Response\Http\Interceptor))
32 /chroot/home/host/domain.com/html/vendor/magento/framework/Interception/Interceptor.php(74): Magento\Framework\View\Result\Layout->renderResult(Object(Magento\Framework\App\Response\Http\Interceptor))
33 /chroot/home/host/domain.com/html/vendor/magento/framework/Interception/Chain/Chain.php(70): Magento\Framework\View\Result\Page\Interceptor->___callParent('renderResult', Array)
34 /chroot/home/host/domain.com/html/vendor/magento/framework/Interception/Chain/Chain.php(63): Magento\Framework\Interception\Chain\Chain->invokeNext('Magento\Framewo...', 'renderResult', Object(Magento\Framework\View\Result\Page\Interceptor), Array, 'result-varnish-...')
35 /chroot/home/host/domain.com/html/vendor/magento/module-page-cache/Model/Controller/Result/VarnishPlugin.php(74): Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain{closure}(Object(Magento\Framework\App\Response\Http\Interceptor))
36 /chroot/home/host/domain.com/html/vendor/magento/framework/Interception/Chain/Chain.php(67): Magento\PageCache\Model\Controller\Result\VarnishPlugin->aroundRenderResult(Object(Magento\Framework\View\Result\Page\Interceptor), Object(Closure), Object(Magento\Framework\App\Response\Http\Interceptor))
37 /chroot/home/host/domain.com/html/vendor/magento/framework/Interception/Interceptor.php(138): Magento\Framework\Interception\Chain\Chain->invokeNext('Magento\Framewo...', 'renderResult', Object(Magento\Framework\View\Result\Page\Interceptor), Array, 'result-builtin-...')
38 /chroot/home/host/domain.com/html/vendor/magento/module-page-cache/Model/Controller/Result/BuiltinPlugin.php(67): Magento\Framework\View\Result\Page\Interceptor->Magento\Framework\Interception{closure}(Object(Magento\Framework\App\Response\Http\Interceptor))
39 /chroot/home/host/domain.com/html/vendor/magento/framework/Interception/Interceptor.php(142): Magento\PageCache\Model\Controller\Result\BuiltinPlugin->aroundRenderResult(Object(Magento\Framework\View\Result\Page\Interceptor), Object(Closure), Object(Magento\Framework\App\Response\Http\Interceptor))
40 /chroot/home/host/domain.com/html/var/generation/Magento/Framework/View/Result/Page/Interceptor.php(130): Magento\Framework\View\Result\Page\Interceptor->___callPlugins('renderResult', Array, Array)
41 /chroot/home/host/domain.com/html/vendor/magento/framework/App/Http.php(139): Magento\Framework\View\Result\Page\Interceptor->renderResult(Object(Magento\Framework\App\Response\Http\Interceptor))
42 /chroot/home/host/domain.com/html/vendor/magento/framework/App/Bootstrap.php(258): Magento\Framework\App\Http->launch()
43 /chroot/home/host/domain.com/html/index.php(39): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http))
44 {main}
关于此代码:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$appState = $objectManager->get('\Magento\Framework\App\State');
$appState->setAreaCode('frontend');
$registry = $objectManager->get('\Magento\Framework\Registry');
$_product= $registry->registry('current_product');
$attribute_code = 'attr_name';
echo $_product->getResource()->getAttribute($attribute_code)-
>getFrontend()->getValue($_product);
首先,请验证您是否已将自定义属性添加到正确的属性集中,并将该属性集分配给您为其添加此代码的产品,然后最后使用以下代码获取其值。
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$appState = $objectManager->get('\Magento\Framework\App\State');
//$appState->setAreaCode('frontend');
$registry = $objectManager->get('\Magento\Framework\Registry');
$_product= $registry->registry('current_product');
$attribute_code = 'attr_name';
echo $_product->getResource()->getAttribute($attribute_code)->getFrontend()->getValue($_product);
谢谢
getResource()->getAttribute($attribute_code)->getFrontend()->getValue($_item);
?>
$_product = $block->getProduct();
print_r($_product->getData);
首先你需要检查产品数据是否可用,如果你有正确的数组,那么你只需要从下面的代码中调用产品属性。
echo $_product->getResource()->getAttribute('ATTRIBUTE_CODE')->getFrontend()->getValue($_product);
$productModel = Mage::getModel('catalog/product');
$_product = $productModel->load(42); // Add your product ID
$attribute_code = 'attr_name'; // Add your attribute name
echo $_product->getResource()->getAttribute($attribute_code)->getFrontend()->getValue($_product);
根据您上面的评论,请试试这个,只需更改我上面提到的产品 ID 和属性代码
我觉得产品对象在您的代码中不可用。仅在当前产品的产品视图页面上的产品对象。在其他页面上您必须创建。
尝试根据您的代码修改此代码
@var $block \Magento\Catalog\Block\Product\Widget\NewWidget
if ($exist = ($block->getProductCollection()
&& $block->getProductCollection()->getSize()))
{
$items = $block->getProductCollection()->getItems();
}
<?php foreach ($items as $_item): ?>
<img src="<?php echo $_item->getData("nameofattribute") ?>" />
<?php endforeach ?>
如果您不知道属性的名称,您可以使用 $_item->getData() 并获取当前项目的所有列表数据。
我在 new_grid.phtml 中获取属性值时遇到问题。如果我这样做:
<?php $_product = $block->getProduct()
echo $_product->getData('attr_name') ?>
页面生成到这个地方,之后就是空白页。
请告诉我其他选项,或者我的代码有错误。
这是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>
代码来自custom_home.xml
<container name="header.container" as="header_container" label="Page Header Container" htmlTag="header" htmlClass="page-header" before="main.content"/>
<block class="Magento\Framework\View\Element\Template" 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>
前面-slider.phtml 我通过代码得到new_grid.phtml:
<?php echo $this->getLayout()->createBlock("Magento\Catalog\Block\Product\Widget\NewWidget")->setDisplayType("all_products")->setProductsCount("6")->setTemplate("product/widget/new/content/new_grid.phtml")->toHtml(); ?>
TBI 答案错误:
1 exception(s): Exception 0 (Magento\Framework\Exception\LocalizedException): Area code is already set
Exception 0 (Magento\Framework\Exception\LocalizedException): 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): Magento\Framework\App\State->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): Magento\Framework\View\TemplateEngine\Php->render(Object(Magento\Catalog\Block\Product\Widget\NewWidget\Interceptor), '/chroot/home/au...', Array) 3 /chroot/home/host/domain.com/html/var/generation/Magento/Catalog/Block/Product/Widget/NewWidget/Interceptor.php(518): Magento\Framework\View\Element\Template->fetchView('/chroot/home/au...') 4 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Element/Template.php(279): Magento\Catalog\Block\Product\Widget\NewWidget\Interceptor->fetchView('/chroot/home/au...') 5 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Element/AbstractBlock.php(659): Magento\Framework\View\Element\Template->_toHtml() 6 /chroot/home/host/domain.com/html/var/generation/Magento/Catalog/Block/Product/Widget/NewWidget/Interceptor.php(830): Magento\Framework\View\Element\AbstractBlock->toHtml() 7 /chroot/home/host/domain.com/html/app/design/frontend/central/central-theme/Magento_Theme/templates/html/front-slider.phtml(81): Magento\Catalog\Block\Product\Widget\NewWidget\Interceptor->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): Magento\Framework\View\TemplateEngine\Php->render(Object(Magento\Framework\View\Element\Template), '/chroot/home/au...', Array) 10 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Element/Template.php(279): Magento\Framework\View\Element\Template->fetchView('/chroot/home/au...') 11 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Element/AbstractBlock.php(659): Magento\Framework\View\Element\Template->_toHtml() 12 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Layout.php(542): Magento\Framework\View\Element\AbstractBlock->toHtml() 13 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Layout.php(518): Magento\Framework\View\Layout->_renderBlock('test_file') 14 /chroot/home/host/domain.com/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(206): Magento\Framework\View\Layout->renderNonCachedElement('test_file') 15 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Layout.php(472): Magento\Framework\View\Layout\Interceptor->renderNonCachedElement('test_file') 16 /chroot/home/host/domain.com/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(193): Magento\Framework\View\Layout->renderElement('test_file', true) 17 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Layout.php(569): Magento\Framework\View\Layout\Interceptor->renderElement('test_file') 18 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Layout.php(520): Magento\Framework\View\Layout->_renderContainer('page.wrapper') 19 /chroot/home/host/domain.com/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(206): Magento\Framework\View\Layout->renderNonCachedElement('page.wrapper') 20 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Layout.php(472): Magento\Framework\View\Layout\Interceptor->renderNonCachedElement('page.wrapper') 21 /chroot/home/host/domain.com/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(193): Magento\Framework\View\Layout->renderElement('page.wrapper', true) 22 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Layout.php(569): Magento\Framework\View\Layout\Interceptor->renderElement('page.wrapper') 23 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Layout.php(520): Magento\Framework\View\Layout->_renderContainer('root') 24 /chroot/home/host/domain.com/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(206): Magento\Framework\View\Layout->renderNonCachedElement('root') 25 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Layout.php(472): Magento\Framework\View\Layout\Interceptor->renderNonCachedElement('root') 26 /chroot/home/host/domain.com/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(193): Magento\Framework\View\Layout->renderElement('root', true) 27 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Layout.php(938): Magento\Framework\View\Layout\Interceptor->renderElement('root') 28 /chroot/home/host/domain.com/html/vendor/magento/framework/Interception/Interceptor.php(146): Magento\Framework\View\Layout->getOutput() 29 /chroot/home/host/domain.com/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(494): Magento\Framework\View\Layout\Interceptor->___callPlugins('getOutput', Array, Array) 30 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Result/Page.php(243): Magento\Framework\View\Layout\Interceptor->getOutput() 31 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Result/Layout.php(164): Magento\Framework\View\Result\Page->render(Object(Magento\Framework\App\Response\Http\Interceptor)) 32 /chroot/home/host/domain.com/html/vendor/magento/framework/Interception/Interceptor.php(74): Magento\Framework\View\Result\Layout->renderResult(Object(Magento\Framework\App\Response\Http\Interceptor)) 33 /chroot/home/host/domain.com/html/vendor/magento/framework/Interception/Chain/Chain.php(70): Magento\Framework\View\Result\Page\Interceptor->___callParent('renderResult', Array) 34 /chroot/home/host/domain.com/html/vendor/magento/framework/Interception/Chain/Chain.php(63): Magento\Framework\Interception\Chain\Chain->invokeNext('Magento\Framewo...', 'renderResult', Object(Magento\Framework\View\Result\Page\Interceptor), Array, 'result-varnish-...') 35 /chroot/home/host/domain.com/html/vendor/magento/module-page-cache/Model/Controller/Result/VarnishPlugin.php(74): Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain{closure}(Object(Magento\Framework\App\Response\Http\Interceptor)) 36 /chroot/home/host/domain.com/html/vendor/magento/framework/Interception/Chain/Chain.php(67): Magento\PageCache\Model\Controller\Result\VarnishPlugin->aroundRenderResult(Object(Magento\Framework\View\Result\Page\Interceptor), Object(Closure), Object(Magento\Framework\App\Response\Http\Interceptor)) 37 /chroot/home/host/domain.com/html/vendor/magento/framework/Interception/Interceptor.php(138): Magento\Framework\Interception\Chain\Chain->invokeNext('Magento\Framewo...', 'renderResult', Object(Magento\Framework\View\Result\Page\Interceptor), Array, 'result-builtin-...') 38 /chroot/home/host/domain.com/html/vendor/magento/module-page-cache/Model/Controller/Result/BuiltinPlugin.php(67): Magento\Framework\View\Result\Page\Interceptor->Magento\Framework\Interception{closure}(Object(Magento\Framework\App\Response\Http\Interceptor)) 39 /chroot/home/host/domain.com/html/vendor/magento/framework/Interception/Interceptor.php(142): Magento\PageCache\Model\Controller\Result\BuiltinPlugin->aroundRenderResult(Object(Magento\Framework\View\Result\Page\Interceptor), Object(Closure), Object(Magento\Framework\App\Response\Http\Interceptor)) 40 /chroot/home/host/domain.com/html/var/generation/Magento/Framework/View/Result/Page/Interceptor.php(130): Magento\Framework\View\Result\Page\Interceptor->___callPlugins('renderResult', Array, Array) 41 /chroot/home/host/domain.com/html/vendor/magento/framework/App/Http.php(139): Magento\Framework\View\Result\Page\Interceptor->renderResult(Object(Magento\Framework\App\Response\Http\Interceptor)) 42 /chroot/home/host/domain.com/html/vendor/magento/framework/App/Bootstrap.php(258): Magento\Framework\App\Http->launch() 43 /chroot/home/host/domain.com/html/index.php(39): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http)) 44 {main}
关于此代码:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$appState = $objectManager->get('\Magento\Framework\App\State');
$appState->setAreaCode('frontend');
$registry = $objectManager->get('\Magento\Framework\Registry');
$_product= $registry->registry('current_product');
$attribute_code = 'attr_name';
echo $_product->getResource()->getAttribute($attribute_code)-
>getFrontend()->getValue($_product);
首先,请验证您是否已将自定义属性添加到正确的属性集中,并将该属性集分配给您为其添加此代码的产品,然后最后使用以下代码获取其值。
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$appState = $objectManager->get('\Magento\Framework\App\State');
//$appState->setAreaCode('frontend');
$registry = $objectManager->get('\Magento\Framework\Registry');
$_product= $registry->registry('current_product');
$attribute_code = 'attr_name';
echo $_product->getResource()->getAttribute($attribute_code)->getFrontend()->getValue($_product);
谢谢
getResource()->getAttribute($attribute_code)->getFrontend()->getValue($_item); ?>
$_product = $block->getProduct();
print_r($_product->getData);
首先你需要检查产品数据是否可用,如果你有正确的数组,那么你只需要从下面的代码中调用产品属性。
echo $_product->getResource()->getAttribute('ATTRIBUTE_CODE')->getFrontend()->getValue($_product);
$productModel = Mage::getModel('catalog/product');
$_product = $productModel->load(42); // Add your product ID
$attribute_code = 'attr_name'; // Add your attribute name
echo $_product->getResource()->getAttribute($attribute_code)->getFrontend()->getValue($_product);
根据您上面的评论,请试试这个,只需更改我上面提到的产品 ID 和属性代码
我觉得产品对象在您的代码中不可用。仅在当前产品的产品视图页面上的产品对象。在其他页面上您必须创建。
尝试根据您的代码修改此代码
@var $block \Magento\Catalog\Block\Product\Widget\NewWidget
if ($exist = ($block->getProductCollection()
&& $block->getProductCollection()->getSize()))
{
$items = $block->getProductCollection()->getItems();
}
<?php foreach ($items as $_item): ?>
<img src="<?php echo $_item->getData("nameofattribute") ?>" />
<?php endforeach ?>
如果您不知道属性的名称,您可以使用 $_item->getData() 并获取当前项目的所有列表数据。