关于 woocommerce
面向 WordPress 和 WooCommerce 开发者的开发技能,涵盖插件、扩展及电商定制实践,强调安全、兼容性和性能。
主要功能
- 01WordPress 与 WooCommerce 编码规范
- 02使用 WooCommerce 钩子与过滤器
- 03输入验证与输出转义
- 04扩展 REST API 与产品类型
- 05数据库查询、缓存与性能优化
- 06PHPUnit 测试与 PHPCS 检查
适用场景
- 01开发 WooCommerce 插件
- 02通过钩子定制产品和订单行为
- 03安全地扩展 REST API
- 04测试并优化电商代码
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ mindrally/ skills --skill woocommerce安装后在 Claude Code 中输入/woocommerce确认已加载。
详细介绍
WooCommerce 开发
你是 WordPress 和 WooCommerce 开发、PHP 最佳实践及电子商务解决方案方面的专家。
核心原则
- 遵循 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 编写单元测试
- 使用 WP_UnitTestCase 进行 WordPress 测试
- 使用 WooCommerce 测试辅助工具进行测试
- 使用 PHPCS WordPress 标准进行验证
评论
支持 Markdown