在new_grid.phtml magento

时间:2017-10-15 13:35:55

标签: magento magento2

我在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>

在front-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个例外:   异常0(Magento \ Framework \ Exception \ LocalizedException):区域代码已设置

     

异常0(Magento \ Framework \ Exception \ LocalizedException):区域代码已设置   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 \状态 - &GT; setAreaCode(&#39;前端&#39;)   1 /chroot/home/host/domain.com/html/vendor/magento/framework/View/TemplateEngine/Php.php(59):include(&#39; / chroot / home / au ...&#39; )   2 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Element/Template.php(255):Magento \ Framework \ View \ TemplateEngine \ Php-&gt; render(Object(Magento \ Catalogue) \ Block \ Product \ Widget \ NewWidget \ Interceptor),&#39; / chroot / home / au ...&#39;,Array)   3 /chroot/home/host/domain.com/html/var/generation/Magento/Catalog/Block/Product/Widget/NewWidget/Interceptor.php(518):Magento \ Framework \ View \ Element \ Template-&gt; fetchView (&#39; / chroot /家/ AU ...&#39;)   4 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Element/Template.php(279):Magento \ Catalog \ Block \ Product \ Widget \ NewWidget \ Interceptor-&gt; fetchView(& #39; / chroot /家/ AU ...&#39;)   5 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Element/AbstractBlock.php(659):Magento \ Framework \ View \ Element \ Template-&gt; _toHtml()   6 /chroot/home/host/domain.com/html/var/generation/Magento/Catalog/Block/Product/Widget/NewWidget/Interceptor.php(830):Magento \ Framework \ View \ Element \ AbstractBlock-&gt; 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-&GT; toHtml()   8 /chroot/home/host/domain.com/html/vendor/magento/framework/View/TemplateEngine/Php.php(59):include(&#39; / chroot / home / au ...&#39; )   9 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Element/Template.php(255):Magento \ Framework \ View \ TemplateEngine \ Php-&gt; render(Object(Magento \ Framework) \ View \ Element \ Template),&#39; / chroot / home / au ...&#39;,Array)   10 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Element/Template.php(279):Magento \ Framework \ View \ Element \ Template-&gt; fetchView(&#39; / chroot /家/ AU ...&#39;)   11 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Element/AbstractBlock.php(659):Magento \ Framework \ View \ Element \ Template-&gt; _toHtml()   12 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Layout.php(542):Magento \ Framework \ View \ Element \ AbstractBlock-&gt; toHtml()   13 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Layout.php(518):Magento \ Framework \ View \ Layout-&gt; _renderBlock(&#39; test_file&#39;)   14 /chroot/home/host/domain.com/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(206):Magento \ Framework \ View \ Layout-&gt; renderNonCachedElement(&#39; test_file& #39)   15 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Layout.php(472):Magento \ Framework \ View \ Layout \ Interceptor-&gt; renderNonCachedElement(&#39; test_file&#39) ;)   16 /chroot/home/host/domain.com/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(193):Magento \ Framework \ View \ Layout-&gt; renderElement(&#39; test_file& #39;,true)   17 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Layout.php(569):Magento \ Framework \ View \ Layout \ Interceptor-&gt; renderElement(&#39; test_file&#39) ;)   18 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Layout.php(520):Magento \ Framework \ View \ Layout-&gt; _renderContainer(&#39; page.wrapper&#39 ;)   19 /chroot/home/host/domain.com/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(206):Magento \ Framework \ View \ Layout-&gt; renderNonCachedElement(&#39; page .wrapper&#39;)   20 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Layout.php(472):Magento \ Framework \ View \ Layout \ Interceptor-&gt; renderNonCachedElement(&#39; page.wrapper& #39)   21 /chroot/home/host/domain.com/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(193):Magento \ Framework \ View \ Layout-&gt; renderElement(&#39; page .wrapper&#39;,true)   22 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Layout.php(569):Magento \ Framework \ View \ Layout \ Interceptor-&gt; renderElement(&#39; page.wrapper& #39)   23 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Layout.php(520):Magento \ Framework \ View \ Layout-&gt; _renderContainer(&#39; root&#39;)   24 /chroot/home/host/domain.com/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(206):Magento \ Framework \ View \ Layout-&gt; renderNonCachedElement(&#39; root& #39)   25 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Layout.php(472):Magento \ Framework \ View \ Layout \ Interceptor-&gt; renderNonCachedElement(&#39; root&#39 ;)   26 /chroot/home/host/domain.com/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(193):Magento \ Framework \ View \ Layout-&gt; renderElement(&#39; root& #39;,true)   27 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Layout.php(938):Magento \ Framework \ View \ Layout \ Interceptor-&gt; renderElement(&#39; root&#39 ;)   28 /chroot/home/host/domain.com/html/vendor/magento/framework/Interception/Interceptor.php(146):Magento \ Framework \ View \ Layout-&gt; getOutput()   29 /chroot/home/host/domain.com/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(494):Magento \ Framework \ View \ Layout \ Interceptor-&gt; ___ callPlugins(&#39) ; getOutput&#39;,Array,Array)   30 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Result/Page.php(243):Magento \ Framework \ View \ Layout \ Interceptor-&gt; getOutput()   31 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Result/Layout.php(164):Magento \ Framework \ View \ Result \ Page-&gt; render(Object(Magento \ Framework) \ APP \响应\ HTTP \拦截器))   32 /chroot/home/host/domain.com/html/vendor/magento/framework/Interception/Interceptor.php(74):Magento \ Framework \ View \ Result \ Layout-&gt; renderResult(Object(Magento \ Framework \ App) \响应\ HTTP \拦截器))   33 /chroot/home/host/domain.com/html/vendor/magento/framework/Interception/Chain/Chain.php(70):Magento \ Framework \ View \ Result \ Page \ Interceptor-&gt; ___ callParent(&#39) ; renderResult&#39;,Array)   34 /chroot/home/host/domain.com/html/vendor/magento/framework/Interception/Chain/Chain.php(63):Magento \ Framework \ Interception \ Chain \ Chain-&gt; invokeNext(&#39; Magento \ Framewo ...&#39;,&#39; renderResult&#39;,对象(Magento \ Framework \ View \ Result \ Page \ Interceptor),数组,&#39;结果 - 清漆-...&#39 ;)   35 /chroot/home/host/domain.com/html/vendor/magento/module-page-cache/Model/Controller/Result/VarnishPlugin.php(74):Magento \ Framework \ Interception \ Chain \ Chain-&gt; Magento \框架\拦截\链{闭合}(对象(Magento的\框架\ APP \响应\ HTTP \拦截器))   36 /chroot/home/host/domain.com/html/vendor/magento/framework/Interception/Chain/Chain.php(67):Magento \ PageCache \ Model \ Controller \ Result \ VarnishPlugin-&gt; 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-&gt; invokeNext(&#39; Magento \ Framewo ...&#39;,&#39; renderResult&#39;,对象(Magento \ Framework \ View \ Result \ Page \ Interceptor),数组,&#39;结果内置-...&#39;)   38 /chroot/home/host/domain.com/html/vendor/magento/module-page-cache/Model/Controller/Result/BuiltinPlugin.php(67):Magento \ Framework \ View \ Result \ Page \ Interceptor-&gt ; Magento的\框架\ {截取闭合}(对象(Magento的\框架\ APP \响应\ HTTP \拦截器))   39 /chroot/home/host/domain.com/html/vendor/magento/framework/Interception/Interceptor.php(142):Magento \ PageCache \ Model \ Controller \ Result \ BuiltinPlugin-&gt; 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-&gt; ___ callPlugins (&#39; renderResult&#39;,Array,Array)   41 /chroot/home/host/domain.com/html/vendor/magento/framework/App/Http.php(139):Magento \ Framework \ View \ Result \ Page \ Interceptor-&gt; renderResult(Object(Magento \ Framework) \ APP \响应\ HTTP \拦截器))   42 /chroot/home/host/domain.com/html/vendor/magento/framework/App/Bootstrap.php(258):Magento \ Framework \ App \ Http-&gt; launch()   43 /chroot/home/host/domain.com/html/index.php(39):Magento \ Framework \ App \ Bootstrap-&gt;运行(对象(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);

6 个答案:

答案 0 :(得分:1)

尝试将此代码自适应

    @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-&gt; getData()并获取当前项目的所有列表数据。

答案 1 :(得分:0)

首先,请验证您是否已将自定义属性添加到适当的属性集中,并将该属性设置为要添加此代码的产品,然后最终使用下面的代码来获取其值。

$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);

由于

答案 2 :(得分:0)

getResource()->getAttribute($attribute_code)->getFrontend()->getValue($_item);
?>

答案 3 :(得分:0)

$_product = $block->getProduct();
print_r($_product->getData);

首先,如果你有合适的阵列,你需要检查产品数据是否可用,然后你只需要从下面的代码中调用产品属性。<​​/ p>

echo  $_product->getResource()->getAttribute('ATTRIBUTE_CODE')->getFrontend()->getValue($_product);

答案 4 :(得分:0)

$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和属性代码,如上所述

答案 5 :(得分:0)

我认为代码中没有产品对象。产品对象仅在当前产品的产品视图页面上。在其他页面上你必须创建。