Advertisement
palsushobhan

hide-on-vacation-vendor-products-from-shop-page

Jul 17th, 2024
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.55 KB | None | 0 0
  1. add_action( 'pre_get_posts', function( $query ) {
  2.     global $wpdb;
  3.     if(!$query->is_main_query()) return;
  4.  
  5.     if( is_shop() || is_product_category() || wcfm_is_store_page() ) {
  6.         $vacation_args = array(
  7.             'role__in'   => apply_filters( 'wcfmmp_allwoed_vendor_user_roles', array( 'wcfm_vendor' ) ),
  8.             'meta_query' => array(
  9.                 array(
  10.                     array(
  11.                         'key'     => 'wcfmmp_profile_settings',
  12.                         'value'   => ';s:18:"wcfm_vacation_mode";s:3:"yes";',
  13.                         'compare' => 'LIKE',
  14.                     ),
  15.                     array(
  16.                         'key'     => 'wcfmmp_profile_settings',
  17.                         'value'   => ';s:30:"wcfm_disable_vacation_purchase";s:3:"yes";',
  18.                         'compare' => 'LIKE',
  19.                     ),
  20.                 ),
  21.             ),
  22.             'fields' => 'ID',
  23.         );
  24.         $on_vacation = get_users( $vacation_args );
  25.         $sql = "select user_id, meta_value from {$wpdb->prefix}usermeta where user_id IN (" . implode( ",", $on_vacation ) . ") AND meta_key = 'wcfmmp_profile_settings'";
  26.         $result = $wpdb->get_results($sql);
  27.         $exclude_vendor = [];
  28.         if( !empty( $result ) ) {
  29.             foreach ($result as $value) {
  30.                 $settings = maybe_unserialize($value->meta_value);
  31.                 if(isset($settings['wcfm_vacation_mode_type']) && 'instant' == $settings['wcfm_vacation_mode_type']) {
  32.                     $exclude_vendor[] = $value->user_id;
  33.                     continue;
  34.                 }
  35.                 $wcfm_vacation_start_date = isset($settings['wcfm_vacation_start_date']) ? $settings['wcfm_vacation_start_date'] : '';
  36.                 $wcfm_vacation_end_date = isset($settings['wcfm_vacation_end_date']) ? $settings['wcfm_vacation_end_date'] : '';
  37.                 if( $wcfm_vacation_start_date && $wcfm_vacation_end_date ) {
  38.                     $current_time = strtotime( 'midnight', current_time( 'timestamp' ) );
  39.                     $start_time = strtotime( $wcfm_vacation_start_date );
  40.                     $end_time = strtotime( $wcfm_vacation_end_date );
  41.                     if( ($current_time >= $start_time) && ($current_time <= $end_time) ) {
  42.                         $exclude_vendor[] = $value->user_id;
  43.                         continue;
  44.                     }
  45.                 }
  46.             }
  47.             $exclude_vendor = array_unique($exclude_vendor);
  48.         }
  49.         $query->set( 'author__not_in', $exclude_vendor );
  50.     }
  51. }, 100 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement