Skip to content

Instantly share code, notes, and snippets.

@robindevitt
Last active February 28, 2020 14:27
Show Gist options
  • Save robindevitt/64219a232379ddacf8b6be6c0980e5ce to your computer and use it in GitHub Desktop.
Save robindevitt/64219a232379ddacf8b6be6c0980e5ce to your computer and use it in GitHub Desktop.
Auto Complete WooCommerce orders when the order contains a specific product ID.
/**
* Auto Complete WooCommerce orders when the order contains a specific product ID.
*/
add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_order' );
function custom_woocommerce_auto_complete_order( $order_id ) {
if ( ! $order_id ) {
return;
}
// add your product ID's to this array
$product_ids = array( 3, 1 );
// setup product array for intersect
$product_array = array();
$order = wc_get_order( $order_id );
$order_items = $order->get_items();
// store product id's to array
foreach ( $order_items as $item => $value) {
$product_array[] = $value[ 'product_id' ];
}
if ( array_intersect( $product_array, $product_ids ) ) {
$order->update_status( 'completed' );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment