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

时间:2021-01-02 21:28:07

标签: php wordpress woocommerce hook-woocommerce orders

我有一个虚拟产品,我希望它在付款完成后改变状态。 以下代码将所有购买更改为“已完成”,但我只想更改我的一种产品,而不是所有产品。 我的产品是可变产品,有 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' );
        }
    }

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

1 个答案:

答案 0 :(得分:2)

您可以从订单商品中定位特定产品 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 文件中。它应该有效。


<块引用>

附加:避免多次通知

您最好像在 WooCommerce: Auto complete paid orders 答案中那样使用 woocommerce_payment_complete_order_status 钩子,以避免多次通知。

所以代码将是:

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;
}

专门定位产品:

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;
}

它应该可以工作...