MrCarrot
MrCarrot

Reputation: 2778

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.

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

Upvotes: 2

Views: 10109

Answers (2)

NS23
NS23

Reputation: 794

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');

Upvotes: 1

MrCarrot
MrCarrot

Reputation: 2778

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>';
            }
        }
    }
}

Upvotes: 6

Related Questions