根据订单 ID WooCommerce 将订阅状态更改为活动

时间:2021-04-22 22:07:49

标签: php wordpress woocommerce hook-woocommerce woocommerce-subscriptions

首先,感谢帮助我编写脚本的人,该脚本将订单状态更改为已完成(脚本如下)。

不幸的是,它不会触发订阅进入活动状态。当我在 WooCommerce 中手动更改订单状态时。所以我的想法是根据给定的 order_id 激活订阅。

如何在 WooCommerce 中根据 order_id 将订阅状态从“待定”更改为“活动”?

这是一个代码,当我使用 100% 优惠券时,它会更改订单状态并且它有效:在 functions.php 中

<?php

add_action('woocommerce_checkout_order_processed', 'custom_woocommerce_auto_complete_order');
function custom_woocommerce_auto_complete_order($order_id)
{
    if (!$order_id) {
        return;
    }

      $order = wc_get_order($order_id);
    if ($order->get_total() == 0) {
        $order->update_status('processing');
        $order->update_status('completed');
    }
}
 

我发现像这样可以激活这些订阅,但我无法实现,也许有人可以帮我解决这个问题? 这是我在github上找到的代码

/**
     * Activates all the subscriptions created by a given order.
     *
     * @param WC_Order|int $order The order or ID of the order for which subscriptions should be marked as activated.
     * @since 1.0
     */
    public static function activate_subscriptions_for_order( $order ) {

        $subscriptions = wcs_get_subscriptions_for_order( $order );

        if ( ! empty( $subscriptions ) ) {

            foreach ( $subscriptions as $subscription ) {

                try {
                    $subscription->update_status( 'active' );
                } catch ( Exception $e ) {
                    // translators: $1: order number, $2: error message
                    $subscription->add_order_note( sprintf( __( 'Failed to activate subscription status for order #%1$s: %2$s', 'woocommerce-subscriptions' ), is_object( $order ) ? $order->get_order_number() : $order, $e->getMessage() ) );
                }
            }

            do_action( 'subscriptions_activated_for_order', $order );
        }
    } 

我试过了,但没有用:

add_action('woocommerce_checkout_order_processed', 'custom_woocommerce_auto_complete_order');
function custom_woocommerce_auto_complete_order($order_id)
{
    if (!$order_id) {
        return;
    }

      $order = wc_get_order($order_id);
    if ($order->get_total() == 0) {
        $order->update_status('processing');
        $order->update_status('completed');
    }
}


do_action('woocommerce_checkout_order_processed', 'subscriptions_activated_for_order', $order_id );
function activate_subscriptions_for_order( $order_id ) {

  $subscriptions = wcs_get_subscriptions_for_order( $order_id );

  if ( ! empty( $subscriptions ) ) {

    foreach ( $subscriptions as $subscription ) {

      try {
        $subscription->update_status( 'active' );
      } catch ( Exception $e ) {
        // translators: $1: order number, $2: error message
        $subscription->add_order_note( sprintf( __( 'Failed to activate subscription status for order #%1$s: %2$s', 'woocommerce-subscriptions' ), is_object( $order_id ) ? $order->get_order_number() : $order_id, $e->getMessage() ) );
      }
    }

  }
}

最好的问候

1 个答案:

答案 0 :(得分:2)

您的代码中存在错误和错误。您可以尝试将所有内容合并在一起,例如:

add_action('woocommerce_checkout_order_processed', 'custom_woocommerce_auto_complete_order');
function custom_woocommerce_auto_complete_order($order_id)
{
    if (!$order_id) {
        return;
    }

    $order = wc_get_order($order_id);

    if ($order->get_total() == 0) {
        // $order->update_status('processing'); // Unneeded as you complete the order
        $order->update_status('completed');
    }

    $subscriptions = wcs_get_subscriptions_for_order( $order );
    
    if ( ! empty( $subscriptions ) ) {
        activate_subscriptions_for_order( $order );
    }
}

代码位于活动子主题(或活动主题)的functions.php 文件中。它可以工作。


或者你可以保留:

add_action('woocommerce_checkout_order_processed', 'custom_woocommerce_auto_complete_order');
function custom_woocommerce_auto_complete_order($order_id)
{
    if (!$order_id) {
        return;
    }

    $order = wc_get_order($order_id);

    if ($order->get_total() == 0) {
        // $order->update_status('processing'); // Unneeded as you complete the order
        $order->update_status('completed');
    }
}

并使用在订单状态为“完成”时触发的钩子 woocommerce_order_status_completed 如下激活与订单相关的订阅:

add_action( 'woocommerce_order_status_completed', 'order_complete_activate_subscription', 10, 2 );
function order_complete_activate_subscription( $order_id, $order ) {
    $subscriptions = wcs_get_subscriptions_for_order( $order );
    
    if ( ! empty( $subscriptions ) ) {
        activate_subscriptions_for_order( $order );
    }
}

代码位于活动子主题(或活动主题)的functions.php 文件中。它可以工作。