businessdad icon

H.Rolands - Hide events that have not started

businessdad | PRO | 11/12/15 08:44:17 AM UTC | 0 ⭐ | 879 👁️ | Never ⏰ | []
PHP |

640 B

|

None

|

0 👍

/

0 👎

/**
 * 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