ワンクリックで
module-plugin
Implement Magento 2 plugins (interceptors) following best practices for before, after, and around method interception
Codex または Claude でインストール この Prompt をコピーして Codex、Claude、または他のアシスタントに貼り付けると、Skill ページを確認してインストールできます。
メニュー
Implement Magento 2 plugins (interceptors) following best practices for before, after, and around method interception
Codex または Claude でインストール この Prompt をコピーして Codex、Claude、または他のアシスタントに貼り付けると、Skill ページを確認してインストールできます。
SOC 職業分類に基づく
Build Magento 2 admin grids using UI Component listing XML, data providers, and admin controllers with proper ACL integration
Implement Magento 2 GraphQL resolvers — define schema.graphqls types, write query and mutation resolvers, and handle authorization
Run Magento 2 CLI commands through Warden's Docker environment: warden shell, bin/magento, composer, Redis/Valkey, Varnish, OpenSearch, RabbitMQ, n98-magerun2, mutagen sync, and env lifecycle.
Adapt Luma-oriented Magento 2 modules to work with Hyva themes — replace RequireJS and Knockout with Alpine.js, Tailwind CSS, and ViewModels
Configure Magento 2 dependency injection — preferences, virtual types, constructor arguments, and proxy generation via di.xml
Create a new Magento 2 module with the required directory structure, registration files, composer autoloading, and module sequence declarations
| name | module-plugin |
| description | Implement Magento 2 plugins (interceptors) following best practices for before, after, and around method interception |
| installed_version | 1.0.0 |
| magehub_version | 0.1.13 |
Plugins (interceptors) allow you to modify the behavior of public methods without changing the original class. This is a core Magento 2 extension mechanism.
Use plugins when you need to:
Do not use a plugin when a service contract implementation, event observer, layout XML update, extension attribute, or explicit configuration extension point gives the same result with less coupling. Plugins are powerful but they bind the module to a concrete method signature and call order.
Magento plugins only intercept public instance methods on classes managed by the object manager. They do not intercept final methods, final classes, static methods, constructors, destructors, private methods, protected methods, or objects instantiated before interception is generated. Always inspect the target method before writing the plugin.
Prefer before and after plugins because they preserve the original call chain.
Use around plugins only when the plugin must conditionally skip execution,
wrap exceptions, measure execution, or change control flow. An around plugin
must call $proceed(...$args) and return its result unless the task explicitly
requires blocking the original method.
Plugin method signatures must mirror the target method closely:
$subject followed by the original arguments and
return an array of replacement arguments or null.$subject, $result, then the original arguments and
return the replacement result.$subject, callable $proceed, then original
arguments and return a value compatible with the target method return type.Preserve nullable, union, and by-reference parameters exactly. If the target method has optional parameters, keep the same defaults in the plugin method.
Plugin/ directoryetc/di.xmlRegister plugins on the concrete target type or interface in the narrowest
area scope that applies: etc/di.xml, etc/frontend/di.xml, or
etc/adminhtml/di.xml. Give every plugin a stable name, use sortOrder
only when order matters, and set disabled="true" only when intentionally
turning off another module's plugin with a documented reason.
Before changing plugin order or adding an around plugin, inspect sibling
plugins on the same target. Multiple around plugins nest by sort order, and a
single plugin that fails to call $proceed() prevents all downstream plugins
and the original method from running. Keep side effects minimal and avoid
mutating objects returned by after plugins unless the target method contract
allows mutable returns.
After adding or changing a plugin, run bin/magento setup:di:compile through
the project environment wrapper. Add or update a targeted test for the modified
behavior and a pass-through case proving the original method behavior still
works when the plugin condition does not apply.
Modify product name before saving
<?php
declare(strict_types=1);
namespace Vendor\Module\Plugin;
use Magento\Catalog\Model\Product;
class ProductNameNormalizerPlugin
{
public function beforeSetName(Product $subject, string $name): array
{
return [trim(strtoupper($name))];
}
}
Append suffix to product name
<?php
declare(strict_types=1);
namespace Vendor\Module\Plugin;
use Magento\Catalog\Model\Product;
class ProductNameSuffixPlugin
{
public function afterGetName(Product $subject, string $result): string
{
return $result . ' - On Sale';
}
}
Plugin declaration
<?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="Magento\Catalog\Model\Product">
<plugin name="vendor_module_product_name_normalizer"
type="Vendor\Module\Plugin\ProductNameNormalizerPlugin"
sortOrder="10"/>
</type>
</config>
Path template:
Plugin/{ClassName}Plugin.php
Plugin class file
<?php
declare(strict_types=1);
namespace {{vendor}}\{{module}}\Plugin;
class {{className}}Plugin
{
public function before{{method}}($subject, ...$args): array
{
return [...$args];
}
}
Path template:
etc/di.xml
Plugin declaration
<?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="{{targetClass}}">
<plugin name="{{pluginName}}"
type="{{vendor}}\{{module}}\Plugin\{{className}}Plugin"
sortOrder="10"/>
</type>
</config>