Skip to content

Instantly share code, notes, and snippets.

@shameemreza
Created April 22, 2025 09:15
Show Gist options
  • Save shameemreza/a50d2c1b1354305ae4ff63e3b0fd5930 to your computer and use it in GitHub Desktop.
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.
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