I have a ViewModel that returns if a user is logged in or not and we need to add this functionality to vendor/magento/module-page-builder/view/frontend/templates/catalog/product/widget/content/carousel.phtml but we're unsure on the correct way to do this.
Cheers
1 Answer 1
From what I know, you cannot use view models out of the box. But I may be wrong.
But when the widget is rendered it calls the toHtml method from the block.
In your case, I think the block is Magento\CatalogWidget\Block\Product\ProductsList.
I think you can create a before plugin on the toHtml method from the block and attach your view model to the block..
something like this
namespace Venrod\Module\Plugin\CatalogWidget\Block;
class ProductsListPlugin
{
private $viewModel;
public function __construct(\Your\ViewModel\ClassHere $viewModel)
{
$this->viewModel = $viewModel;
}
}
public function beforeToHtml(
\Magento\CatalogWidget\Block\Product\ProductsList $subject
) {
$subject->setData('myViewModel', $this->viewModel);
}
then you should be able to use in your template (which you should overwrite in your theme) this
<php $viewModel = $block->getData('myViewModel');?>
<?php if ($viewModel) : ?>
your logic goes here
<?php endif;?>
-
Thankyou. I will try this method!unknown– unknown2022年03月15日 15:43:24 +00:00Commented Mar 15, 2022 at 15:43