WordPress Plugin Foundation
At its core, WooCommerce is a WordPress plugin that extends WordPress functionality to provide e-commerce capabilities. The plugin integrates deeply with WordPress through:Plugin Entry Point
The main plugin filewoocommerce.php bootstraps the entire system:
plugins/woocommerce/woocommerce.php
Main Plugin Class
TheWooCommerce class in includes/class-woocommerce.php manages the plugin lifecycle:
Hook System
WooCommerce leverages WordPress’s action and filter hook system extensively for extensibility.Actions and Filters
The hook system allows plugins and themes to modify WooCommerce behavior without modifying core files:All hook registration in modern code (
src/ directory) should happen in the init() method of the class.Template System
WooCommerce uses a template hierarchy system similar to WordPress themes:plugins/woocommerce/includes/wc-template-functions.php
API Layers
WooCommerce provides multiple API layers for different use cases:REST API
The WordPress REST API integration provides endpoints for external integrations:- Store API: Modern API for blocks and frontend applications (
/wc/store/v1/*) - WC REST API: Admin and integration API (
/wc/v3/*) - Legacy API: Deprecated older API versions
Data Store Abstraction
WooCommerce uses data store classes to abstract database operations:Container-Based Services
Modern code uses the dependency injection container to access services:Modern vs Legacy Code Patterns
WooCommerce architecture includes two distinct coding approaches:Legacy Patterns (includes/ directory)
- Global functions and variables
- Direct WordPress API calls
- Singleton pattern for main classes
- Hook registration in constructors
includes/class-woocommerce.php
Modern Patterns (src/ directory)
- PSR-4 autoloading
- Dependency injection
- Type hints and strict types
- Separation of concerns
src/Internal/AssignDefaultCategory.php
New code should always be written in the
src/ directory using modern patterns. The includes/ directory is maintained for backwards compatibility.Directory Structure
The architecture is reflected in the directory organization:Namespace Organization
All modern code uses theAutomattic\WooCommerce namespace:
- Root namespace:
Automattic\WooCommerce - Internal classes:
Automattic\WooCommerce\Internal\* - Public API classes:
Automattic\WooCommerce\*(root level) - Blocks:
Automattic\WooCommerce\Blocks\* - Store API:
Automattic\WooCommerce\StoreApi\*
Key Architectural Principles
1
Backwards Compatibility
Maintain compatibility with existing extensions and themes by preserving legacy patterns in
includes/.2
Progressive Enhancement
Add new features using modern patterns in
src/ while keeping legacy code functional.3
Separation of Concerns
Use dependency injection and single-responsibility classes in modern code.
4
WordPress Integration
Leverage WordPress APIs (hooks, caching, database) while abstracting them in modern code.
Next Steps
Monorepo Workflow
Learn about the pnpm workspace structure and build tools
Modern vs Legacy
Understand when to use src/ vs includes/ directories
Dependency Injection
Master the DI container and service resolution