Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 自动将电子商务订阅状态更改为";“暂停”;而不是",;主动的;_Php_Wordpress_Woocommerce_Orders_Woocommerce Subscriptions - Fatal编程技术网

Php 自动将电子商务订阅状态更改为";“暂停”;而不是",;主动的;

Php 自动将电子商务订阅状态更改为";“暂停”;而不是",;主动的;,php,wordpress,woocommerce,orders,woocommerce-subscriptions,Php,Wordpress,Woocommerce,Orders,Woocommerce Subscriptions,在Woocommerce中,我希望在订单仍处于“处理”状态时,自动将所有Woocommerce订阅置于“暂停”状态,而不是“活动”状态。一旦我将订单标记为“已完成”,订阅应更改为“活动” 我已经尝试了我能想到的一切,如果有人知道怎么做,请告诉我 我正在运行wordpress 4.8.1/Woocommerce 3.1.2/Woocommerce订阅2.2.7/并且支付网关是Stripe 3.2.3。这可以通过两个步骤完成: 1) 通过woocommerce\u thankyou操作挂钩中的自定义

在Woocommerce中,我希望在订单仍处于“处理”状态时,自动将所有Woocommerce订阅置于“暂停”状态,而不是“活动”状态。一旦我将订单标记为“已完成”,订阅应更改为“活动”

我已经尝试了我能想到的一切,如果有人知道怎么做,请告诉我


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

这可以通过两个步骤完成:

1) 通过
woocommerce\u thankyou
操作挂钩中的自定义函数,当订单处于“处理”状态且包含订阅时,我们将订阅状态更新为'on-hold'

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' );
        }
    }
}
// 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文件或任何插件文件中

2) 通过在
woocommerce\u order\u status\u completed
操作挂钩中挂接的自定义功能,当订单状态更改为“completed”时,它将自动将订阅状态更改为“active”

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' );
        }
    }
}
// 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+上进行了测试,可以正常工作。

我已经阅读了有关Woocommerce订阅状态暂停的教程。我想这会对你有帮助