Woocommerce订阅显示下一个付款日期

时间:2015-07-27 23:23:39

标签: php wordpress woocommerce subscriptions

我正在使用Woocommerce订阅插件,并在处理订单续订电子邮件中,我想在下一个付款日期时显示。但是,当我尝试访问订单的下一个付款日期时,它总是返回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 :-( */
        }
    }
?>

WC_Subscriptions_Order::get_total_initial_payment()等其他功能按预期工作。

如何获得订阅订单的下一个付款日期?

2 个答案:

答案 0 :(得分:4)

我想通了 - 看起来我正在处理订阅已被取消的订单,因此get_next_payment_date()返回false。

这是我的解决方案,如果它可以帮助其他人尝试做类似的事情:

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

答案 1 :(得分:0)

感谢您的提示,但是有一种更简单的方法,因为您无需检查这是否是续订订单。新订单包含一个订阅标识号,您可以使用它来执行任何工作。

 $order                = new WC_Order( $order_id );                  
 $OrderNumber          = $order->parent_id;
 $ParentOrder          = new WC_Order( $OrderNumber );
 $SubscriptionNumber   = $order->get_order_number();
 $PaymentDate          = $order->get_date_created()->format ('Y-m-d');    <=== new / updated date.