下单后设置下一个续订付款日期的最可靠挂钩

Most reliable hook for setting next renewal payment date after order

我们在 Woocommerce 订阅设置中遇到默认同步选项不起作用的情况。我已经创建了一个手动日历,我的 functions.php 文件在创建订阅时会引用它,但它只会在创建订阅时进行检查,然后插件会自动将下一次续订设置为此后每 4 周一次。

我需要在每次订阅创建 and/or 续订后启动我的续订日期检查器功能,以根据我的日历计算下一个续订日期。我知道我至少可以尝试两个钩子,它们是: wcs_renewal_order_createdwoocommerce_subscription_payment_complete,但我不太了解何时触发它们,无法知道哪个最安全。

关于 woocommerce_subscription_payment_complete,WC 订阅文档说:

Triggered when a payment is made on a subscription. This can be payment for the initial order or a renewal order. It is important to note, however, that subscription date changes related to early renewals are processed differently than other renewals, so this action should not be relied upon for capturing the next payment date on a subscription.

我不是 100% 确定他们在笔记中提到的是什么情况,但这足以让我停下来。

关于 wcs_renewal_order_created,我不清楚什么时候会触发:

These orders are always created through the wcs_create_renewal_order() function, regardless of whether they are created for a scheduled renewal event, manually via the WooCommerce > Edit Subscription administration screen, or via the Subscriptions endpoints for the WooCommerce REST API. Because of this, it’s possible to add, remove or update the value of anything on that renewal order using this filter.

For example, this can be used to add a discount to specific renewal orders, like the 12th order each year. It could also be used to add one-time fee for a certain renewal order, like a special annual extra fee on a monthly subscription.

在此挂钩期间尝试通过 update_dates() 设置 next_date 密钥是否会推送它即将处理或已经发生的当前付款,因此现在可以安全地设置下一个日期?

以编程方式设置下一个续订日期的最可靠方法是 为给定订阅处理完当前期限后立即?

免责声明:我分享我的推理和初步信息,我没有做任何实际检查。

根据描述,两个挂钩都与所需的事件无关:wcs_renewal_order_created 响应订阅订单的创建,woocommerce_subscription_payment_complete 响应此类订单的付款。但是这两点都负责创建或更新订阅的“簿记原理”,而不是创建或更新本身。

我建议测试 woocommerce_helper_subscription_activate_success 钩子,它可以在 class-wc-helper.php:

中找到
/**
 * Active a product subscription.
 */
private static function _helper_subscription_activate() {

    // ...

    if ( $activated ) {
        /**
         * Fires when the Helper activates a product successfully.
         *
         * @param int    $product_id Product ID being activated.
         * @param string $product_key Subscription product key.
         * @param array  $activation_response The response object from wp_safe_remote_request().
         */
        do_action( 'woocommerce_helper_subscription_activate_success', $product_id, $product_key, $activation_response );
    } else {

    // ...

这个钩子好像是响应订阅成功的那一刻。并且有可能这不仅意味着订阅的初始激活时刻,还意味着其后续续订。

这可以通过以下代码完成,例如:

function test_woocommerce_helper_subscription_activate_success( $product_id, $product_key, $activation_response ) { 
   // your code here
} 
add_action('woocommerce_helper_subscription_activate_success', 'test_woocommerce_helper_subscription_activate_success', 10, 3);