Woocommerce:每天将所有产品保存 X 次

我有更新所有产品的代码。它工作正常。但我需要改变它。


    add_action( 'wp', '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();

    }

}

上面的函数将更新的产品标记为“更新”。然后此代码将保存所有产品一次。


问题:如何更改此功能,以便每次执行该功能时都保存所有产品,而不仅仅是一次。


慕姐4208626
浏览 126回答 1
1回答

精慕HU

更新:该功能将每天运行 5 次(最多)。如果您有很多产品,则无法更新所有产品。每次浏览您的网站时都会运行以下内容,并且每次都会更新定义数量的产品以避免出现问题(它将永远运行,您将删除它):add_action( 'wp', 'update_products_by_x' );function update_products_by_x(){&nbsp; &nbsp; // Get next start time&nbsp; &nbsp; $start_time = (float) get_option('product_update_start_time');&nbsp; &nbsp; if ( $start_time <= time() ) {&nbsp; &nbsp; &nbsp; &nbsp; if ( empty($start_time) || $start_time == 0 ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $start_time = time();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ## ----- SETTINGS (Below) ----- ##&nbsp; &nbsp; &nbsp; &nbsp; $next_time = $start_time + ( 4.8 * 60 * 60 ); // 5 times à day&nbsp; &nbsp; &nbsp; &nbsp; $limit&nbsp; &nbsp; &nbsp;= 200; // Fix the number of products to update each time&nbsp; &nbsp; &nbsp; &nbsp; $offset&nbsp; &nbsp; = (int) get_option('product_update_offset');&nbsp; &nbsp; &nbsp; &nbsp; // Get products&nbsp; &nbsp; &nbsp; &nbsp; $products_ids = get_posts( array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'post_type'&nbsp; &nbsp; &nbsp; &nbsp; => 'product', // or ['product','product_variation'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'numberposts'&nbsp; &nbsp; &nbsp; => $limit,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'offset'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=> $offset,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'post_status'&nbsp; &nbsp; &nbsp; => 'publish',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'fields'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=> 'ids',&nbsp; &nbsp; &nbsp; &nbsp; ) );&nbsp; &nbsp; &nbsp; &nbsp; $count = count($products_ids);&nbsp; &nbsp; &nbsp; &nbsp; if( $count > 0 ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Loop through product Ids&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ( $products_ids as $product_id ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Get the WC_Product object&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $product = wc_get_product($product_id);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $product->save();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; update_option('product_update_offset', ( $count == $limit ? ( $count + $offset ) : 0 ) );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; update_option('product_update_start_time', $next_time);&nbsp; &nbsp; }}代码位于活动子主题(或活动主题)的 function.php 文件中。测试和工作。
打开App,查看更多内容
随时随地看视频慕课网APP