woocommerce-subscriptions - WooCommerce 订阅:如何确定给定订阅的最后一个正确支付的订单
问题描述
是否有任何已经编程的方法来获取给定订阅的最后一个正确支付的订单?
$subscription->get_last_order()
将返回最后一个关联的订单,无论该订单是否涉及正确付款。
$subscription->get_related_orders()
将返回整个订单列表,该列表可以包括待付款或失败的订单。
解决方案
我认为,如果您$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,然后从那里获取第一个。
推荐阅读
- html - 如何从字符串中删除特定的 Img 标签
- akka-stream - Akka Streams 在异步边界记录
- api - 如何使用 Vue 3 Composition API 使用 Axios 通过 Id 获取项目
- c - 在C中全局初始化互斥锁
- c# - 出于某种原因,我的僵尸试图去 0 0 0 而不是玩家,我不知道为什么
- aws-iot - 如何有效地配置 AWS IoT 规则以将数据写入设备特定的 Timestream 数据库?
- azure-devops - 是否可以重命名发行版?
- swift - 为什么 UICollectionView SafeArea.Top 约束被 UIView/ScrollView 撞倒?
- c# - 如何在运行时更新中仅在需要时更新列表?
- python - 访问嵌套列