Remove/hide total_sales WooCommerce custom field

Is there a way to remove the total_sales custom field when displaying the_meta for a product?

I can change the entry in the editor to another name and value, but it magically appears again and won’t be deleted.

Related posts

Leave a Reply

2 comments

  1. I would use the ‘the_meta_key’ filter for this. you have a few options, and of course you can combine them.

    Control what to hide with CSS

    add_filter( 'the_meta_key' , 'class_custom_fields', 10, 3);
    
    function classes_custom_fields($string, $key, $value){
        return str_replace('<li>','<li class="' . str_replace(' ', '-', $key). '">',$string);
    }
    
    <style>
    ul.post-meta li.total_sales{
        display:none;
    }
    </style>
    

    Control what to hide via PHP & CSS

    add_filter( 'the_meta_key' , 'hide_custom_fields', 10, 3);
    
    function hide_custom_fields($string, $key, $value){
        $hide_keys = array(
            'total_sales'
        );
        if(in_array(strtolower($key), $hide_keys)){
            return str_replace('<li>','<li class="hide">',$string);
        }
        return $string;
    }
    <style>
        .hide{
            display:none;
        }
    </style>
    

    Control what to Display with PHP

    add_filter( 'the_meta_key' , 'allowed_custom_fields', 10, 3);
    
    function allowed_custom_fields($string, $key, $value){
    
        $allowed_keys = array(
            'attribute one',
        );
    
        if(in_array(strtolower($key), $allowed_keys)){
            return $string;
        }
    }
    

    Control what not to Display with PHP

    add_filter( 'the_meta_key' , 'disallowed_custom_fields', 10, 3);
    
    
    function disallowed_custom_fields($string, $key, $value){
    
        $disallowed_keys = array(
            'total_sales'
        );
        if(!in_array(strtolower($key), $disallowed_keys)){
            return $string;
        }
    }
    
  2. Since total_sales ends up as the last list item of it’s ul group (in this case ul.post-meta), just enter: ul.post-meta li:last-child{
    display: none;
    }