2

I'm using the Woocommerce Subscriptions plugin, and in the processing order renewal email I wanted to show when the next payment date will be. However when I try to access the next payment date for an order it always returns false.

<?php
    if (WC_Subscriptions_Renewal_Order::is_renewal( $order ) ) { /* This returns true */
        $parent_id = WC_Subscriptions_Renewal_Order::get_parent_order_id( $order ); /* This gets the original parent order id */
        $parent_order = new WC_Order( $parent_id );
        foreach ($parent_order->get_items() as $item) { /* This loops through each item in the order */
            $h = WC_Subscriptions_Order::get_next_payment_date ( $parent_order, $item['product_id'] ); /* This should get the next payment date... */
            var_dump($h); /* But it returns false :-( */
        }
    }
?>

Other functions such as WC_Subscriptions_Order::get_total_initial_payment() work as expected.

How can I get the next payment date for a subscription order?

MrCarrot
  • 2,546
  • 1
  • 23
  • 29
  • The first parameter of `get_next_payment_date()` method is a `$subscription_key` which appears to be retrieved from `get_subscription_key()` method. But I'm also looking at Subs 2.0 beta code, so I'm not sure. As a heads up, `get_next_payment_date()` method is deprecated as of 2.0. – helgatheviking Jul 28 '15 at 13:03

2 Answers2

6

I figured it out - looks like I was working an order where the subscription had been cancelled, hence get_next_payment_date() returning false.

Here is my solution if it helps anyone else trying to do a similar thing:

add_action( 'woocommerce_email_order_meta', 'my_email_next_payment_date', 10, 1 );

function my_email_next_payment_date( $order ) {
    if ( ! class_exists( 'WC_Subscriptions') ) {
        return;
    }
    if ( WC_Subscriptions_Renewal_Order::is_renewal( $order ) ) {
        $parent_id = WC_Subscriptions_Renewal_Order::get_parent_order_id( $order ); /* This gets the original parent order id */
        $parent_order = new WC_Order( $parent_id );
        foreach ( $parent_order->get_items() as $item ) { /* This loops through each item in the order */
            $date = WC_Subscriptions_Order::get_next_payment_date ( $parent_order, $item['product_id'] ); /* This should get the next payment date... */
            if ( $date ) {
                echo '<p style="margin: 16px 0 8px; text-align: left;">Your next payment is due on <strong>' . date( 'l jS F Y', strtotime( $date ) ) . '</strong></p>';
            }
        }
    } elseif ( WC_Subscriptions_Order::order_contains_subscription( $order ) ) {
        foreach ( $order->get_items() as $item ) { /* This loops through each item in the order */
            $date = WC_Subscriptions_Order::get_next_payment_date ( $order, $item['product_id'] ); /* This should get the next payment date... */
            if ( $date ) {
                echo '<p style="margin: 16px 0 8px; text-align: left;">Your next payment is due on <strong>' . date( 'l jS F Y', strtotime( $date ) ) . '</strong></p>';
            }
        }
    }
}
MrCarrot
  • 2,546
  • 1
  • 23
  • 29
1

You can also get next payment date for a subscription using below code

<?php

 $subscription = new WC_Subscription(1);
 $nextPayment = $subscription->get_time('next_payment');
 $dt = new DateTime("@$nextPayment");
 echo $dt->format('Y-m-d H:i:s');
NS23
  • 684
  • 5
  • 14