php - Magento 2 - Empty custom widget render -
i try create custom widget on magento 2 ee list products custom attribute 'end_life' yes/no type. it's ok in backoffice, created widget type "end life products" , inserted on homepage.
but render on homepage empty. <p></p>
please me render 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(); }
i found solution :
define template protected $_template
variable, , 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 template .phtml in custom module template folder : app/code/my/custommodule/view/frontend/template/widget/end_life_products.phtml
Comments
Post a Comment