skip to Main Content

I am trying to read product value when admin click on trash for a product (image attached).

enter image description here

After some searching i found that woocommerce_trash_{$post_type} hook can be use for this purpose.

I found the details on class-wc-product-data-store-cpt.php line 307 @version 3.0.0

And I applied the code:

add_action('woocommerce_trash_product', 'My_custom_trash_product');
function My_custom_trash_product($product_id) {
    error_log(print_r($product_id, true));
}

But it is not firing. I am using WooCommerce 6.0.0. Any advice?

2

Answers


  1. Chosen as BEST ANSWER

    Thanks @7uc1f3r for your reply. It is working & I also found a another action hook trashed_post from here which fires after a post is sent to the trash.

    I submitted my solution here -

    /**
     * Fires after a post is sent to the Trash.
     *
     * @since 2.9.0
     *
     * @param int $post_id Post ID.
     */
    function action_trashed_post( $post_id ) {
        error_log( print_r( $post_id, true ) );
    }
    add_action( 'trashed_post', 'action_trashed_post', 10, 1 );
    

  2. See: Trash/delete product hooks don’t fire

    We don’t control the WordPress UI delete and trash functionality, so
    it bypasses CRUD completely.

    If you need to detect these events across CRUD and across WordPress
    UI, use wp_trash_post and wp_delete_post actions. If we ever have our
    own UI (not WP UI) we’ll be able to consistently fire them everywhere.

    So alternatively you can use:

    /**
     * Fires before a post is sent to the Trash.
     *
     * @since 3.3.0
     *
     * @param int $post_id Post ID.
     */
    function action_wp_trash_post( $post_id ) {
        error_log( print_r( $post_id, true ) );
    }
    add_action( 'wp_trash_post', 'action_wp_trash_post', 10, 1 );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search