Magento 2 After plugin with parameter
If you need input parameters and you also need to change output, you can use an around plugin or an after plugin:
Example using around
:
public function aroundGetCategoryUrl(
\Magento\Catalog\Helper\Category $subject,
\Closure $proceed,
$category
) {
$originalResult = $proceed($category);
if (...) {
...
return $otherResult;
}
return $originalResult;
}
Example using after
:
public function afterGetCategoryUrl(
\Magento\Catalog\Helper\Category $subject,
$result,
$category
) {
if (...) {
...
return $otherResult;
}
return $result;
}
Just a note:
Please notice that if you are going to change an internal behaviour, a preference could be a better option that a plugin. It depends on what you are going to do.
Since Magento 2.2 it is possible to have input parameters in after plugin
namespace My\Module\Plugin;
class AuthLogger
{
private $logger;
public function __construct(\Psr\Log\LoggerInterface $logger)
{
$this->logger = $logger;
}
/**
* @param \Magento\Backend\Model\Auth $authModel
* @param null $result
* @param string $username
* @return void
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterLogin(\Magento\Backend\Model\Auth $authModel, $result, $username)
{
$this->logger->debug('User ' . $username . ' signed in.');
}
}
See Magento documentation for details https://devdocs.magento.com/guides/v2.2/extension-dev-guide/plugins.html#after-methods