Set Incremental SKU-s to your WooCommerce products

When you have products with no SKU and you would like to set Incremental SKU numbers for them directly in the website – then this snippet will help you.

<?php
if(isset($_GET["set_sku"]))
add_action('wp',function(){
$base_sku = 100;
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => '_sku',
'compare' => 'NOT EXISTS'
)
)
);
$query = new WP_Query($args);
while ($query->have_posts()) {
$query->the_post();
$product_id = get_the_ID();
$sku = $base_sku++;
update_post_meta($product_id, '_sku', "SK_".$sku);
}
wp_reset_query();
die("DONE. You can remove this code from your website!");
});

That’s it. Now just run yourwebsitecom/?set_sku=1 once and done!

The code will create SK_100, SK_101, … SKU-s for your website.

If you want to create SKU-s for variation as well, we can improve our code and get this snippet below:

Continue reading “Set Incremental SKU-s to your WooCommerce products”