Woocommerce 订阅显示下一个付款日期 [英] Woocommerce Subscriptions display next payment date

查看:37
本文介绍了Woocommerce 订阅显示下一个付款日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Woocommerce 订阅插件,并且在处理订单续订电子邮件中,我想显示下一个付款日期的时间.但是,当我尝试访问订单的下一个付款日期时,它总是返回 false.

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 :-( */
        }
    }
?>

WC_Subscriptions_Order::get_total_initial_payment() 等其他函数按预期工作.

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?

推荐答案

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

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

这篇关于Woocommerce 订阅显示下一个付款日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆