Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save groundcat/69e31cdd7c5c646c351601b0b0fcc413 to your computer and use it in GitHub Desktop.
Save groundcat/69e31cdd7c5c646c351601b0b0fcc413 to your computer and use it in GitHub Desktop.
Automatically mark WooCommerce orders complete, only if all the products in the order is downloadable.
/**
* Auto Complete all WooCommerce orders with only downloadable products.
* If all the products are downloadable, the order status will be updated to 'completed'.
* Use this plugin to implement the function:
* https://github.com/woocommerce/theme-customisations
*/
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 );
$items = $order->get_items();
// Check if all products in the order are downloadable
$all_downloadable = true;
foreach ( $items as $item ) {
$product = $item->get_product();
if ( ! $product->is_downloadable() ) {
$all_downloadable = false;
break;
}
}
// If all products are downloadable, mark the order as completed
if ( $all_downloadable ) {
$order->update_status( 'completed' );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment