woocommerce について
WordPress と WooCommerce の開発者向けスキルです。プラグインや拡張、EC のカスタマイズを扱い、セキュリティ、互換性、性能に配慮します。
主な機能
- 01WordPress と WooCommerce のコーディング規約
- 02WooCommerce のフックとフィルター
- 03入力検証と出力エスケープ
- 04REST API と商品タイプの拡張
- 05データベースクエリ、キャッシュ、最適化
- 06PHPUnit テストと PHPCS チェック
活用シーン
- 01WooCommerce プラグインの開発
- 02フックによる商品・注文動作のカスタマイズ
- 03REST API の安全な拡張
- 04EC コードのテストと最適化
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ mindrally/ skills --skill woocommerceインストール後、Claude Code で/woocommerceを入力して読み込まれたか確認します。
詳細
WooCommerce 開発
WordPress と WooCommerce の開発、PHP のベストプラクティス、eコマースソリューションに精通しています。
基本原則
- WordPress のコーディング規約に従います
- WooCommerce のフックとフィルターを適切に使用します
- すべてのコードでセキュリティを優先します
- 後方互換性を維持します
- パフォーマンスとスケーラビリティに優れたコードを作成します
PHP のベストプラクティス
コーディング規約
- WordPress の PHP コーディング規約に従います
- 意味のある関数名と変数名を使用します
- 競合を避けるため、すべての関数とクラスにプレフィックスを付けます
- PHPDoc コメントでコードを文書化します
名前空間
namespace MyPlugin\WooCommerce;
class ProductHandler {
public function __construct() {
add_action('woocommerce_before_add_to_cart_form', [$this, 'custom_content']);
}
public function custom_content() {
// Custom functionality
}
}
WooCommerce フック
アクションフック
// Add content after product summary
add_action('woocommerce_after_single_product_summary', 'custom_product_content', 15);
function custom_product_content() {
echo '<div class="custom-content">Additional information</div>';
}
// Modify order processing
add_action('woocommerce_order_status_completed', 'process_completed_order', 10, 1);
function process_completed_order($order_id) {
$order = wc_get_order($order_id);
// Process order
}
フィルターフック
// Modify product price display
add_filter('woocommerce_get_price_html', 'custom_price_html', 10, 2);
function custom_price_html($price, $product) {
if ($product->is_on_sale()) {
$price .= '<span class="sale-badge">Sale!</span>';
}
return $price;
}
// Add custom checkout fields
add_filter('woocommerce_checkout_fields', 'custom_checkout_fields');
function custom_checkout_fields($fields) {
$fields['billing']['billing_custom_field'] = [
'type' => 'text',
'label' => __('Custom Field', 'textdomain'),
'required' => false,
'priority' => 25,
];
return $fields;
}
セキュリティ
データ検証
// Sanitize input
$product_id = absint($_POST['product_id']);
$quantity = wc_stock_amount($_POST['quantity']);
$email = sanitize_email($_POST['email']);
// Escape output
echo esc_html($product->get_name());
echo esc_url($product->get_permalink());
echo wp_kses_post($product->get_description());
nonce の検証
// Create nonce
wp_nonce_field('custom_action', 'custom_nonce');
// Verify nonce
if (!wp_verify_nonce($_POST['custom_nonce'], 'custom_action')) {
wp_die(__('Security check failed', 'textdomain'));
}
権限チェック
if (!current_user_can('manage_woocommerce')) {
wp_die(__('Unauthorized access', 'textdomain'));
}
カスタム商品タイプ
class WC_Product_Custom extends WC_Product {
public function get_type() {
return 'custom';
}
// Custom methods
}
add_filter('product_type_selector', function($types) {
$types['custom'] = __('Custom Product', 'textdomain');
return $types;
});
REST API の拡張
add_action('rest_api_init', function() {
register_rest_route('custom/v1', '/products/featured', [
'methods' => 'GET',
'callback' => 'get_featured_products',
'permission_callback' => '__return_true',
]);
});
function get_featured_products($request) {
$args = [
'status' => 'publish',
'featured' => true,
'limit' => 10,
];
$products = wc_get_products($args);
return rest_ensure_response($products);
}
データベース操作
global $wpdb;
// Use prepare for queries with variables
$results = $wpdb->get_results($wpdb->prepare(
"SELECT * FROM {$wpdb->prefix}wc_orders WHERE status = %s",
'completed'
));
// Use WooCommerce data stores when possible
$product = new WC_Product();
$product->set_name('New Product');
$product->set_regular_price('29.99');
$product->save();
パフォーマンス
- キャッシュには transient を使用します
- データベースクエリを最適化します
- 可能な場合は遅延読み込みを行います
- HTTP リクエストを最小限に抑えます
- オブジェクトキャッシュを使用します
キャッシュ
$cached_data = get_transient('custom_product_data');
if (false === $cached_data) {
$cached_data = expensive_query();
set_transient('custom_product_data', $cached_data, HOUR_IN_SECONDS);
}
プラグインの構成
plugin-name/
├── plugin-name.php
├── includes/
│ ├── class-main.php
│ ├── class-admin.php
│ └── class-frontend.php
├── admin/
│ ├── css/
│ └── js/
├── public/
│ ├── css/
│ └── js/
├── templates/
└── languages/
テスト
- PHPUnit でユニットテストを作成します
- WordPress のテストには WP_UnitTestCase を使用します
- WooCommerce のテストヘルパーを使用してテストします
- PHPCS の WordPress 規約に照らして検証します
コメント
Markdown 対応