Categories : PHP , Wordpress

With this script you can update the stock status of multiple products at once programmatically.

<?php
# load wordpress if you are working on out of wordpress context
require_once __DIR__ . '/wp-load.php';

$skus = ['SKU1','SKU2'];
echo 'Total Skus: '. count($skus) . PHP_EOL;
foreach ($skus as $sku) {
    $product_id = wc_get_product_id_by_sku(trim($sku));
    if ($product_id) {
                $product = wc_get_product($product_id);
        # make it in stock if it is variable product
        if ($product->is_type('variable')) {
            $variations = $product->get_available_variations();
            foreach ($variations as $variation) {
                $variation_id = $variation['variation_id'];
                $variation_product = wc_get_product($variation_id);
                $variation_product->set_stock_status('instock');
                $variation_product->save();
            }
        } else {
            $product->set_stock_status('instock');
            $product->save();
        }
        $product->save();
        echo 'Product: '. $sku .' updated to in stock'. PHP_EOL;
    } else {
        echo 'Product: '. $sku .' not found'. PHP_EOL;
    }
    } else {
        echo 'Product: '. $sku .' not found'. PHP_EOL;
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *