4

registration.php


\Magento\Framework\Component\ComponentRegistrar::register(
     \Magento\Framework\Component\ComponentRegistrar::MODULE,
     'Relabs_LabAPI',
     __DIR__
    );

module.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Relabs_LabAPI" setup_version="1.0.2" schema_version="1.0.0">
        <sequence>
            <module name="Magento_CatalogWidget"/>
        </sequence>
    </module>
</config>    

widget.xml

<widgets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Widget:etc/widget.xsd">
    <widget id="sponsored_bestsellerwidget" class="Relabs\LabAPI\Block\Widget\Bestseller">
        <label translate="true">Bestseller</label>
        <description>Widget in Magento2</description>
        <parameters>
            <parameter name="title" xsi:type="text" required="false" visible="true">
                <label>Title</label>
            </parameter>
            <parameter name="description" xsi:type="text" required="false" visible="true">
                <label>Description</label>
            </parameter>
        </parameters>
    </widget>

</widgets>

Bestseller.php

<?php

namespace Relabs\LabAPI\Block\Widget;
use \Magento\Framework\View\Element\Template;
use \Magento\Widget\Block\BlockInterface;

class Bestseller extends \Magento\CatalogWidget\Block\Product\ProductsList  {
    protected function _construct()
    {
        parent::_construct();
        $this->setTemplate('widget/Bestseller.phtml');
    }
    public function createCollection()
    {
        $collection = $this->productCollectionFactory->create();
        $collection = $this->_addProductAttributesAndPrices($collection)
            ->setPageSize(2) // only get 2 products 
            ->setCurPage(1)  // first page (means limit 0,10)
            ->load(); 
        return $collection->getData();
    }


}

Till here everything is working fine. when I am printing $collection, below is the output

Array
(
    [0] => Array
        (
            [entity_id] => 1
            [attribute_set_id] => 15
            [type_id] => simple
            [sku] => 24-MB01
            [has_options] => 0
            [required_options] => 0
            [created_at] => 2018-10-09 06:27:13
            [updated_at] => 2018-10-09 06:27:13
            [price] => 34.0000
            [tax_class_id] => 2
            [final_price] => 34.0000
            [minimal_price] => 34.0000
            [min_price] => 34.0000
            [max_price] => 34.0000
            [tier_price] => 
        )

    [1] => Array
        (
            [entity_id] => 2
            [attribute_set_id] => 15
            [type_id] => simple
            [sku] => 24-MB04
            [has_options] => 0
            [required_options] => 0
            [created_at] => 2018-10-09 06:27:13
            [updated_at] => 2018-10-09 06:27:13
            [price] => 32.0000
            [tax_class_id] => 2
            [final_price] => 32.0000
            [minimal_price] => 32.0000
            [min_price] => 32.0000
            [max_price] => 32.0000
            [tier_price] => 
        )

) 

widget template(Bestseller.phtml)

<?php
  $blockObj= $block->getLayout()->createBlock('Relabs\LabAPI\Block\Widget\Bestseller');
  $blockObj->createCollection();
?>

What should I do here so that it can use existing theme block like "RECENTLY VIEWED ITEMS". I dont want to create custom html templates, I want to use the theme block template. Please suggest ???

2

In Your Bestseller.phtml File

$bestsellerData = [
   'catdata' => $categoryData //get Data in Array
]; 

echo $this->getLayout()->createBlock('Relabs\LabAPI\Block\Widget\Bestseller')->setData($bestsellerData)->setTemplate('categories/bestseller.phtml')->toHtml();
  • What will be 'Your_Templte_Path'. I didn't get it. – Rohit Jaiswal Mar 1 at 11:26
  • setTemplate('folder_name/bestseller.phtml') – Rk Rathod Mar 1 at 11:28
  • Error filtering template: Warning: Illegal offset type in /home/ubuntu/magento2/vendor/magento/framework/DataObject.php on line 76. Check this link magento2.recosenselabs.com – Rohit Jaiswal Mar 1 at 11:37
  • check my updated answer – Rk Rathod Mar 1 at 11:50
  • <?php $categoryData = $block->createCollection(); $bestsellerData = [ 'catdata' => $categoryData ]; echo $this->getLayout()->createBlock('Relabs\LabAPI\Block\Widget\Bestseller')->setData($bestsellerData)->setTemplate('widget/Bestseller.phtml')->toHtml(); ?> This is what I am doing in Bestseller.phtml page. Page is not responding for long time and yet not rendered. – – Rohit Jaiswal Mar 1 at 12:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.