woocommercewoocommerce wordpress 

WooCommerce: How to Change the order status of a particular product on purchase

In woocommerce settings by default, there is an option directly change to set the settings of particular product status on successful payment.

  1. First, find the product id you need to change the status
  2. After getting the productId, copy the below code to the functions.php file and paste it, if you are feeling difficulty in finding functions.php and paste, you can use this plugin called: My custom functions and activate and then paste it inside the textbox, save and then turn it on. and then just replace the ‘your product id’ in the below code. in the below, I am trying to change the status of ‘processing’ to ‘completed’.
function status_auto_complete_by_payment_method($order_id){
    if ( ! $order_id ) {
    	return;
    }
    global $product;
    $order = wc_get_order( $order_id );
    
    $items = $order->get_items();
    foreach ($items as $item) {
        $product_id = $item['product_id'];
        if ($product_id == 1234) { //replace your product id here
            if ($order->data['status'] == 'processing') {
                $payment_method=$order->get_payment_method();
                if ($payment_method!="cod"){
                    $order->update_status( 'completed' );
                }
            }
        }
    }    
}
add_action('woocommerce_order_status_changed', 'status_auto_complete_by_payment_method');

Feel free to connect me if you have any queries

Also Read:  how to change the wordpress wp-admin url pathname

Related posts