问题
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