2

This is my registration.php

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

This is my 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>    

This is my 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>

This is my 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] => 
        )

) 

This is my widget template(Bestseller.phtml)

<?php
  $blockObj= $block->getLayout()->createBlock('RecoSense\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 ???

Your Answer

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

Browse other questions tagged or ask your own question.