I would like to update the stock of woocommerce products once a day. The quantity to be entered daily is given by an acf field present in each single product.
The products are variable, but they all have a single variation, so the stock is set only in the general tab of the product. So the stock is unique.
This is my code, but the update function returns me false
, while the daily stock is correctly displayed in each product.
function tommix_custom_cron_schedule_two($schedules) {
if (!isset($schedules["tommix_daily"]))
$schedules["tommix_daily"] = array(
'interval' => 86400,
'display' => __('Daily'),
);
return $schedules;
}
add_filter('cron_schedules', 'tommix_custom_cron_schedule_two');
if (!wp_next_scheduled('wp_footer')) {
wp_schedule_event(time(), 'tommix_daily', 'wp_footer');
}
add_action('wp_footer', 'tommix_cron_function');
function tommix_cron_function($post_id) {
$daily_stock = get_field('al_giorno', $post_id);
if (!empty($daily_stock)) {
d($daily_stock);
//update_post_meta($post_id,"_stock",$daily_stock);
d(update_post_meta($post_id, "_stock", $daily_stock));
}
}