WooCommerce 中特定产品的自动更改订单状态以完成

Auto change order status to completed for specific products in WooCommerce

我有一个虚拟产品,我希望它在付款完成后更改状态。 以下代码将所有购买更改为“已完成”,但我只想更改我的一个产品,而不是所有产品。 我的产品是可变产品,有 4 个项目,

add_action( 'woocommerce_thankyou', '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->has_status( 'processing' ) ) {
            $order->update_status( 'completed' );
        }
    }

我搜索了很多但没有找到答案。请帮我。谢谢

您可以从订单项目中定位特定产品 ID,以使您的代码仅适用于它们,如下所示:

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

    $order = wc_get_order( $order_id );
    
    $product_ids   = array('23'); // Here set your targeted product(s) Id(s)
    $product_found = false;
    
    // Loop through order items
    foreach ( $order->get_items() as $item ) {
        if( array_intersect( $product_ids, array($item->get_product_id(), $item->get_variation_id()) ) ) {
            $product_found = true;
            break;
        }
    }

    if( $order->has_status( 'processing' ) && $product_found ) {
        $order->update_status( 'completed' );
    }
}

如果您想专门定位这些产品,那么您将改用以下内容:

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

    $order = wc_get_order( $order_id );
    
    $product_ids   = array('23'); // Here set your targeted product(s) Id(s)
    $product_found = true;
    
    // Loop through order items
    foreach ( $order->get_items() as $item ) {
        if( ! array_intersect( $product_ids, array($item->get_product_id(), $item->get_variation_id()) ) ) {
            $product_found = false;
            break;
        }
    }

    if( $order->has_status( 'processing' ) && $product_found ) {
        $order->update_status( 'completed' );
    }
}

代码进入活动子主题(或活动主题)的 functions.php 文件。它应该有效。


Addition: Avoid multiple notifications

You should better use woocommerce_payment_complete_order_status hook instead like in answer, to avoid multiple notifications.

So the code is going to be:

add_action( 'woocommerce_payment_complete_order_status', 'wc_auto_complete_paid_order', 10, 3 );
function wc_auto_complete_paid_order( $status, $order_id, $order ) {
    $product_ids   = array('23'); // Here set your targeted product(s) Id(s)
    $product_found = false;
    
    // Loop through order items
    foreach ( $order->get_items() as $item ) {
        if( array_intersect( $product_ids, array($item->get_product_id(), $item->get_variation_id()) ) ) {
            $product_found = true;
            break;
        }
    }

    return $product_found ? 'completed' : $status;
}

Or targeting products exclusively:

add_action( 'woocommerce_payment_complete_order_status', 'wc_auto_complete_paid_order', 10, 3 );
function wc_auto_complete_paid_order( $status, $order_id, $order ) {
    $product_ids   = array('23'); // Here set your targeted product(s) Id(s)
    $product_found = true;
    
    // Loop through order items
    foreach ( $order->get_items() as $item ) {
        if( ! array_intersect( $product_ids, array($item->get_product_id(), $item->get_variation_id()) ) ) {
            $product_found = false;
            break;
        }
    }

    return $product_found ? 'completed' : $status;
}

It should work...