Woocommerce Subscriptions display next payment date

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.

Read More

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

Related posts

2 comments

  1. 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>';
                }
            }
        }
    }
    
  2. 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');
    

Comments are closed.