我对 WooCommerce 订阅的结束日期有疑问。我在这里找到了这个话题 Woocommerce Subscription change expiration date
然而,我们这里的案例有点不同,但这个话题很有帮助。
我们使用按比例分配(每月 10 日)设置以便同时创建续订订单(每月)。这工作正常。
我们还使用 WooCommerce 成员(member)资格,以便客户可以访问特定的成员(member)内容。 WooCommerce 订阅会根据上次付款日期自动设置结束日期。我们有一个具有不同持续时间的订阅变体产品。 (3、6 和 12 个月)
有了这个,我们有以下问题:
当客户今天在 04/03/2021 (d/m/y) 订购订阅时,系统将创建一个订阅,第一笔付款为 10/03/2021。在这种情况下,结束日期将是 10/06/2021。 (10/03/2021 + 3 个月)
但我们希望结束日期是 04/06/2021(开始日期 +3 个月)而不是 10/06/2021。
为此,我们尝试根据开始日期(订单日期)和特定订阅版本(3. 6 或 12 个月)计算订阅结束日期。
示例:
=> 结束日期应该是 04/06/2021 而不是 10/06/2021
其他示例:
=> 结束日期应该是 20/06/2021 而不是 10/07/2021
当前方法
我们尝试在这里使用这些 Action 钩子(Hook):https://docs.woocommerce.com/document/subscriptions/develop/action-reference/
在这种当前状态下,结束日期不会改变。我现在不确定是什么问题。我认为我以正确的方式访问结束日期,但在订阅创建后它没有改变。
add_action( 'woocommerce_checkout_subscription_created', 'recalc_subscription_end_date' );
function recalc_subscription_end_date ($order, $subscription) {
if ( function_exists( 'wcs_order_contains_subscription' ) ) {
if ( wcs_order_contains_subscription( $order->ID ) ) {
// Set subscription end date based on subscription variation
$end_date = date('Y-m-d H:i:s',strtotime("+12 months", strtotime($start_date)));
$dates_to_update = array();
$dates_to_update['end'] = $end_date;
$subscription->update_dates($dates_to_update);
$subscription->update_dates( array(
'end' => $end_date,
) );
$subscription->save();
}
$return;
}
}
更新:使用来自@LoicTheAztec 的新信息,我在日志中收到以下错误。
目前,+12 个月的计算对于每个变体都是硬编码的。我如何才能访问所有 3 个变体的数组?
我们的变体 ID : 11903 3 个月,11904 6 个月,11905 12 个月,
[04-Mar-2021 16:43:10 UTC] PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to function recalc_subscription_end_date(), 1 passed in /home/testsystem/www/test887854.ch/cbctest/wp-includes/class-wp-hook.php on line 289 and exactly 2 expected in /home/testsystem/www/test887854.ch/cbctest/wp-content/plugins/code-snippets/php/snippet-ops.php(446) : eval()'d code:2
Stack trace:
#0 /home/testsystem/www/test887854.ch/cbctest/wp-includes/class-wp-hook.php(289): recalc_subscription_end_date(Object(WC_Subscription))
#1 /home/testsystem/www/test887854.ch/cbctest/wp-includes/class-wp-hook.php(311): WP_Hook->apply_filters(NULL, Array)
#2 /home/testsystem/www/test887854.ch/cbctest/wp-includes/plugin.php(478): WP_Hook->do_action(Array)
#3 /home/testsystem/www/test887854.ch/cbctest/wp-content/plugins/woocommerce-subscriptions/includes/class-wc-subscriptions-checkout.php(103): do_action('woocommerce_che...', Object(WC_Subscription), Object(Automattic\WooCommerce\Admin\Overrides\Order), Object(WC_Cart))
最佳答案
更新 - 变量 $start_date
未在您的代码中定义,并且还有其他错误。
尝试以下(未经测试):
add_action( 'woocommerce_checkout_subscription_created', 'recalc_subscription_end_date' );
function recalc_subscription_end_date ($order, $subscription) {
if ( function_exists( 'wcs_order_contains_subscription' ) && wcs_order_contains_subscription( $order ) ) {
// The array of subscriptions dates
$date_types = array('start', 'trial_end', 'next_payment', 'last_payment', 'end');
$dates = array(); // Initializing
foreach( $suscription_date_types as $date_type ) {
$dates[$date_type] = $subscription->get_date($date_type);
}
// Set subscription end date based on subscription variation
$dates['end'] = date( 'Y-m-d H:i:s',strtotime( "+12 months", strtotime( $dates['start'] ) ) );
$subscription->update_dates($dates);
$subscription->save();
}
}
代码位于事件子主题(或事件主题)的 functions.php 文件中。它可以工作。
关于php - 更改 WooCommerce 订阅结束日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66470641/