Woocommerce Subscriptions display next payment date

两盒软妹~` 提交于 2019-12-07 20:19:24

问题


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?


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


来源:https://stackoverflow.com/questions/31664987/woocommerce-subscriptions-display-next-payment-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!