Skip to content

Instantly share code, notes, and snippets.

@JoelEadeDesign
Created June 3, 2022 00:57
Show Gist options
  • Save JoelEadeDesign/aa3f63f92949f1241177c65280ed519f to your computer and use it in GitHub Desktop.
Save JoelEadeDesign/aa3f63f92949f1241177c65280ed519f to your computer and use it in GitHub Desktop.
Function to control WooCommerce Order Status by User Role
<?php
/* Source: https://quadlayers.com/change-order-status-automatically-in-woocommerce/ */
function user_role_order_status( $order_id ) {
if ( ! $order_id ) { return; }
$order = wc_get_order( $order_id );
// Change db prefix `wp_` of `wp_capabilities`, if required
$usermeta = get_user_meta( get_current_user_id(),'wp_capabilities', true );
// Enter the user role name to apply this function to
if ( key( $usermeta ) === 'user_role_name_goes_here' ) {
// Check for order status i.e. cancelled, failed, on-hold, pending, processing, completed
if( 'processing'== $order->get_status() ) {
// Change order status i.e. cancelled, failed, on-hold, pending, processing, completed
$order->update_status( 'completed' );
}
}
}
add_action( 'woocommerce_thankyou', 'user_role_order_status', 10, 1 );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment