首页 > 解决方案 > WooCommerce 订阅:如何确定给定订阅的最后一个正确支付的订单

问题描述

是否有任何已经编程的方法来获取给定订阅的最后一个正确支付的订单?

$subscription->get_last_order()将返回最后一个关联的订单,无论该订单是否涉及正确付款。

$subscription->get_related_orders()将返回整个订单列表,该列表可以包括待付款或失败的订单。

标签: woocommerce-subscriptions

解决方案


我认为,如果您$subscription->get_last_order()使用woocommerce_subscription_payment_complete操作(https://docs.woocommerce.com/document/subscriptions/develop/action-reference/)包装/触发,您基本上可以实现该目标。该钩子会为初始订阅订单和续订订单触发,并将确保支付 $last_order 的费用。像这样的东西:

add_action( 'woocommerce_subscription_payment_complete', 'set_last_order' );
function set_last_order( $subscription ) {

    $last_order = $subscription->get_last_order( 'all', 'any' );

    // If you want to be able to reference that $last_order at any time
    // then you could just save/update that order ID to post meta so
    // that you can grab it any time outside of the action.

   }
}

我知道这看起来有点笨拙,但这是我能想到的最好方法。想到的唯一其他选择是循环$subscription->get_related_orders()检查 is_paid() 从高 ID 到低 ID,然后从那里获取第一个。


推荐阅读