替换弃用的woocommerce增加股票功能

I'm updating some code that increases stock on woocommerce after an order cancellation, but it doesn't work.

Previously, we were using the increase_stock function, but now is deprecated and they suggest using wc_update_product_stock. I'm using this code in a custom plugin. The result is that:

  1. The order products stock get increased on order cancelation.
  2. We get a notification about how many units were increased.

    add_action("woocommerce_order_status_changed", "my_awesome_publication_notification");
    function my_awesome_publication_notification($order_id, $checkout=null) {
        global $woocommerce;
        $order = new WC_Order( $order_id );
    
        if ( ! get_option('woocommerce_manage_stock') == 'yes' && ! sizeof( $order->get_items() ) > 0 ) {
            return;
        }
    
        if($order->status === 'cancelled' || $order->status === 'failed' || $order->status === 'refunded')
        {
            foreach ( $order->get_items() as $item ) {
                if ( $item['product_id'] > 0 ) {
                    $_product = $order->get_product_from_item( $item );
                    if ( $_product && $_product->exists() && $_product->managing_stock() ) {
                        $old_stock = $_product->stock;
                        $qty = apply_filters( 'woocommerce_order_item_quantity', $item['qty'], $this, $item );
                        $new_quantity = $_product->increase_stock( $qty );
                        do_action( 'woocommerce_auto_stock_restored', $_product, $item );
                        $order->add_order_note( sprintf( __( 'Inventario del Item #%s aumento de %s a %s.', 'woocommerce' ), $item['product_id'], $old_stock, $new_quantity) );
                        $order->send_stock_notifications( $_product, $new_quantity, $item['qty'] );
                    }
                }
            }
        }
    }
    

I have tried replacing the line:

$new_quantity = $_product->increase_stock( $qty );

for

$new_quantity = $_product->wc_update_product_stock($_product,$qty, 'increase');

but the site crashes when I change an order from pending state to canceled state.