0

It seems to be impossible to use a helper in the template Magento_Catalog\templates\product\list\items.phtml.

I have tried it via argument injection:

app\design\frontend\company\base\Magento_Catalog\layout\override\base\catalog_product_view.xml

<!-- Related Products Slider -->
    <block class="Magento\Catalog\Block\Product\ProductList\Related"
           name="catalog.product.related"
           template="Magento_Catalog::product/list/items.phtml"
    >
        <arguments>
            <argument name="type" xsi:type="string">related</argument>
            <argument name="company_helper" xsi:type="object">Company\Base\Helper\Product</argument>
        </arguments>
        <block class="Magento\Catalog\Block\Product\ProductList\Item\Container"
               name="related.product.addto" as="addto"
        >
            <block class="Magento\Catalog\Block\Product\ProductList\Item\AddTo\Compare"
                   name="related.product.addto.compare" as="compare"
                   template="Magento_Catalog::product/list/addto/compare.phtml"
            />
        </block>
    </block>

<!-- Upsell Products Slider -->
    <block class="Magento\Catalog\Block\Product\ProductList\Upsell"
           name="product.info.upsell"
           template="Magento_Catalog::product/list/items.phtml"
    >
        <arguments>
            <argument name="type" xsi:type="string">upsell</argument>
            <argument name="company_helper" xsi:type="object">Company\Base\Helper\Product</argument>
        </arguments>
        <block class="Magento\Catalog\Block\Product\ProductList\Item\Container"
               name="upsell.product.addto" as="addto"
        >
            <block class="Magento\Catalog\Block\Product\ProductList\Item\AddTo\Compare"
                   name="upsell.product.addto.compare" as="compare"
                   template="Magento_Catalog::product/list/addto/compare.phtml"
            />
        </block>
    </block>

and by extending the block classes.

I even extended the block class \Magento\Catalog\Block\Product\AbstractProduct

<?php

namespace Company\Base\Block\Product;

class AbstractProduct extends \Magento\Catalog\Block\Product\AbstractProduct
{
    protected $productHelper;

    /**
     * @param Context $context
     * @param array $data
     */
    public function __construct(
        \Magento\Catalog\Block\Product\Context $context
        , array $data = []
        , \Company\Base\Helper\Product $productHelper
    ) {
        $this->productHelper = $productHelper;
        parent::__construct($context, $data);
    }

    public function getProductHelper()
    {
        return $this->productHelper;
    }

}

But if I try to use the helper in the template:

$productHelper = $block->getProductHelper();  // also tried $this instead of $block
echo $productHelper->test();  // should output "success"

Then I get an error saying that $productHelper is null

2

try with

$this->helper(\Company\Base\Helper\Product::class);

inside the template

and why it didn't work for you when you overridden the AbstractProduct class...

You extended the class, but your new class is never instantiated. The blocks used in by the template still use the original AbstractProduct class.
overriding works only for classes that get instantiated. The AbstractProduct class is only extended by other classes, but is never instantiated.

| improve this answer | |
  • Thank you, I also had an error in one of the blocks which was causing the issue. But now I understand why it makes no sense to extend the AbstractProduct class – Black May 27 at 15:24

Your Answer

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

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