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 Plugin 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 disabled="false" name="Mage2Gen_Module_Plugin_Test" sortOrder="10" type="Mage2Gen\Module\Plugin\Test"/>
	</type>
</config>

Plugin/Test.php

<?php


namespace Mage2Gen\Module\Plugin;

class Test
{

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