how to execute a function only when i send an order to trash [closed]

It depend if you’re using HPOS or not:

If not:

function my_custom_function_on_order_trash( $post_id ) {
    // Get the post type of the trashed item
    $post_type = get_post_type( $post_id );

    // Check if the trashed item is an order
    if ( 'shop_order' === $post_type ) {
        // Your custom code here
      
    }
}
add_action( 'wp_trash_post', 'my_custom_function_on_order_trash' );

If you’re using HPOS (High-Performance Order Storage) in WooCommerce, orders are no longer treated as standard posts, so wp_trash_post might not be the best hook to use. Instead, you should use WooCommerce-specific hooks to detect when an order is being moved to the trash.

Here’s how you can achieve that with HPOS:

function my_custom_function_on_order_trash( $order ) {
    if ( $order instanceof \Automattic\WooCommerce\Orders\Order ) {
        // Your custom code here

    }
}
add_action( 'woocommerce_before_trash_order', 'my_custom_function_on_order_trash' );

techhipbettruvabetnorabahisbahis forumutaraftarium24edueduseduseduseduedusedusedueduedu