Last active
May 7, 2024 16:15
-
-
Save Acephalia/873f1a6c13e842a8c62aa73a1b976231 to your computer and use it in GitHub Desktop.
Auto Complete Woocommerce Virtual Orders
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Auto-complete virtual orders if payment is completed by u/acephaliax | |
function auto_complete_virtual_orders($order_id) { | |
if (!$order_id) { | |
return; | |
} | |
// Get the order object | |
$order = wc_get_order($order_id); | |
// Check if the order contains virtual products | |
$contains_virtual = false; | |
foreach ($order->get_items() as $item) { | |
$product = $item->get_product(); | |
if ($product && $product->is_virtual()) { | |
$contains_virtual = true; | |
break; | |
} | |
} | |
// Check if the payment is completed | |
$payment_completed = $order->is_paid(); | |
// If the order contains virtual products and the payment is completed, auto-complete it | |
if ($contains_virtual && $payment_completed) { | |
$order->update_status('completed'); | |
} | |
} | |
add_action('woocommerce_order_status_changed', 'auto_complete_virtual_orders', 10, 3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, did you write this snippet? Are you available for modifying it? If so, please email me dax702 at gmail