【问题标题】:Get the Subscription id from the Order ID从订单 ID 中获取订阅 ID
【发布时间】:2017-08-26 07:41:29
【问题描述】:

我正在尝试从动作挂钩 woocommerce_order_status_changed 中获取 subscription id

它给了我 order id,客户每次切换时都会更改它。

例如:如果subscription id10,那么原来的order id9

现在,客户进行的每一次切换都会生成一个新的订单 ID,上面的操作会为您提供该 ID。此时我有 $customer_id$order_id 和原来的 post id,即 9,

如何获取当前订单的subscription id

谢谢

【问题讨论】:

    标签: php wordpress woocommerce orders woocommerce-subscriptions


    【解决方案1】:

    您可以使用专用函数 wcs_get_subscriptions_for_order() 来检索 $subscription ID。

    所以这可能是你的代码:

    add_action('woocommerce_order_status_changed', 'action_order_status_changed');
    function action_order_status_changed( $order_id ){
        $subscriptions_ids = wcs_get_subscriptions_for_order( $order_id, array( 'order_type' => 'any' ) );
        // We get all related subscriptions for this order
        foreach( $subscriptions_ids as $subscription_id => $subscription_obj )
            if($subscription_obj->order->id == $order_id) break; // Stop the loop
    
        // The subscription ID: $subscription_id 
        // The An instance of the Subscription object: $subscription_obj 
        // ...
    }
    

    【讨论】:

    • 给任何阅读本文的人的注意事项:我必须将函数的参数设置为 wcs_get_subscriptions_for_order( $order_id, array( 'order_type' => 'any' ) ) 才能使其工作。如果没有第二个参数,它对我不起作用(请参阅 woocommerce-subscriptions 插件中的 class-wc-subscriptions-order.php 文件中的函数 add_subscriptions_to_view_order_templates() ,它做同样的事情)。我正在使用 WC 3.3.5 和 Wordpress 4.9.4
    • @Sarah 谢谢你的来信……
    • @Sarah 感谢您的留言,它真的很有帮助。非常感谢:)
    猜你喜欢
    • 2017-08-05
    • 1970-01-01
    • 2014-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-27
    • 1970-01-01
    • 2020-05-08
    相关资源
    最近更新 更多