Magento 2 - Get product details in list view adding custom block using plugin

Magento 2 - Get product details in list view adding custom block using plugin

September 15, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento 2 - Get product details in list view adding custom block using plugin

Magento 2: Accessing Product Details in List View Using Custom Plugin

In Magento 2, modifying the product list view is a common requirement for many developers. One of the most efficient ways to achieve this is by using the plugin system to add a custom block and retrieve product details.

Understanding the Plugin System

The plugin system in Magento allows you to modify or extend existing functionality without directly changing core files. It’s particularly useful when you want to alter the behavior of third-party extensions or Magento’s built-in features.

To create a plugin, you need to define it in your module’s di.xml file and then implement the logic in the PHP class.

Setting Up the Plugin

Let’s start by setting up the plugin as shown in the provided example:

<type name="Magento\Catalog\Block\Product\AbstractProduct">
    <plugin name="vendor.module.category.products.list" type="Vendor\Module\Plugin\Block\Product\AbstractProduct" />
</type>

In this configuration, we are creating a plugin for the Magento\Catalog\Block\Product\AbstractProduct class. The plugin is named “vendor.module.category.products.list” and points to our custom PHP class located at Vendor/Module/Plugin/Block/Product/AbstractProduct.php.

Implementing the Plugin Logic

In your custom PHP class, you need to implement the method that corresponds to the Magento event/behavior you want to modify. In this case, we are overriding the afterGetProductDetailsHtml method:

namespace Vendor\Module\Plugin\Block\Product;

class AbstractProduct
{
    public function afterGetProductDetailsHtml(
        \Magento\Catalog\Block\Product\AbstractProduct $subject,
        $result
    ) {
         // Here is where I want to pull product details like SKU
         // I also want to pull my custom helper method here
         return '<b>rendered additional html element</b>';
    }
}

The afterGetProductDetailsHtml method is a callback that is triggered after the getProductDetailsHtml method of the abstract product block is executed. This is where you can add your custom logic to modify the HTML rendering.

Accessing Product Details

To access product details within this plugin, you can use the $subject parameter passed to the afterGetProductDetailsHtml method. The $subject represents an instance of the original Magento class that is being extended by the plugin.

Here’s how you can retrieve the product object:

namespace Vendor\Module\Plugin\Block\Product;

class AbstractProduct
{
    public function afterGetProductDetailsHtml(
        \Magento\Catalog\Block\Product\AbstractProduct $subject,
        $result
    ) {
        $product = $subject->getProduct();

        // Now you can access product details like SKU, name, etc.
        $sku = $product->getSku();
        $name = $product->getName();

         // Add your custom logic here...

         return '<b>rendered additional html element</b>';
    }
}

You can use the get() method to access any product attribute. For example, $product->getSku() retrieves the SKU of the current product being rendered.

Retrieving Custom Helper Method

If you want to pull data from a custom helper class within your plugin, you’ll need to inject it as a dependency in your PHP class. To do this, you can use the constructor injection method:

namespace Vendor\Module\Plugin\Block\Product;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\ObjectManagerInterface;

class AbstractProduct
{
    private $customHelper;

    public function __construct(
        \Magento\Catalog\Block\Product\AbstractProduct $subject,
        AbstractHelper $customHelper
    ) {
        $this->customHelper = $customHelper;
    }

    // Rest of the class remains the same...
}

In this example, we are injecting an instance of your custom helper class into the constructor. You can then access its methods within your plugin logic.

Conclusion

Using the plugin system in Magento 2 is a powerful way to add custom functionality without modifying core files or third-party extensions directly. By following the steps outlined above, you can efficiently modify the product list view and retrieve essential product details using plugins.

Remember to test your changes thoroughly after implementing them to ensure they don’t introduce any unexpected behavior into your store’s front-end or back-end operations.

For more advanced techniques on working with Magento 2 plugins and customizations, I recommend exploring our comprehensive guides at Laravel Company.

Magento 2 Catalog Extensions Configuration

Our network