0

I created this helper method:

app\code\Company\Base\Helper\Product.php

public function test($product = null)
{
    if (!empty($product)) { exit("TRUE, product is not empty!"); } else { return false; }
}

And injected it into a block, so I can use it there:

THEME\Magento_Catalog\layout\catalog_category_view.xml

<referenceBlock name="category.products.list">
    <arguments>
        <argument name="test" xsi:type="helper" helper="Company\Base\Helper\Product::test" />
    </arguments>
</referenceBlock>

Then I call my helper in THEME\Magento_Catalog\templates\product\list.phtml

<?php /** @var $_product \Magento\Catalog\Model\Product */ ?>
<?php foreach ($_productCollection as $_product) :?>
<?php echo $block->getTest($_product); ?>

This is the template for the category view btw.

But I never get to see "TRUE, product is not empty!".

The same code works great in other locations e.g. on the block product.info.media.image with template Magento_Catalog::product/view/gallery.phtml

2 Answers 2

1

You have done wrong. You should use view model https://devdocs.magento.com/guides/v2.3/extension-dev-guide/view-models.html.

I suggest that you have to use view model here instead of helper.

  • Create view model class at app/code/Company/Base/ViewModel/Product.php

    and implement test($product = null) method at there.

    like

    <?php

    namespace Company\Base\ViewModel;

    class Product implements \Magento\Framework\View\Element\Block\ArgumentInterface
    {
        public function test($product = null)
        {
            if (!empty($product) || ($product != null)) {
                return true;
            } else {
                return false; 
             }
        }
    }

phtml code look like:

<?php /** @var $_product \Magento\Catalog\Model\Product */ ?>
<?php $viewModel = $block->getViewModel(); ?>
<?php foreach ($_productCollection as $_product) :?>
<?php echo $viewModel->getTest($_product); ?>
1
  • Thank you for your solution :) what is the advantage over my method? Why should we prefer your solution?
    – Black
    Apr 22, 2020 at 7:42
0

The reason why it does not work is because it is not intended to work like this. It is not a helper like in magento 1.

You can use the helper to get data, but you cannot add dynamic parameters into it from the template.

So I had to use xsi:type="object" instead:

<referenceBlock name="category.products.list">         
    <arguments>
        <argument name="company_helper" xsi:type="object">Company\Base\Helper\Product</argument>
    </arguments>
</referenceBlock>

I also had to implement \Magento\Framework\View\Element\Block\ArgumentInterface in my helper class btw.

class Product extends \Magento\Framework\App\Helper\AbstractHelper implements \Magento\Framework\View\Element\Block\ArgumentInterface{
    ...

Then I used it like this:

$_helper = $block->getCompanyHelper();
$isProductNew = $_helper->isProductNew($_product);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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