I want to make own recently viewed widget, cause this standard loads items with javascirpt (I want to have items directly). So I made this code, based on other posts:
namespace AA\WidgetRecently\Block\Widget;
use Magento\Framework\View\Element\Template;
use Magento\Widget\Block\BlockInterface;
class Data extends Template implements BlockInterface
{
protected $_template = "widget/recently.phtml";
protected $recentlyViewed;
public function __construct(
\Magento\Catalog\Block\Product\Context $context,
\Magento\Reports\Block\Product\Viewed $recentlyViewed,
\Magento\Catalog\Block\Product\ImageBuilder $imageBuilder,
array $data = []
) {
$this->recentlyViewed = $recentlyViewed;
$this->imageBuilder = $imageBuilder;
parent::__construct( $context, $data );
}
public function getRecentlyViewedProducts(){
return $this->recentlyViewed->getItemsCollection();
}
}
and this code in template
<?php if ($exist = ($block->getRecentlyViewedProducts() && $block->getRecentlyViewedProducts()->getSize())): ?>
returns nothing, is false.
But when I add block in layout file:
<block class="Magento\Reports\Block\Product\Widget\Viewed" name="recently_viewed" cacheable="false" template="Magento_Reports::widget/viewed/content/viewed_grid.phtml">
<action method="setPageSize">
<argument name="page_size" xsi:type="number">5</argument>
</action>
</block>
the widget phtml is working and shows products (and obviously recently products are repeated cause of adding block in xml layout).
Why widget is not working, it looks like something more should be initialized?