Search code examples
phpwordpresswoocommercehook-woocommerce

Do an action when woocomerce saves a product


I need to save some options when a woocommerce product gets saved.

Is there any way to do it?


Solution

  • You could use save_post action hook!

    save_postDocs

    You could use this boilerplate to do your custom work when a product gets saved!

    add_action('save_post', 'do_some_custom_work');
    
    function do_some_custom_work( $post_id )
    {
    
        if ('product' == get_post_type($post_id)) {
    
            // Do what is necessary here!
    
            die('You hit the right hook!!!');
        }
    }
    

    Note:

    • In addition to $post_ID, you could pass $post and $update to your callback function as well. Please read the documentation for more details!
    add_action('save_post', 'do_some_custom_work', 10, 3);
    
    function do_some_custom_work( $post_id, $post,  $update )
    {
    
        if ('product' == get_post_type($post_id)) {
    
            // Do what is necessary here!
    
            die('You hit the right hook!!! This is the product id: ' . $post->ID);
        }
    }