Add after method with plugin on underscore methods

You shouldn't be using plugins for private or protected methods. As plugins only work for public methods.

By convention in Magento functions & variables that start with an underscore indicate that the variable or function is protected or private, hence why I am assuming you are trying to plugin into a private or protected method.

A plugin, or interceptor, is a class that modifies the behavior of public class functions by intercepting a function call and running code before, after, or around that function call .. readmore


Neither of these answers actually address the issue in the title: how to define an interceptor for a method which name begins with an underscore; although the assumption that the developer is likely to be trying to intercept a protected/private method, you sometimes stumble upon someone else's code that does't follow such convention.

On Magento 2.3.1 it works by simply prefixing after to the method name like in:

public function _doFoo(...)

public function after_doFoo(...)

In order to validate a new Plugin definition, I found this code useful

$pluginValidator = $objectManager->get(\Magento\Framework\Interception\Code\InterfaceValidator::class);
$pluginValidator->validate(FooPlugin::class, Foo::class);

You can see the internals of how the method names are being mapped in \Magento\Framework\Interception\Code\InterfaceValidator::getOriginMethodName