繁体   English   中英

自动将 Woocommerce 订阅状态​​更改为“暂停”而不是“活动”

[英]Auto change Woocommerce Subscriptions status to “On-Hold” rather than “Active”

在 Woocommerce 中,当订单仍在“处理”时,我想自动将所有 Woocommerce 订阅“搁置”而不是“活动”。 一旦我将订单标记为“已完成”,该订阅应更改为“活动”。

我已经尝试了所有我能想到的方法,如果有人知道如何做到这一点,请告诉我。

我正在运行 wordpress 4.8.1 / Woocommerce 3.1.2 / Woocommerce Subscriptions 2.2.7 / 并且支付网关是 Stripe 3.2.3。

这可以分两步完成:

1) 使用挂在woocommerce_thankyou操作挂钩中的自定义函数,当订单具有“正在处理”状态并包含订阅时,我们将订阅状态更新“暂停”

add_action( 'woocommerce_thankyou', 'custom_thankyou_subscription_action', 50, 1 );
function custom_thankyou_subscription_action( $order_id ){
    if( ! $order_id ) return;

    $order = wc_get_order( $order_id ); // Get an instance of the WC_Order object

    // If the order has a 'processing' status and contains a subscription 
    if( wcs_order_contains_subscription( $order ) && $order->has_status( 'processing' ) ){

        // Get an array of WC_Subscription objects
        $subscriptions = wcs_get_subscriptions_for_order( $order_id );
        foreach( $subscriptions as $subscription_id => $subscription ){
            // Change the status of the WC_Subscription object
            $subscription->update_status( 'on-hold' );
        }
    }
}

代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中。

2) 在woocommerce_order_status_completed动作钩子中挂接一个自定义函数,当订单状态变为“已完成”时,它会自动将订阅状态变为“活动”

// When Order is "completed" auto-change the status of the WC_Subscription object to 'on-hold'
add_action('woocommerce_order_status_completed','updating_order_status_completed_with_subscription');
function updating_order_status_completed_with_subscription($order_id) {
    $order = wc_get_order($order_id);  // Get an instance of the WC_Order object

    if( wcs_order_contains_subscription( $order ) ){

        // Get an array of WC_Subscription objects
        $subscriptions = wcs_get_subscriptions_for_order( $order_id );
        foreach( $subscriptions as $subscription_id => $subscription ){
            // Change the status of the WC_Subscription object
            $subscription->update_status( 'active' );
        }
    }
}

代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中。

所有代码都在 Woocommerce 3+ 上进行了测试并且可以正常工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM