Skip to content

Instantly share code, notes, and snippets.

@Acephalia
Created March 9, 2024 03:42
Show Gist options
  • Save Acephalia/8e49a2176cdadbd43e73a1b264bb6f03 to your computer and use it in GitHub Desktop.
Save Acephalia/8e49a2176cdadbd43e73a1b264bb6f03 to your computer and use it in GitHub Desktop.
Woocommerce Bulk Resave/Update All Products
// This code will resave / update 200 woocommerce products every time you visit one of your pages. Increase or decrease limit depending on your server resources.
add_action( 'woocommerce_loaded', 'update_products_by_x' );
function update_products_by_x(){
$limit = 200;
// getting all products
$products_ids = get_posts( array(
'post_type' => 'product', // or ['product','product_variation'],
'numberposts' => $limit,
'post_status' => 'publish',
'fields' => 'ids',
'meta_query' => array( array(
'key' => '_sync_updated',
'compare' => 'NOT EXISTS',
) )
) );
// Loop through product Ids
foreach ( $products_ids as $product_id ) {
// Get the WC_Product object
$product = wc_get_product($product_id);
// Mark product as updated
$product->update_meta_data( '_sync_updated', true );
$product->save();
}
}
@Acephalia
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment