I want to add text after image on product list page. For that I created plugin for getImage() function of vendor/magento/module-catalog/Block/Product/AbstractProduct.php
Plugin Code:
<?php
namespace Vendor\Module\Model;
class Product extends \Magento\Catalog\Block\Product\AbstractProduct
{
public function afterGetImage(\Magento\Catalog\Model\Product $subject, $result)
{
$result .= 'Testing From Plugin';
return $result;
}
}
But I'm getting this error:
Uncaught TypeError: Argument 2 passed to Magento\Catalog\Block\Product\AbstractProduct::__construct() must be of the type array, object given
What I am doing wrong?
2 Answers 2
It create an Issue because return type of getImage is \Magento\Catalog\Block\Product\Image and you are trying to convert into string. I think by just appending the text you can not achieve your goal.
I am not sure what is your exact goal but I can assume that you need to add additional attribute to product Image attribute so you can use it in your phtml file. For that you can print the $result->getData() in your log file you can see that number of product Image attribute. So you can add your custom attribute by appending this data.
In log file I am getting,
array (
'image_url' => 'My Product image URL,
'width' => '240',
'height' => '300',
'label' => 'test',
'ratio' => 1.25,
'custom_attributes' => '',
'resized_image_width' => 135,
'resized_image_height' => 135,
)
You can use with aroundPlugin and check,
public function aroundGetImage(
\Magento\Catalog\Block\Product\AbstractProduct $subject,
\Closure $proceed,
\Magento\Catalog\Model\Product $product,
$imageId,
$attributes = []
) {
$result = $proceed($product, $imageId, $attributes);
$newStr = 'Testing From Plugin';
return $result. $newStr;
}
Argument 1 passed to Module\Model\Product::afterGetImage() must be an instance of Magento\Catalog\Model\Product, instance of Magento\Catalog\Block\Product\ListProduct\Interceptor given,