Now Reading: Generate Random SKUs for WooCommerce Products


Generate Random SKUs for WooCommerce Products


Here, we’ll explore how to streamline your WooCommerce product management by automatically generating and assigning random Stock Keeping Unit (SKU) numbers to your products. With a custom script, you can effortlessly add unique SKUs to your entire product catalog, enhancing organization and simplifying inventory tracking. Say goodbye to manual SKU assignments and embrace a more efficient way to manage your e-commerce store.

Random sku for woocommerce generator

This code defines a function generate_random_sku() that generates a random SKU of the desired length (in this example, 8 characters).

The generate_and_assign_random_sku_to_all_products() the function iterates through all products and assigns a randomly generated SKU to each one.

The action hook init is used to execute the function once.

After all products have random SKUs assigned, you should remove or deactivate the custom code to prevent it from running again accidentally.

// Add this code to your theme's functions.php file , coded by
function generate_and_assign_random_sku_to_all_products() {
    $args = array(
        'post_type' => 'product',
        'posts_per_page' => -1,

    $products = new WP_Query($args);

    if ($products->have_posts()) {
        while ($products->have_posts()) {
            $product_id = get_the_ID();
            $random_sku = generate_random_sku(); // Call a function to generate a random SKU.
            update_post_meta($product_id, '_sku', $random_sku);


function generate_random_sku() {
    $characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $sku_length = 8; // Change this value to adjust the length of the SKU.
    $sku = '';

    for ($i = 0; $i < $sku_length; $i++) {
        $sku .= $characters[rand(0, strlen($characters) - 1)];

    return $sku;

// Run the function once to generate and assign random SKUs to all products.
add_action('init', 'generate_and_assign_random_sku_to_all_products');
2 People voted this article. 2 Upvotes - 0 Downvotes.

What do you think?

Show comments / Leave a comment

Leave a reply

Quick Navigation
  • 01

    Generate Random SKUs for WooCommerce Products