$1*/ mo hosting! Get going with GoDaddy!

Disable delete for Woocommerce order

I have some problem recently with Woocommerce order being deleted by Admin and we have no idea who did it. And that annoys me. Hence, i went ahead and disable Woocommerce delete for any order for all users. Pretty much i do not want any delete to happen for any order regardless it is intentionally or accidentally.

Disable Woocommerce Delete

In order to disable woocommerce delete for order, you need to hook in to the 2 action hook which is wp_tash_post and before_delete_post as shown below,

// disable delete entirely
function restrict_post_deletion($post_ID){
    $type = get_post_type($post_ID);
    if($type == 'shop_order'){
        echo "You are not authorized to delete this page.";
        exit;
    }
}
add_action('wp_trash_post', 'restrict_post_deletion', 10, 1);
add_action('before_delete_post', 'restrict_post_deletion', 10, 1);

what the above does is to get the post_id of the post you are deleting and see what type it is. If the type if a shop_order, do not allow them to delete by exiting the script entirely and show a message to the user so that they will stop doing silly things.

Share
Increase sales with social commerce at BigCommerce. Learn More!