0

We have a site with over 1200 sku's that are wrong and we want to delete them all without affecting the products.

Is there a quicker way to do this via the db in phpmyadmin.

Any help would be great.

Thanks

GazzaC
  • 13
  • 1
  • 6

1 Answers1

0

Here is your solution. change post per page argument and post meta values for sku. (Not tested)

https://www.themelocation.com/how-to-updateadd-sku-of-all-products-in-woocommerce/

add_action( 'init', 'update_sku', 10, 1);

function update_sku( $sku ){

   $args = array(

           'post_type' => 'product',

          'posts_per_page' => -1

           );

   $i=0;

   $loop = new WP_Query( $args );

   if ( $loop->have_posts() ) {

       while ( $loop->have_posts() ) : $loop->the_post();

           $random_sku = mt_rand(100000, 999999);

           update_post_meta($loop->post->ID,'_sku','');

           $i++;
       endwhile;

   } else {
       echo __( 'No products found' );
   }
   wp_reset_postdata();
}
Harry SM
  • 76
  • 5
  • This solution sets a random value sku's for products. This isn't what's being asked for here. The question is how to go on about removing them from the database, not randomly setting values to all of them. – CaliCo Apr 16 '18 at 17:03
  • Here update_post_meta($loop->post->ID,'_sku',''); remove (update with empty) sku for products. – Harry SM Apr 18 '18 at 06:50
  • Can you confirm the actual sql query that I should try in phpadmin for this? @HarrySM – GazzaC Apr 18 '18 at 07:33
  • You can use like "UPDATE wp_postmeta SET 'meta_value'='' WHERE 'meta_key'='_sku' " Please take a backup first before run query. – Harry SM Apr 18 '18 at 09:10
  • @HarrySM Im not proficant in running sql queries. Is this the actual cmd to run this to remove skus? – GazzaC Apr 19 '18 at 09:07
  • @GazzaC Yes, run query between double quote. but before that please take a backup of database or run this query in staging server first. – Harry SM Apr 19 '18 at 12:57
  • @HarrySM I get this error #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'meta_value' <> '')' at line 1 – GazzaC Apr 21 '18 at 11:47
  • Hi Guys, Anyone know how to do this? – GazzaC Apr 24 '18 at 10:30