Categories : PHP , Wordpress

If you want to update simple or variable product description in your woocommerce wordpress store programmatically.

For one product example : based on product id

# Initialize wordpress if you are working out of the plugin or theme
require_once __DIR__ . '/wp-load.php';

# wc update product description programmatically
$product_id = 8281;

$product = wc_get_product($product_id);
$product->set_description('This is a new description');
$product->save();

For product id array example : based on array that has product IDs

# Initialize wordpress if you are working out of the plugin or theme
require_once __DIR__ . '/wp-load.php';

# get all product ids
$products = wc_get_products(array(
    'limit' => -1,
    'status' => 'publish',
    'type' => 'variable',
    'orderby' => 'date',
    'order' => 'DESC',
    'return' => 'ids',
));

# update descriptions in array

foreach ($products as $product_id){
    $product = wc_get_product($product_id);
    # update product description
    $product->set_description('New description');
    $product->save();
}

Example for id and description array

# Initialize wordpress if you are working out of the plugin or theme
require_once __DIR__ . '/wp-load.php';

# product id and description data
$products_and_descriptions =[
    ["id" => 1, "description" => "New Description 1"],
    ["id" => 2, "description" => "New Description 2"],
    ["id" => 3, "description" => "New Description 3"],
    ["id" => 4, "description" => "New Description 4"],
    ["id" => 5, "description" => "New Description 5"],
    ["id" => 6, "description" => "New Description 6"],
    ["id" => 7, "description" => "New Description 7"],
    ["id" => 8, "description" => "New Description 8"],
    ["id" => 9, "description" => "New Description 9"],
    ["id" => 10, "description" => "New Description 10"],
];

# loop for update
foreach ($products as $product){
    $product_id = $product['id'];
    $product = wc_get_product($product_id);
    $product->set_description($product['description']);
    $product->save();
}

In this article I have showed you how to update product description in wordpress woocommerce store programmatically.

First example was about update from one product id, second one is about getting all product ids and updating, third one is updating from product id, description array.

With this examples you can make your own updater.

Leave a Reply

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