Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Next Payment date in Woocommerce Subscriptions?

I would like to change the next payment date of active subscriptions after the Initial (first) order was created.

current code which does not work. Why? What am i missing?

//fires when new order payment is complete (NOT RENEWAL but original order!)
add_action('woocommerce_payment_complete', 'fm_upate_next_payment_datetime');

function fm_upate_next_payment_datetime ( $order_id ) {

if (WC_Subscriptions_Order::order_contains_subscription( $order_id ) == false) return;      
$subscription_key = WC_Subscriptions_Manager::get_subscription_key( $order_id, $product_id
= '');

 //hard code next payment date...(must be in future)
$next_payment = date( 'Y-m-d H:i:s', '2016-05-20 00:10:10' );

//set the new payment date  
WC_Subscriptions_Manager::set_next_payment_date( $subscription_key, $user_id = '', $next_payment );

}
like image 937
SgtPepperAut Avatar asked May 06 '16 09:05

SgtPepperAut


People also ask

Can WooCommerce do recurring payments?

With WooCommerce Subscriptions, you can create and manage products with recurring payments — payments that will give you residual revenue you can track and count on. WooCommerce Subscriptions allows you to introduce a variety of subscriptions for physical or virtual products and services.

What is the difference between WooCommerce subscriptions and memberships?

The main difference between WooCommerce subscriptions and memberships is that WooCommerce subscriptions are sold as a product, while memberships are a service. This means that WooCommerce subscriptions can be sold as an add-on to other products or services, and memberships can be sold as a standalone product.

Can WooCommerce handle subscriptions?

WooCommerce Subscriptions by WooCommerceWith WooCommerce Subscriptions, you can create and manage products with recurring payments. This implies that these payments will give the residual revenue that you can track later. Moreover, you can also introduce variety of subscriptions for physical and virtual products.


2 Answers

what you said and also we need to set a start date! (can be NOW) but if its not passed it will throw an error. Got it to work like this:

function saveNewDate($post_id) {

    $subscription = wcs_get_subscription( $post_id );
    $dates        = array();

    $datetime           = current_time( 'timestamp', true );
    $dates[ 'start' ] = date( 'Y-m-d H:i:s', $datetime );
    $datetime = strtotime( "08/23/2016 23:30" );
    $dates['next_payment'] = date( 'Y-m-d H:i:s', $datetime );

    try {
        $subscription->update_dates( $dates, 'gmt' );
        wp_cache_delete( $post_id, 'posts' );
    } catch ( Exception $e ) {
        wcs_add_admin_notice( $e->getMessage(), 'error' );
    }
}
like image 60
SgtPepperAut Avatar answered Sep 28 '22 19:09

SgtPepperAut


working solution for me is (see also https://docs.woocommerce.com/document/subscriptions/develop/functions/#section-4):

function update_next_payment_date($subscription) {
    $date = new DateTime('2021-05-28');
    $date->setTime(8, 55);

    $subscription_next_payment_date = date_format($date, 'Y-m-d H:i:s'); // = '2021-05-28 08:55:00'

    $new_dates = array(
        'start' => $subscription->get_date('start'),
        'trial_end' => $subscription->get_date('trial_end'),
        'next_payment' => $subscription_next_payment_date,
        'last_payment' => $subscription->get_date('last_payment'),
        'end' => $subscription->get_date('end'),
    );

    $subscription->update_dates($new_dates);
}
like image 44
vorwerg-ni Avatar answered Sep 28 '22 17:09

vorwerg-ni