class-wc-prevent-purchasing.php 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. class Booked_WC_Prevent_Purchasing {
  3. private function __construct() {
  4. add_action('pre_get_posts', array($this, 'remove_products_from_shop_listing'), 90, 1);
  5. }
  6. public static function setup() {
  7. return new self();
  8. }
  9. public function remove_products_from_shop_listing( $query ) {
  10. if ( is_admin() ) {
  11. return $query;
  12. }
  13. if ( $query->get('post_type')!=='product' ) {
  14. return $query;
  15. }
  16. global $wpdb;
  17. $booked_products = $wpdb->get_col("SELECT DISTINCT `posts`.`ID` FROM `{$wpdb->posts}` AS `posts`
  18. INNER JOIN `{$wpdb->postmeta}` AS `meta` ON ( `posts`.`ID` = `meta`.`post_id` AND `meta`.`meta_key` = '_booked_appointment' )
  19. WHERE `posts`.`post_type` = 'product'
  20. AND `posts`.`post_status` = 'publish'
  21. AND `meta`.`meta_value` = 'yes'");
  22. if ( !$booked_products ) {
  23. return $query;
  24. }
  25. $post__no_in = (array) $query->get('post__not_in');
  26. $query->set('post__not_in', array_merge($post__no_in, $booked_products));
  27. return $query;
  28. }
  29. }