woocommerceproduct-variationssku

Remove all parents SKU from Woocommerce


So I imported more than 6000 products and I'm facing a problem.

The thing is that I didn't have a parent SKU so when I imported all my variations, my first variation took the same SKU as the parent one, so when I update price it's doesn't work for my first variation.

So I would like to just remove the parent sku from all my variations and replace it by blank or random.

Is it possible or I have to reimport everything?

Thanks in advance.


Solution

  • Place the following function in your functions.php . Always do backup before bulk changes. This is tested and works on WP 6.0.2 WC 6.9.2

    // Change hook if needed. This will run on each refresh of the website
    add_action('init','test');
    function test() {
        // Get variable products.
        $args = array(
            'type' => 'variable',
            'return' => 'ids',
        );
        $product_ids = wc_get_products( $args );
        foreach($product_ids as $product_id) {
            // Get the current parent sku 
            $main_sku = get_post_meta( $product_id, '_sku', true );
            // If not empty clear it
            if(!empty($main_sku)) {
                update_post_meta($product_id, '_sku', '');
            }
        }
    }