Plugin

Creates a Plugin

Plugins are designed to overwrite core Magento methods or methods from other 3rd party modules.

You can choose to change it before, after, or around the original method is called.

Example

Change the product name to show pipes before and after the name.

Input for the plugin form

  • classname: MagentoCatalogModelProduct
  • methodname: getName
  • plugintype: After
public function afterGetName(
        Magento\Catalog\Model\Product $subject,
        $result
){
        return '|'.$result.'|';
}

Use the snippet in the Magento 2 module creator.

Files

etc/di.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
	<type name="Test">
		<plugin name="Mage2Gen_Module_Plugin_Test" type="Mage2Gen\Module\Plugin\Test" sortOrder="10" disabled="false"/>
	</type>
</config>

Plugin/Test.php

<?php
declare(strict_types=1);

namespace Mage2Gen\Module\Plugin;

class Test
{

    public function afterTest(\Test $subject, $result, //$functionParam) {
        //Your plugin code
        return $result;
    }
}