Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.03 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Woo filter for sub category
  5.  */
  6. class HbWooAjaxFilterSubCat {
  7.  
  8.     function __construct() {
  9.         add_action( 'init', [ $this, 'ajax_init' ] );
  10.     }
  11.  
  12.     public function ajax_init() {
  13.         add_action( 'wp_enqueue_scripts', array( $this, 'woo_cat_filter_ajax_vars' ) );
  14.         if ( wp_doing_ajax() ) {
  15.             add_action( 'wp_ajax_hb_ajax_filter', [ $this, 'filter' ] );
  16.             add_action( 'wp_ajax_nopriv_hb_ajax_filter', [ $this, 'filter' ] );
  17.         }
  18.     }
  19.  
  20.     public function woo_cat_filter_ajax_vars() {
  21.  
  22.         if ( is_product_category() ) {
  23.             $category = get_queried_object();
  24.             $cat_id   = $category->term_id;
  25.         } else {
  26.             $cat_id = 'none';
  27.         }
  28.  
  29.         wp_localize_script( 'conj-scripts', 'hbFilterCat', [
  30.             'url'    => admin_url( 'admin-ajax.php' ),
  31.             'cat_id' => $cat_id
  32.         ] );
  33.     }
  34.  
  35.     public function filter() {
  36.  
  37.         $filters = $_POST['filter'];
  38.         $cat_id  = (int) $_POST['cat'];
  39.         if ( ! empty( $filters ) ) {
  40.  
  41.             $filter_query = $this->getFilter( $filters, $cat_id );
  42.  
  43. //          var_dump( $filter_query );
  44.             $query = new WP_Query( [
  45.                 'post_type' => 'product',
  46.                 'tax_query' => [
  47.                     'relation' => 'AND',
  48.                     $filter_query,
  49.                 ]
  50.             ] );
  51.  
  52.             if ( $query->have_posts() ) {
  53.                 while ( $query->have_posts() ) {
  54.                     $query->the_post();
  55.                     wc_get_template_part( 'content', 'product' );
  56.                 }
  57.             } else {
  58.                 echo __( 'No products found' );
  59.             }
  60.  
  61.             wp_reset_postdata();
  62.  
  63.             wp_die();
  64.         }
  65.  
  66.         wp_die();
  67.     }
  68.  
  69.     public function getFilter( $filters, $cat_id ) {
  70.         $filter_query = [];
  71.         foreach ( $filters as $filter ):
  72.             if ( ! empty( $filter['checked'] ) ) :
  73.  
  74.                 $checked = [];
  75.  
  76.                 foreach ( $filter['checked'] as $filchecked ):
  77.                     $checked[] = (int) $filchecked;
  78.                 endforeach;
  79.  
  80.                 $filter_query[] = [
  81.                     'taxonomy' => $filter['termName'],
  82.                     'field'    => 'id',
  83.                     'terms'    => $checked,
  84.                 ];
  85.             endif;
  86.         endforeach;
  87.  
  88.         $filter_query[] = [
  89.             'taxonomy' => 'product_cat',
  90.             'field'    => 'id',
  91.             'terms'    => $cat_id
  92.         ];
  93.  
  94.         return $filter_query;
  95.     }
  96. }
  97.  
  98. new HbWooAjaxFilterSubCat();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement