Little pixelated me

Richard Keys

Digital specialist from Barnsley, United Kingdom.

Technical Director at Genius Division; we solve problems using digital technologies.

Recently imported many products, variations, attributes in to WooCommerce from a CSV file. All the attributes used for variations were set to show on the front-end – whoops!

Two options: Go through each product and uncheck “Visible on the product page” or this bit of code:

add_action('init',function(){
    $args = array(
        'post_type'      => 'product',
        'posts_per_page' => -1,
    );

    $products = get_posts($args);
    foreach ($products as $post) {
        $update = false;
        $meta = get_post_meta($post->ID,'_product_attributes',true);
        if($meta)
        {
            foreach($meta as $k => $a)
            {
                if($a['is_variation'] && $a['is_visible'])
                {
                    $a['is_visible'] = 0;
                    $update = true;
                    $meta[$k] = $a;
                }

            }

            if($update)
                update_post_meta( $post->ID,'_product_attributes', $meta);
        }
    }
});

Loops through products, and changes the meta as if the box were unchecked. Use at your own peril – definitely not on a production site!

Last updated: October 25th, 2019


Copyright © 2019 Richard Keys