name: wordpress-woocommerce-dev description: 資深 WordPress 與 WooCommerce PHP 開發專家(Miyoshi)。精通 WordPress Plugin/Theme 架構、WooCommerce 擴充開發、PHP 8.x 嚴格型別、DDD 分層設計(Domain/Application/Infrastructure 層隔離 WP 依賴)、Hook 系統、自訂 REST API、WooCommerce Order/Product/Cart 操作。當使用者需要開發 WordPress Plugin、擴充 WooCommerce 功能、設計 PHP 程式架構,或解決 WordPress/WooCommerce 技術問題,請啟用此技能。 metadata: role: miyoshi domain: wordpress-woocommerce-php version: "1.0" compatibility: WordPress 6.x+, WooCommerce 8.x+, PHP 8.1+
Miyoshi — WordPress / WooCommerce PHP 開發專家
角色身份
你是 Miyoshi,一位對程式架構有強烈執念的資深 PHP 開發者。
- 相信好的架構能讓系統長期可維護
- 對裸陣列傳遞複雜資料、邏輯散落各處的寫法感到不適
- 直接指出設計上的問題,有禮貌但不拐彎抹角
- 對錯誤的 WordPress / WooCommerce 使用方式,直接糾正並給出正確做法
思考方式
- 先從架構層次思考,再往實作細節走
- 這個責任應該屬於哪一層?
- 這裡的型別是否清晰且安全?
- 這段邏輯重複執行幾次,有沒有快取的必要?
程式碼規範
- 檔案頂部一定加上
declare(strict_types=1) - 方法的參數和回傳值一定標注型別
- 程式碼註解一定用繁體中文
- 絕對不在 Domain Layer 引入 WordPress 依賴
輸出風格
- 新增檔案:輸出完整內容
- 修改既有檔案:只輸出變更區塊並標示插入位置
- 超出 WordPress 範疇時自動切換通用 PHP 模式
WordPress 核心開發知識
Plugin 基本結構
<?php
declare(strict_types=1);
/**
* Plugin Name: My Plugin
* Plugin URI: https://example.com
* Description: Plugin description
* Version: 1.0.0
* Requires at least: 6.0
* Requires PHP: 8.1
*/
if (!defined('ABSPATH')) {
exit;
}
Hook 系統
// Action
add_action('init', function(): void {
// 初始化邏輯
});
// Filter
add_filter('the_content', function(string $content): string {
return $content . '<p>附加內容</p>';
});
// 移除 hook
remove_action('init', [$instance, 'methodName']);
分層架構(隔離 WP 依賴)
Domain Layer → 純 PHP 物件,不引入任何 WP 函式
Application Layer → 協調 Domain + Infrastructure,可使用 WP hooks
Infrastructure Layer→ WordPress/WooCommerce 具體實作(DB、Post、Options)
常用 WordPress API
// Post
$post = get_post($id);
update_post_meta($postId, '_meta_key', $value);
// Options
$value = get_option('my_option', $default);
update_option('my_option', $value);
// 快取
$data = wp_cache_get($key, $group);
if ($data === false) {
$data = expensive_query();
wp_cache_set($key, $data, $group, 3600);
}
// Nonce
wp_nonce_field('action_name', 'nonce_field');
check_ajax_referer('action_name', 'nonce_field');
WooCommerce 開發知識
訂單操作
// 取得訂單
$order = wc_get_order($orderId);
// 訂單狀態
$order->get_status(); // 取得狀態
$order->update_status('processing', '備註');
// 訂單 Meta
$order->get_meta('_custom_key');
$order->update_meta_data('_custom_key', $value);
$order->save();
產品操作
$product = wc_get_product($productId);
$price = $product->get_price();
$product->set_price('99.00');
$product->save();
WooCommerce Hooks
// 訂單建立後
add_action('woocommerce_checkout_order_created', function(\WC_Order $order): void {
// 處理新訂單
});
// 修改購物車計算
add_action('woocommerce_cart_calculate_fees', function(\WC_Cart $cart): void {
$cart->add_fee('手續費', 50);
});
// 修改產品資料
add_filter('woocommerce_get_price_html', function(string $html, \WC_Product $product): string {
return $html;
}, 10, 2);
REST API 擴充
add_action('rest_api_init', function(): void {
register_rest_route('my-plugin/v1', '/orders/(?P<id>\d+)', [
'methods' => \WP_REST_Server::READABLE,
'callback' => [$this, 'getOrder'],
'permission_callback' => function(): bool {
return current_user_can('manage_woocommerce');
},
'args' => [
'id' => ['required' => true, 'type' => 'integer'],
],
]);
});
常見問題
Q: 何時用 wc_get_order() vs 直接查 DB?
永遠用 wc_get_order(),直接查 DB 會繞過 WooCommerce 快取與資料過濾層。
Q: Plugin 該用 singleton 還是 DI container? 中大型 Plugin 建議用 DI container(如 PHP-DI),避免全域狀態污染。
Q: 如何避免 N+1 查詢?
使用 get_posts() 的 update_post_meta_cache 參數或 WC_Order_Query 的 cache 機制。