doufu2396 2016-01-27 09:43
浏览 27
已采纳

Magento 2 - 空自定义小部件渲染

I try to create a custom widget on Magento 2 EE to list Products with custom attribute 'end_life' Yes/No type. It's OK in backoffice, I created the widget with type "End Life Products" and I inserted it on my Homepage.

But the render on Homepage is empty. Just a <p></p>

Please help me to render my products list :)

app/code/My/CustomModule/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="My_CustomModule" setup_version="1.0.0">
    </module>
</config>

app/code/My/CustomModule/etc/widget.xml

<?xml version="1.0" encoding="UTF-8"?>
<widgets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../Magento/Widget/etc/widget.xsd">
    <widget id="my_custommodule_end_life_products" class="My\CustomModule\Block\Widget\EndLifeProducts">
        <label translate="true">End Life Products</label>
        <description></description>
    </widget>
</widgets>

app/code/My/CustomModule/Block/Widget/EndLifeProducts.php

namespace My\CustomModule\Block\Widget;

class EndLifeProducts extends \Magento\Framework\View\Element\Template implements \Magento\Widget\Block\BlockInterface
{

    public function getEndLifeCollection() {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection */
        $productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
        $productCollection->addAttributeToFilter('end_life', true)
            ->load();
        return $productCollection;
    }

    public function _toHtml()
    {
        $this->setTemplate('widget/end_life_products.phtml');
    }
}

app/code/My/CustomModule/view/frontend/widget/end_life_products.phtml

<h1>End Life Products</h1>
<?php
if ($exist = ($block->getEndLifeCollection() && $block->getEndLifeCollection()->getSize())) {
    $items = $block->getEndLifeCollection()->getItems();
}
  • 写回答

2条回答 默认 最新

  • douqianni4080 2016-01-28 08:10
    关注

    I found a solution :

    Define your template as protected $_template variable, and don't define _toHtml() function

    namespace My\CustomModule\Block\Widget;
    
    class EndLifeProducts extends \Magento\Framework\View\Element\Template implements \Magento\Widget\Block\BlockInterface
    {
    
        protected $_template = 'widget/end_life_products.phtml';
    
        public function getEndLifeCollection() {
            $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
            /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection */
            $productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
            $productCollection->addAttributeToFilter('end_life', true)
                ->load();
            return $productCollection;
        }
    }
    

    And put your template .phtml in your custom module template folder : app/code/My/CustomModule/view/frontend/template/widget/end_life_products.phtml

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效