1

Is there any way I can auto complete orders only for specific product IDs on Woocommerce?

I used the code on this thread to auto complete orders.

I also read this thread but it excludes product ids from auto complete. And I am not able to make it work the other way around.

Since I have 20+ products in my shop and I want to use auto complete on only 2 of them, it would be great if I can specify the order ids which I want to auto complete.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Mayank Chawla
  • 49
  • 1
  • 5

1 Answers1

1

Here is a way to autocomplete paid orders for specific product IDS:

add_action( 'woocommerce_payment_complete_order_status', 'wc_auto_complete_paid_order', 10, 3 );
function wc_auto_complete_paid_order( $status, $order_id, $order ) {
    // Below the targeted product Ids
    $targeted_ids = array(37, 53);

    // Loop through order line items
    foreach( $order->get_items() as $item ) { 
        if ( in_array( $item->get_product_id(), $targeted_ids ) || in_array( $item->get_variation_id(), $targeted_ids ) ) {
            return 'completed';
        }
    }

    return $status;
}

Code goes in functions.php file of the active child theme (or active theme).


LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Another thing I wanted to know was, if I checkout by adding 1 auto complete product and 1 non auto complete product in cart, it still marks it as complete after payment. Is there any way I can mark it to processing in that case? Really appreciate your response. – Mayank Chawla Aug 06 '19 at 21:44
  • This code is perfect in my case except but as i use woo subscriptions i need to autocomplete renewal orders but not first subscription order. So the condition does not work in that case because these two orders type contains the subscription product id. I have to add a condition if it is a renewal order or not. – pipoulito Nov 07 '22 at 11:00