/**
* Hides products whose start date is in the past.
*
* @param bool visible The original visibility flag.
* @param int product_id The product ID.
* @return bool
* @author Aelia <[email protected]>
*/
add_filter('woocommerce_product_is_visible', function($visible, $product_id) {
$start_date = DateTime::createFromFormat('Ymd', trim(get_post_meta($product_id, 'start_date', true)));
// If a valid start date was entered, the product is visible only if said
// start date has not yet been reached
if(!empty($start_date)) {
$today = new DateTime();
$visible = $today < $start_date;
}
return $visible;
}, 10, 2);
Comments