-1

我正在为我的商店使用 wordpress 和 woocommerce。

我需要每晚补货。

前任。我有一个库存数量为 30 的产品。然后,如果有人购买该产品,数量为 29 - 当然。但是第二天,股票应该会再次自动切换到 30。

有谁知道这是怎么做到的吗?或者创建一个 php/code/function 什么的?

4

1 回答 1

1

注册一个名为的 cron 事件increase_stock_daily并让它每天运行或根据您的要求运行。

// Add function to register event to WordPress init
add_action( 'init', 'register_daily_stock_event');

// Function which will register the event
function register_daily_stock_event() {
    // Make sure this event hasn't been scheduled
    if( !wp_next_scheduled( 'increase_stock_daily' ) ) {
        // Schedule the event
        wp_schedule_event( time(), 'daily', 'increase_stock_daily' );
    }
}   

function increase_stock_daily() {
    $product = new WC_Product(10); // replace 10 with your own product ID
    if($product->get_total_stock() < 30) {
        $product->set_stock(30);
    }
}
于 2016-05-02T15:37:48.920 回答