I’m trying to creste a cron job that will compare a date set as product meta data to the current date, then update the products stock if the date set as product meta data has passed.
Here’s what I have so far, but it doesn’t work:
// Custom Cron Function
function update_pre_order_stock_status($post_id){
$current_date = date( 'YYYY-mm-dd' );
$expiryDate = $product->get_meta('woo_expiry_date');
$woo_expiry_action = $product->get_meta('woo_expiry_action');
$product_category = array( 'comic-book-pre-orders', 'dc-comics-pre-orders', 'image-comics-pre-orders', 'manga-pre-orders', 'marvel-comics-pre-orders', 'other-publisher-pre-orders');
if(( is_product_category( $product_category )) && ( $expiryDate <= $current_date ) && ( $woo_expiry_action == 'out' )) {
// 1. Updating the stock quantity
update_post_meta( $post_id, '_stock', 0);
// 2. Updating the stock quantity
update_post_meta( $post_id, '_stock_status', wc_clean( 'outofstock' ) );
// 3. Updating post term relationship
wp_set_post_terms( $post_id, 'outofstock', 'product_visibility', true );
}
}
if ( ! wp_next_scheduled( 'my_pre_order_stock_update' ) ) {
wp_schedule_event( time(), 'hourly', 'my_pre_order_stock_update' );
}
add_action( 'my_pre_order_stock_update', 'update_pre_order_stock_status' );
The Cron event ‘my_pre_order_stock_update’ shows up in the cron list, but running it isn’t doing anything.
Any recommendations would be very much appreciated!