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' );
Related Posts:
- Woocommerce – Hide a Column in Cart Table
- How do I change the initial quantity of a product in woocommerce?
- how to add custom functionality after woocommerce place order button
- Remove an action by extending class and replacing it
- WooCommerce change Tax Class programmatically when Recalculating an existing Order [closed]
- Hide one specific woocoomerce product
- how to retrieve post_id under woocommerce_add_to_cart_validation hook?
- woocommerce single product page hook not working
- Woocommerce: block user removing cart item
- Trigger a JavaScript function based on the data fetched from Woo commerce hook
- Custom fields in the billing address section woocommerce
- Difference Between Filter and Action Hooks?
- How many times will this code run? (or, how rich is grandma?)
- add_action hook for completely new post?
- WordPress Hook for user register
- Using filters and actions for plugin API?
- Explanation of the “posts_join” and “posts_fields” filter hooks?
- product-attribute-slug-is-too-long-28-characters-max
- How to check if certain plugin exists and in expected version
- Plugin vs Settings load order (woocommerce dependency)
- Is it possible to modify the media library admin screen?
- What function to hook for changes made in status and visibility of a post
- Add multiple shipping rates from add_rate function with custom ID
- Order of Operation for these three hooks
- Modifying values with add_action to be sent to db
- save_post hook not triggered in WP v3.6.1
- Hook to get image filename when it is uploaded
- How to display a message about updates in the plugin list
- How to delete an user including data from custom database tables
- Should action callbacks start with a verb?
- Execute Hook on the footer or header after activating a plugin
- How to check if Woocommerce Order number equals to post ID?
- How plugins_loaded works?
- What filter should I use to insert a button inside on Media>Add New
- WooCommerce registration password field not displaying
- How to use a hook to override an update_post_meta call in a plugin?
- External Authentication, session_tokens not destroyed on logout
- Woocommerce checkout update totals with datepicker
- correct way to call javascript into hook function
- Strange issue saving custom field data for a WooCommerce order
- Woocommerce – Convert Delivery method into a custom field
- Prefixing plugin hooks (actions/filters) with a wrapper class or functions
- Send push when new post is published in a certain category
- How can we display product price $104 on the cart while the credit card payment method is checked?
- In woocommerce cart item we can add custom text but how can i add custom link? [closed]
- Updating Woocommerce Settings API when WordPress Settings API saved and vise versa
- How to extend WooCommerce API to show bookable product availability rules?
- Customize WooCommerce my account dashboard through plugin
- Woocommerce Composite Products – Add a composite product to cart programmatically via ajax [closed]
- Force HTTPS 301 redirect with hook
- use of do_action() without any functions attached
- What action hook can I use to add a JavaScript to a page post using a theme template that is not including get_header() nor get_footer()?
- Plugin uninstall function is not working
- Woocommerce order empty items array
- How to get all product in the woo-commerce? [closed]
- Bind a function with its own argument to show something dynamically after every content
- What’s the hook for filtering every texts of site?
- Hook add_attachment error
- Sending an SMS on new order placed – WooCommerce
- Update variable value via add_filter
- Can we use a library under MIT license in a WooCommerce plugin?
- Set _regular_price 0 if empty in woocommerce product
- hook update_option/updated_option empty $old_value and $value
- Woocommerce – Provide Associated Category Link for Product List Plugin [closed]
- How to find list of all functions bind to a particular hook from my plugin?
- Can I Hook Into the TinyMCE Insert/Edit Link Button to Use Shortlink For Post?
- want to show CMB2 metabox on woocommerce product data tab
- Dynamic page generation upon purchase of a product
- Customise Grouped Product display in Woocommerce with custom column
- Create Woocommerce product without creating api credentials
- Add custom product data using javascript when add to cart button is clicked
- How to enhance a self developed plugin by its own plugin architecture
- `registration_errors` filter doesn’t seem to be called
- How to grab data after wp user search is complete
- Trying to run a Ajax request from a checkout form in woocommerce via a custom plugin
- Securing custom rest API endpoints with public access from PWA
- WooCommerce – Stop assigning order numbers
- Adding customs fields on each product on the cart
- Override woocommerce loop-start.php from theme using plugin?
- How to direct WordPress to load custom template for custom post type
- Redirect WooCommerce checkout to cart
- Does hooking into a plugin action / filter override the plugin’s function for that hook?
- Nested Hooks with do_action for performance reasons – safe/necessary?
- Pre-populated WooCommerce checkout fields: dropdown fields issue
- WordPress Plugin Boilerplate – add actions and/or filters based on user’s role
- How to trigger WooCommerce user profile edit change?
- Override theme’s WooCommerce file
- WC_Geolocation not found inside plugin
- How to use functions from Woocommerce documentation?
- How and when would I implement some code which would take data from one part of the Db and put it in another?
- add_option_{$option} action hook not being called
- plugin modal/popup integration best practice
- Woocommerce Custom Checkout
- Woo-commerce: Adding additional page sections using a child theme
- Get user logged in status from within a plugin. $current_user not defined
- Insert plugin html content to a specific spot in the frontpage
- get_term_by() returning null on plugin
- WooCommerce adds a newline symbol (\n) between email recipients
- Allow HTML in product attributes and variation for WooCommerce
- How do I add multiple custom menu Woo-commerce my account page?