办公效率
WooCommerce订阅计费开发
为会员、软件和实物订阅提供商品创建及周期计费的实现参考,覆盖试用、套餐切换、续费和付款失败重试。
# WooCommerce Subscriptions
## 概述
WooCommerce Subscriptions(官方付费插件)新增了订阅产品类型,包括简单订阅和可变订阅。其计费周期可配置为每日、每周、每月或每年,并支持免费试用、注册费以及按比例计算费用的升级和降级。它可与 Stripe、PayPal Reference Transactions 及其他支持自动循环扣费的支付网关集成。通过丰富的 WordPress 动作和过滤器,自定义逻辑可以接入订阅生命周期。
## 何时使用此 Skill
- 按周期性计费计划销售产品或服务时(SaaS、会员服务、订阅盒子)
- 实现订阅升级、降级或套餐切换时
- 构建自定义续订逻辑,或为支付失败重试添加业务规则时
- 将订阅状态与访问控制集成时(例如会员网站的内容访问限制)
- 使用自定义数据扩展订阅邮件或管理员报告时
- 创建订阅附加服务或按单位数量调整规模时
## 核心说明
1. **以编程方式创建订阅产品**
```php
<?php
// Create a simple subscription product via code (e.g., in a migration script)
$product = new WC_Product_Subscription();
$product->set_name('Monthly Pro Plan');
$product->set_status('publish');
$product->set_regular_price('29.99');
// Save first so the product gets an ID
$product->save();
// Subscription-specific meta (product must be saved before setting post meta)
update_post_meta($product->get_id(), '_subscription_price', '29.99');
update_post_meta($product->get_id(), '_subscription_period', 'month');
update_post_meta($product->get_id(), '_subscription_period_interval', '1');
update_post_meta($product->get_id(), '_subscription_length', '0'); // 0 = forever
update_post_meta($product->get_id(), '_subscription_trial_length', '14');
update_post_meta($product->get_id(), '_subscription_trial_period', 'day');
update_post_meta($product->get_id(), '_subscription_sign_up_fee', '0');
```
2. **接入订阅生命周期事件**
WooCommerce Subscriptions 在生命周期的每个阶段都会触发特定动作:
```php
<?php
// When a new subscription is created (after successful first payment)
add_action('woocommerce_subscription_status_active', function ($subscription) {
$user_id = $subscription->get_user_id();
$plan = $subscription->get_items(); // Array of WC_Order_Item_Product
// Grant access — e.g., set user role or update capabilities
$user = new WP_User($user_id);
$user->add_role('subscriber_member');
// Track in analytics
error_log("Subscription {$subscription->get_id()} activated for user {$user_id}");
});
// When a renewal payment succeeds
add_action('woocommerce_subscription_renewal_payment_complete', function ($subscription, $last_order) {
$user_id = $subscription->get_user_id();
// Extend access, send renewal receipt, update CRM
do_action('my_plugin_renewal_processed', $user_id, $subscription);
}, 10, 2);
// When a renewal payment fails
add_action('woocommerce_subscription_payment_failed', function ($subscription, $last_order) {
$user_id = $subscription->get_user_id();
$retry_count = $subscription->get_failed_payment_count();
// Notify user and optionally pause access
if ($retry_count >= 2) {
$user = new WP_User($user_id);
$user->remove_role('subscriber_member');
// Send dunning email
$subscription->update_status('on-hold');
}
}, 10, 2);
// When subscription is cancelled
add_action('woocommerce_subscription_status_cancelled', function ($subscription) {
$user_id = $subscription->get_user_id();
$user = new WP_User($user_id);
$user->remove_role('subscriber_member');
});
```
3. **以编程方式查询订阅**
```php
<?php
// Get all active subscriptions for a user
function get_user_active_subscriptions(int $user_id): array {
return wcs_get_users_subscriptions($user_id, ['active']);
}
// Check if a user has an active subscription to a specific product
function user_has_active_subscription_to_product(int $user_id, int $product_id): bool {
$subscriptions = wcs_get_subscriptions_for_product($product_id, 'any', ['customer_id' => $user_id]);
foreach ($subscriptions as $subscription) {
if ($subscription->has_status('active')) {
return true;
}
}
return false;
}
// Get subscription by ID
function get_subscription_details(int $subscription_id): ?WC_Subscription {
$subscription = wcs_get_subscription($subscription_id);
if (!$subscription) return null;
return $subscription;
}
// Usage
$subscription = get_subscription_details(1234);
if ($subscription) {
echo $subscription->get_status(); // 'active', 'on-hold', 'cancelled', etc.
echo $subscription->get_next_payment_date(); // ISO 8601 date
echo $subscription->get_total(); // Current billing amount
}
```
4. **处理套餐升级和降级**
```php
<?php
// Add proration logic for plan switches
add_filter('woocommerce_subscriptions_switch_proration', function ($proration_amount, $subscription, $new_order, $product, $switch_cart_item) {
// Custom proration: charge/credit based on days remaining in billing period
$next_payment = strtotime($subscription->get_next_payment_date());
$last_payment = $subscription->get_date('last_payment');
$billing_period_days = ($next_payment - strtotime($last_payment)) / DAY_IN_SECONDS;
$days_remaining = ($next_payment - time()) / DAY_IN_SECONDS;
$old_daily_rate = (float)$subscription->get_total() / $billing_period_days;
$new_product_price = (float)$switch_cart_item['data']->get_price();
$new_daily_rate = $new_product_price / $billing_period_days;
// Credit remaining days at old rate, charge at new rate
$proration_amount = ($new_daily_rate - $old_daily_rate) * $days_remaining;
return round($proration_amount, 2);
}, 10, 5);
// Trigger a plan switch programmatically
function switch_subscription_plan(int $subscription_id, int $new_product_id): bool {
$subscription = wcs_get_subscription($subscription_id);
if (!$subscription) return false;
// Remove existing item and add new product
foreach ($subscription->get_items() as $item_id => $item) {
$subscription->remove_item($item_id);
}
$item = new WC_Order_Item_Product();
$item->set_product(wc_get_product($new_product_id));
$item->set_quantity(1);
$subscription->add_item($item);
// Recalculate totals
$subscription->calculate_totals();
$subscription->save();
return true;
}
```
5. **重试失败的支付并同步计费日期**
```php
<?php
// Trigger an immediate payment retry (e.g., from an admin action)
function retry_failed_subscription_payment(int $subscription_id): bool {
$subscription = wcs_get_subscription($subscription_id);
if (!$subscription || !$subscription->has_status('on-hold')) {
return false;
}
// Create a renewal order and attempt payment
$renewal_order = wcs_create_renewal_order($subscription);
if (is_wp_error($renewal_order)) {
return false;
}
// Process payment using the subscription's payment method
$payment_gateway = wc_get_payment_gateway_by_order($subscription);
if ($payment_gateway && method_exists($payment_gateway, 'scheduled_subscription_payment')) {
$payment_gateway->scheduled_subscription_payment(
$renewal_order->get_total(),
$renewal_order
);
}
return true;
}
// Synchronize all active subscriptions to renew on the 1st of the month
add_filter('woocommerce_subscriptions_synced_next_payment_date', function ($next_payment_date, $product, $from_timestamp, $trial_end_timestamp) {
if ($product->get_meta('_subscription_period') === 'month') {
$next_month = date('Y-m-01', strtotime('+1 month'));
return strtotime($next_month);
}
return $next_payment_date;
}, 10, 4);
```
## 示例
### 暂停和恢复流程
```php
<?php
// Admin-triggered suspension with reason logging
function suspend_subscription_with_reason(int $subscription_id, string $reason): void {
$subscription = wcs_get_subscription($subscription_id);
if (!$subscription) return;
// Store suspension reason as meta before status change
$subscription->update_meta_data('_suspension_reason', $reason);
$subscription->update_meta_data('_suspension_date', current_time('mysql'));
$subscription->save();
// Change status to on-hold (fires woocommerce_subscription_status_on-hold action)
$subscription->update_status('on-hold', sprintf('Suspended: %s', $reason));
}
// Reinstate and reset payment date
function reinstate_subscription(int $subscription_id): void {
$subscription = wcs_get_subscription($subscription_id);
if (!$subscription || !$subscription->has_status('on-hold')) return;
// Reset next payment date to avoid immediately triggering a renewal
$subscription->set_date('next_payment', strtotime('+1 month'));
// Activate subscription
$subscription->update_status('active', 'Reinstated by admin');
$subscription->save();
}
```
### 自定义订阅邮件
```php
<?php
// Add a custom "Renewal Reminder" email 7 days before renewal
add_filter('woocommerce_email_classes', function ($email_classes) {
require_once plugin_dir_path(__FILE__) . 'class-renewal-reminder-email.php';
$email_classes['My_Renewal_Reminder_Email'] = new My_Renewal_Reminder_Email();
return $email_classes;
});
// Schedule the emails via WooCommerce action scheduler
add_action('woocommerce_scheduled_subscription_payment', function ($subscription_id) {
$subscription = wcs_get_subscription($subscription_id);
if (!$subscription) return;
// Schedule reminder 7 days before next payment
$next_payment = strtotime($subscription->get_next_payment_date()) - (7 * DAY_IN_SECONDS);
if ($next_payment > time()) {
as_schedule_single_action(
$next_payment,
'my_plugin_send_renewal_reminder',
[['subscription_id' => $subscription_id]]
);
}
}, 10);
```
## 最佳实践
- **使用 Action Scheduler**(随 WooCommerce 提供)处理所有异步订阅任务——涉及支付的关键操作绝不能依赖 WP cron
- **使用 `wcs_get_subscription()` 时始终进行空值检查**——在边缘情况下(手动删除、导入失败),订阅可能已被删除或缺失
- **测试支付网关的令牌处理**——订阅续费需要已保存的支付令牌;测试网关在续费时是否正确向已存档的原卡扣款
- **明确处理支付失败后的催缴**——默认重试时间为失败后的 1、4 和 7 天;通过 `wcs_retry_rules` 过滤器自定义,以符合你的业务政策
- **记录所有订阅状态变更**——将状态变更历史保存在订单备注或自定义表中,以便审计和客户支持
- **套餐切换时遵循按比例计费**——不要强迫客户为同一时段支付两次费用;使用内置的按比例计费功能或实现自定义逻辑
- **测试升级和降级的边缘情况**——尤其是在试用期内或计费周期发生变化时(从每月变为每年)
- **使用 `wcs_user_has_subscription()` 进行访问控制**——这比仅检查用户角色更可靠
## 常见问题
| 问题 | 解决方案 |
|---------|----------|
| 续费支付未得到处理 | 检查支付网关的功能列表中是否包含 `supports[] = subscription`;网关必须明确声明支持订阅 |
| 支付成功后订阅状态仍为 "pending" | 必须触发 `woocommerce_payment_complete` 动作——确认支付网关在支付成功时调用了 `$order->payment_complete()` |
| 按比例计费产生负数扣款 | 在按比例计费过滤器的返回值中设置 `0.00` 的最低下限;负数金额会导致大多数支付处理商的网关报错 |
| `wcs_get_subscriptions_for_product` 返回空值 | 对于变体,传入产品的父级 ID——WCS 在订阅项目中存储的是父产品 ID,而非变体 ID |
| 试用结束但未扣收续费 | 确保 `_subscription_trial_length` 元数据设置为大于 0 的数字,并且初始订单的支付网关令牌已正确保存 |
| 管理员更改状态时发送了取消邮件 | 如需阻止发送邮件,在以编程方式取消之前为 `woocommerce_subscription_status_cancelled` 添加 `remove_action`,并在之后重新添加 |
## 相关 Skill
- @woocommerce-plugin-development
- @woocommerce-rest-api
- @subscription-billing
- @stripe-integration
- @woocommerce-blocks同类提示词
办公效率· n8n 工作流的对话模型
商品照片转棚拍广告生图指令
上传商品照片并提供说明文字,分析材质、配色和原图问题,写出保留商品外观的棚拍广告生图指令。
=你是一位专业产品摄影师和广告创意总监。分析上传的产品图片及其配文:{{ $('Telegram Trigger Received').item.json.message.caption }} 你的任务是创建一段详细的图像生成提示词,将这张产品照片转化为高端、商业级的广告视觉作品。 分析要求: 1. **产品识别**:识别产品类型、尺寸、类别、材质、配色和关键特征 2. **当前状态评估**:指出当前的光线、背景、构图和质量问题 3. **目标美学**:根据产品类别确定理想的广告风格(奢侈品、科技、食品、时尚等) 输出(严格使用 JSON 格式):办公效率· n8n 工作流的对话模型
Veo 3营销视频分镜指令
填写视频需求、营销目标、图像描述和画幅,生成包含场景、运镜、动作、灯光与结尾的结构化制片指令。
=# Veo3 视频生成系统提示词 你是一名专业的视频提示词工程师,擅长为 Veo3 视频生成撰写细致、富有电影感的提示词。你的角色是分析用户输入,将其转化为全面的视频生成指令,以产出高质量、引人入胜的内容。 ## 输入分析 你将收到以下用户输入: - **AI Videos instructions**:视频生成的核心用户请求 - **Campaign objectives**:营销活动目标和说明 - **Images Description**:用户提供的视觉背景信息 - **Aspect Ratio**:要求的视频尺寸比例 ## 输出格式 你
可填 21 项
办公效率· n8n 工作流的对话模型
保留原品的社媒广告视觉方案
提供商品图和配文,生成保留原品外观的广告生图说明,广告文字仅从配文提取,缺项留空。
={ "system_prompt": "你是一位顶尖创意总监,专攻爆款产品广告,拥有15年以上奢侈品牌广告活动经验。将产品图片转化为商业级广告,让浏览者停下滑动并促进转化。", "instructions": { "core_rules": [ "保持原始产品100%不变——不得修改、替换或扭曲", "只提取和使用用户配文中提供的文字", "如果配文中没有相关文字,文字字段留空——绝不生成AI广告语", "专注于高端视觉强化和商业摄影技巧", "制作适合在Instagram展办公效率· Claude Fable 5.2
用代码制作长纪录片流程
从题材调研、脚本和样帧审核开始,按章节生成三维分镜、旁白、环境声与可复现渲染。
<inputs> 向我询问: 1. 我的主题 2. 目标视频时长 3. 我是否已有脚本 如果我只给你一个主题,先研究该主题并撰写脚本。 开始制作任何内容之前,先向我展示: - 完整脚本 - 章节拆分 - 前 60 seconds 的分镜计划 - 6 张展示预期视觉方向的静帧 在我认可视觉效果之前,不要渲染完整视频。 </inputs> <story> 按照高品质 YouTube 纪录片的方式来写,不要写成 AI 科普解说。 故事必须始终向前推进。 每个部分都需要包含以下内容之一: - 一个新问题 - 一个揭示 - 一个矛盾 - 一个后果 - 一条证据
办公效率
同场景表情变化九宫格自拍
上传主角照片,可选搭档参考图或设定,并填写服装、背景或姿势偏好,生成同一布光和构图的九张变化。
# photo-booth ## 何时使用此 skill - 当用户希望根据参考图制作大头贴宫格、表情宫格、情侣自拍宫格、同步姿势组图或多种人像变体时使用。 - 当请求制作 3x3 宫格,且主体、房间、服装、构图和布光保持一致,仅表情或细微姿势发生变化时触发。 ## 必需输入 - 主要人物的主参考图。 - 可选的伴侣或角色参考图。 - 如果未提供伴侣参考图,需提供期望的伴侣设定。 - 用户指定的服装、背景、摄影时代、姿势清单或氛围。 如果缺少主参考图,请用户附上。如果伴侣设定或预期风格不明确,提出一个有针对性的问题。 ## 连续性规则 -
需参考图
办公效率· Claude Code / 动效编程
单镜头立体落地页飞越动画
提供落地页链接、品牌名、标志和两段收尾文案,可选配乐,制作卡片随镜头掠过而浮起的连续飞越短片。
<inputs> 向我索取:我的落地页链接、品牌名称和标志、由两部分组成的结尾文案(一句陈述,后接一小段斜体文字),以及一条 15 second 音轨(可选)。 </inputs> <direction> 以 60fps 对我的落地页进行 14.5 second 3D 飞越拍摄,1440x1152。页面平放在明亮的摄影棚地面上(中央为 #f9fbfb,角落为 #f2f3f6),周围有柔和宽阔的阴影,圆角为 24px。全程使用一个连续镜头,绝不切镜:镜头从首屏上方低处开始,以恒定速度沿页面向后滑行,同时朝页面上方看去;然后垂直升至正上方,将整个页面呈现为