How to set up Claude Code for a Magento 2 project
To set up Claude Code for Magento 2 you need four things: a CLAUDE.md with the bin/magento commands, the coding standard and the rule that vendor/ is never edited; skills in .claude/skills/ for repeated tasks such as a plugin, PHPCS and PHPUnit; permission rules and a PreToolUse hook in .claude/settings.json that block edits under vendor/ and commands against production; and a per-ticket workflow where a human reads the diff before a merge. Every file format and command below is checked against the Claude Code and Adobe Commerce docs, November 2026.
I am Oleksandr Osadchyi, an ex-Magento 2 core engineer with commits in the public magento/magento2 repository and a Claude Certified Architect - Professional. Every ticket I take goes through Claude Code and then through my review; this is the setup I use.
Step 1. Install Claude Code
On macOS, Linux and WSL the native installer is one line; start it from the Magento root so it sees the project files:
curl -fsSL https://claude.ai/install.sh | bash
claude --version
cd /var/www/shop
claude
Windows PowerShell uses irm https://claude.ai/install.ps1 | iex; Homebrew and npm (Node.js 22 or newer) installers are in the docs. You need a Pro, Max, Team, Enterprise or Console account. The native install updates itself; claude doctor prints diagnostics. If Magento runs in Docker, run Claude Code on the host and prefix commands in CLAUDE.md with docker compose exec php or ddev exec.
Step 2. Write a CLAUDE.md for the Magento project
CLAUDE.md lives at ./CLAUDE.md or ./.claude/CLAUDE.md and loads at the start of every session; /init writes a first draft from your code base. The docs ask for under 200 lines of facts that matter in every session; multi-step procedures go into skills. The docs also say CLAUDE.md is context, not enforcement: a rule that must hold whatever the model decides needs a hook or a permission rule (Step 4). Personal notes go into CLAUDE.local.md, added to .gitignore; rules for one part of the tree go into .claude/rules/*.md with a paths field, loaded only when Claude touches matching files. A realistic file for a store:
# Shop (Magento Open Source 2.4.x, PHP 8.3, MariaDB, OpenSearch)
## Layout
- Custom modules: app/code/Acme/*. Theme: app/design/frontend/Acme/default.
- vendor/ and generated/ are Composer and compiler output. Never edit them.
Core behaviour changes go through a plugin, a preference or a Composer patch.
- app/etc/env.php holds credentials. Read it only when a task needs it; never print it.
- Local stack: ddev. Run every command below as `ddev exec <command>`.
## Commands
- bin/magento setup:upgrade after a new module, db_schema.xml or a setup patch
- bin/magento setup:di:compile after di.xml, constructor or plugin changes
- bin/magento cache:clean after config, layout or template changes
- bin/magento indexer:reindex after price, stock or attribute logic changes
- bin/magento setup:static-content:deploy -f en_US only when testing production mode
- vendor/bin/phpcs --standard=Magento2 app/code/Acme
- vendor/bin/phpunit -c dev/tests/unit/phpunit.xml.dist app/code/Acme/<Module>/Test/Unit
## Rules
- Plugins (before/after) over preferences, preferences over copying a class.
No around plugin unless the original must not run; say why in the PR.
- No direct ObjectManager use outside factories and tests.
- New tables and columns: db_schema.xml plus
bin/magento setup:db-declaration:generate-whitelist --module-name=Acme_<Module>.
- Admin controllers need an ACL resource; frontend output is escaped with $escaper.
- Done means: phpcs clean, unit tests for new classes, setup:upgrade and
setup:di:compile pass. Then stop and show me the diff.
- Never run a command against a host other than the local ddev container.
- Branch per ticket: feature/<ticket-id>. Commit only when asked.
Each line is there because the model gets it wrong without it:
- it edits a vendor file to fix a bug;
- reaches for the
ObjectManager; - adds a column without the whitelist;
- forgets
di:compileand reports that the plugin does not fire.
Step 3. Skills for the tasks you repeat
A skill is a file .claude/skills/NAME/SKILL.md with a short frontmatter and instructions. Claude loads it when the description matches the task, or you call /NAME with arguments; $ARGUMENTS holds all of them, $0 and $1 single ones. disable-model-invocation: true limits a skill to manual calls. The older .claude/commands/NAME.md files were merged into skills and keep working; the docs prefer skills for new work. My plugin skill:
---
name: magento-plugin
description: Write a Magento 2 plugin (interceptor) for a public method. Use when asked to change what a core or third-party method does without editing it.
argument-hint: [Class::method] [Acme_Module]
---
Target method: $0. Module that owns the plugin: $1.
1. Open the target class. Confirm the method is public, not static, not final,
and the class is not final and not a virtual type. If any of this fails, stop
and propose a preference or an observer instead.
2. grep every di.xml for existing plugins on this class and note their sortOrder.
3. Write the plugin in app/code/Acme/<Module>/Plugin/. Prefer an after method.
Use before only to change arguments. Use around only when the original must
not run, and say why above the class.
4. Declare it in etc/di.xml, or etc/frontend/di.xml and etc/adminhtml/di.xml when
it is area-specific, with a unique name and a sortOrder.
5. Add a unit test under Test/Unit/Plugin/.
6. Run bin/magento setup:di:compile, phpcs on the changed files and phpunit on the
test directory. Paste the three results, then stop for review.
Point 1 is the list of plugin limitations from the Adobe docs:
- final methods and classes;
- non-public and static methods;
- constructors;
- virtual types;
- objects created before interception boots.
The prefer-after, avoid-around rule is Adobe's too; around plugins grow stack traces and cost performance.
Three more skills earn their place.
magento-modulecreatesregistration.php,etc/module.xmlandcomposer.json, then runs these in that order, as the Adobe docs describe:bin/magento module:enable Acme_Module bin/magento setup:upgrade bin/magento cache:cleanphpcsrunsvendor/bin/phpcs --standard=Magento2on a path, appliesvendor/bin/phpcbffor what it can fix, then fixes the rest by hand until the run is clean.magento-testruns the unit suite for one module and, when a database is involved, the integration suite fromdev/tests/integrationagainst the test database ininstall-config-mysql.php, never the development one.
Step 4. Hooks and permissions that block dangerous actions
Team settings live in .claude/settings.json; .claude/settings.local.json is yours and stays out of git. The permissions block holds allow, ask and deny rules. Edit(path) rules apply to every built-in tool that edits files and, per the docs, to shell output redirects too. A path starting with one slash is anchored at the project root. Deny rules apply at once, without the trust step allow rules wait for.
{
"permissions": {
"allow": [
"Bash(bin/magento cache:clean *)",
"Bash(bin/magento setup:di:compile)",
"Bash(vendor/bin/phpcs *)",
"Bash(vendor/bin/phpunit *)"
],
"deny": [
"Edit(/vendor/**)",
"Edit(/generated/**)",
"Edit(/app/etc/env.php)"
]
},
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{ "type": "command",
"command": "${CLAUDE_PROJECT_DIR}/.claude/hooks/no-production.sh" }
]
}
]
}
}
The deny rules cover vendor/; the hook covers production. A PreToolUse hook runs before the tool call, gets the call as JSON on stdin and blocks it when the script exits with code 2; stderr goes back to Claude as the reason. For Bash the command is in tool_input.command. The script needs jq and chmod +x:
#!/bin/bash
# .claude/hooks/no-production.sh
COMMAND=$(jq -r '.tool_input.command')
for pattern in 'shop.example.com' 'ssh deploy@' 'magento-cloud' 'mysql -h prod'; do
if echo "$COMMAND" | grep -qF -- "$pattern"; then
echo "Blocked: '$pattern' points at production. Claude Code works on the local stack only." >&2
exit 2
fi
done
exit 0
Put your real production host names in the list. For files with a reason attached, add a second group with "matcher": "Edit|Write" and a script that reads .tool_input.file_path and exits 2 under vendor/ or generated/. The blunt option, "Bash(ssh *)" in deny, works when Claude never needs a remote machine.
Step 5. The per-ticket workflow
- Analysis. Start in plan mode (
claude --permission-mode plan, or Shift+Tab in a session): Claude reads and explores but does not edit. Ask for the code path, the modules involved, existing plugins and preferences on those classes, and a plan. Correct the plan before any code exists; this is the cheapest place to be wrong. - Code. Approve the plan, work on the ticket branch, use the skills. Compile after each DI change;
setup:di:compileis the fast feedback for a wrong constructor ordi.xml. - Tests. phpcs clean, unit tests for new classes, integration tests for anything touching the database or an indexer,
setup:upgradeon a fresh database copy to prove a schema change from zero. - Review. Me, every file. I look for the items in the next section, plus
ObjectManagercalls, around plugins, raw SQL where a repository exists, a preference where a plugin was enough, admin controllers without ACL and unescaped output. Sometimes a fresh Claude session reviews the diff without my conclusions. The ticket is not done until a human has read the diff. - Release. The Adobe sequence on the server, by me or by CI (
composer updateonly if packages changed):
Claude Code never touches that machine; the hook makes sure.bin/magento maintenance:enable composer update bin/magento setup:upgrade bin/magento setup:di:compile bin/magento setup:static-content:deploy bin/magento cache:clean bin/magento maintenance:disable
What AI does well and badly on Magento 2
Good, in my experience:
- boilerplate (modules, plugins, observers, data and schema patches, console commands, cron jobs, REST clients for an ERP);
- reading
vendor/to find where a value is computed across twenty modules, the task where it saves the most time; - unit tests and mocks for existing classes;
- PHPCS and PHPStan fixes;
- upgrade chores such as deprecated classes, PHP 8 fixes and moving
InstallSchematodb_schema.xml; - explaining a stack trace.
Bad, concretely:
- Layout XML. It invents block and container names, takes a handle from another theme or version, and cannot see the rendered page. Make it list the real blocks from the theme's layout files first.
- EAV. It joins
catalog_product_entity_varcharby hand and forgets store scope (store_id0 against a store view), attribute set and backend type. Push it to repositories and collections withaddAttributeToSelect. - Indexers and cache. It forgets that a price or stock change needs a reindex or a mview subscription, and that a new dynamic block needs cache identities or private content, so full page cache serves stale HTML. Here I read every line.
- DI configuration. Good at a plugin in
di.xml, weak on merge order across global and area files and on virtual types. The compiler catches syntax, not a wrong area. - Version drift. It proposes classes removed in 2.4.x or patterns from 2.2 tutorials. The exact version in
CLAUDE.mdhelps; pointing it at this installation'svendor/magento/helps more.
None of this makes the tool bad. It means the reviewer must know Magento better than the model does; that review is where a senior engineer earns the hour.
If you want this set up in your repository, or a backlog worked through this way, I do it by the hour, paid for hours worked. Write to alex@osadchyi.dev; my rate and how I work are on the pricing page.
FAQ
Can Claude Code edit vendor/ by accident?
Not with Edit(/vendor/**) in the deny list: the rule covers every built-in file tool and shell redirects into that path. CLAUDE.md alone is not enough; the docs call it context, not enforcement. Core changes go through a plugin, a preference or a Composer patch.
Does Claude Code replace a Magento developer?
No. It writes module code, tests and upgrade chores fast and reads a large vendor/ tree faster than a person. Layout XML, EAV scope, indexers and cache invalidation still need a reviewer who knows Magento; a bad merge there costs more than the time saved.
Do I need a skill for every bin/magento command?
No. Single commands and when to run them belong in CLAUDE.md, which loads every session. A skill is for a multi-step procedure you repeat, such as writing a plugin, and loads only when needed.
Which permission mode should I use?
Manual, the default, asks before each shell command and edit; right for a store with real credentials in env.php. Plan mode for analysis. Skip bypassPermissions on any machine that can reach production.