Created
April 22, 2025 09:15
-
-
Save shameemreza/a50d2c1b1354305ae4ff63e3b0fd5930 to your computer and use it in GitHub Desktop.
Automatically delete products when stock hits zero, but only after the order is completed.
This file contains hidden or 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
add_action( 'woocommerce_order_status_completed', function( $order_id ) { | |
$order = wc_get_order( $order_id ); | |
if ( ! $order ) { | |
return; | |
} | |
foreach ( $order->get_items() as $item ) { | |
$product = $item->get_product(); | |
if ( | |
$product instanceof WC_Product && | |
$product->managing_stock() && | |
$product->get_stock_quantity() <= 0 | |
) { | |
wp_delete_post( $product->get_id(), true ); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment