Woocommerce - Automatic restock a product [closed]

一笑奈何 提交于 2019-12-23 06:10:19

问题


I'm using wordpress and woocommerce for my shop.

I need to restock a product every night.

Ex. I have a product where the stock amount is 30. Then if someone buys the product the amount is 29 - of course. But then the next day, the stock should automatically switch to 30 again.

Does anyone know how to do that? Or create a php/code/function or something?


回答1:


Register a cron event named increase_stock_daily and have it run daily or as per your requirement.

// 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);
    }
}


来源:https://stackoverflow.com/questions/36984277/woocommerce-automatic-restock-a-product

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!