We have a problem as I am not a woocommerce developer. The client wants to make a product out of stock once the order is completed (and payment received).
I added an action in functions.php
add_action( 'woocommerce_order_status_completed', 'set_product_out_of_stock', 10, 1 );
function set_product_out_of_stock( $order_id ) {
error_log( "This is a test: set product out of stock $order_id ^^", 0 );
//if ( 'completed' == $order_status) {
$order = new WC_Order( $order_id );
$items = $order->get_items();
foreach ( $items as $item ) {
$status = 'outofstock';
update_post_meta($item['item_id'], '_stock', 0);
update_post_meta( $item['item_id'], '_stock_status', wc_clean( $status ) );
wp_set_post_terms($item['item_id'], 'outofstock', 'product_visibility', true );
}
// }
}
The problem is that this action is not even triggered, The error_log doesn't show in the logs.. what may be the issue?
The website uses woocommerce v 4.7
.
Thank you !