0

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

asked Mar 15, 2022 at 14:23

1 Answer 1

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;?>
answered Mar 15, 2022 at 14:47
1
  • Thankyou. I will try this method! Commented Mar 15, 2022 at 15:43

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.