Created
March 9, 2024 03:42
-
-
Save Acephalia/8e49a2176cdadbd43e73a1b264bb6f03 to your computer and use it in GitHub Desktop.
Woocommerce Bulk Resave/Update All Products
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Original code by LoicTheAztec: https://stackoverflow.com/a/55966698/11055908