WooCommerce订阅:如何确定给定订阅的最后正确付款的订单

时间:2019-09-10 09:03:03

标签: woocommerce-subscriptions

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

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

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

1 个答案:

答案 0 :(得分:2)

我认为,如果您用db.collection.aggregate([ { $group: { "_id": { user_id: "$user_id" }, requestA_count: { $sum: { $cond: [ { $and: [ { $ne: [{$type: "$requestA"}, "missing"] }, { $eq: [{$type: "$requestB"}, "missing"] }, { $eq: [{$type: "$requestC"}, "missing"] } ] }, // you need to close bracket here not below 1, 0 ] } }, requestB_count: { $sum: { $cond: [{ $ne: [{ $type: "$requestB" }, "missing"] }, 1, 0] } }, requestC_count: { $sum: { $cond: [{ $ne: [ { $type: "$requestC" }, "missing" ] }, 1, 0] } } } }, { $project: { _id: 0, user_id: "$_id.user_id", requestA_count: 1, requestB_count: 1, requestC_count: 1 } } ]) 动作(https://docs.woocommerce.com/document/subscriptions/develop/action-reference/)包装/触发$subscription->get_last_order(),则基本上可以实现该目标。该挂钩同时触发初始订阅订单和续订订单,并确保为$ last_order付款。像这样:

woocommerce_subscription_payment_complete

我知道这似乎有些笨拙,但这是我能想到的最好方法。想到的唯一其他选择是通过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. } } 循环检查is_paid()从高ID到低ID并从那里获取第一个ID。