Обновление: функция будет запускаться 5 раз в день (макс.).
Если у вас много продуктов, вы не можете обновить все продукты.
Следующее будет выполняться каждый раз, когда ваш веб-сайт просматривается, и будет обновлять определенное количество продуктов каждый раз, чтобы избежать проблем (оно будет работать вечно, если вы его удалите) :
add_action( 'wp', 'update_products_by_x' );
function update_products_by_x(){
// Get next start time
$start_time = (float) get_option('product_update_start_time');
if ( $start_time <= time() ) {
if ( empty($start_time) || $start_time == 0 ) {
$start_time = time();
}
## ----- SETTINGS (Below) ----- ##
$next_time = $start_time + ( 4.8 * 60 * 60 ); // 5 times à day
$limit = 200; // Fix the number of products to update each time
$offset = (int) get_option('product_update_offset');
// Get products
$products_ids = get_posts( array(
'post_type' => 'product', // or ['product','product_variation'],
'numberposts' => $limit,
'offset' => $offset,
'post_status' => 'publish',
'fields' => 'ids',
) );
$count = count($products_ids);
if( $count > 0 ) {
// Loop through product Ids
foreach ( $products_ids as $product_id ) {
// Get the WC_Product object
$product = wc_get_product($product_id);
$product->save();
}
update_option('product_update_offset', ( $count == $limit ? ( $count + $offset ) : 0 ) );
}
update_option('product_update_start_time', $next_time);
}
}
Код помещается в файл function.php вашей активной дочерней темы (или активной темы). Проверено и работает.