Advertisement
Guest User

WP_Query

a guest
Nov 20th, 2014
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.52 KB | None | 0 0
  1. <?php
  2. function product_filter_ajax() {
  3.         global $wpdb, $product, $post;
  4.         if (!isset($_REQUEST['filters'])) die('none');
  5.         $filters = array();
  6.         $filtersAA = array();
  7.         $possibleTaxonomies = array('price', 'product_brand', 'pa_colour', 'pa_size', 'pa_strap', 'pa_function', 'pa_movement');
  8.         $postCount = 0;
  9.  
  10.         // Just placing into an easier data structure
  11.         foreach($_REQUEST['filters'] AS $filter) {
  12.                 $filters[] = parse_filter_option($filter);
  13.         }
  14.  
  15.         foreach($filters AS $selectedFilter) {
  16.                 $filtersAA[$selectedFilter[0]] = $selectedFilter[1];
  17.         }
  18.  
  19.         // Set variable variable so each taxonomy can be accessed via $price, $product_brand, $pa_colour etc from $possibleTaxonomies list
  20.         foreach($filtersAA AS $key => $val) {
  21.                 $$key = array(
  22.                                 'taxonomy' => $key,
  23.                                 'field' => 'slug',
  24.                                 'terms' => $val
  25.                         );
  26.         }
  27.  
  28.         // Add all additional filters
  29.         foreach($possibleTaxonomies AS $taxonomy) {
  30.                 if (!isset($$taxonomy)) {
  31.                         $$taxonomy = array();
  32.                 }
  33.         }
  34.  
  35.         if (isset($price) && !empty($price)) {
  36.                 $priceRange = parse_filter_option($price['terms'], '-');
  37.         }
  38.  
  39.         // WP_Query thing goes here to actually make use of filter...
  40.         // Base query arguments go here
  41.         $queryArgs = array(
  42.                                 'post_type' => 'product',
  43.                                 'posts_per_page' => -1,
  44.                                 'post_status' => 'publish',
  45.                                 'meta_query' => array(
  46.                                         array(
  47.                                                 'key'                   => '_visibility',
  48.                                                 'value'                 => array('catalog', 'visible'),
  49.                                                 'compare'               => 'IN'
  50.                                         )
  51.                                 ),
  52.                                 'tax_query' => array(
  53.  
  54.                                 )
  55.                         );
  56.  
  57.         if (count($filtersAA) > 1) {
  58.                 $queryArgs['tax_query']['relation'] = 'AND';
  59.         };
  60.  
  61.         foreach($possibleTaxonomies AS $taxonomy) {
  62.                 // Price is different
  63.                 if ($taxonomy == 'price') continue;
  64.  
  65.                 if (isset($$taxonomy) && !empty($$taxonomy)) {
  66.                         $queryArgs['tax_query'][] = $$taxonomy;
  67.                 }
  68.         }
  69.  
  70.         if (isset($priceRange) && !empty($priceRange)) {
  71.                 $queryArgs['meta_key'] = '_price';
  72.                 $queryArgs['meta_query'] = array(
  73.                         array(
  74.                                 'key' => '_price',
  75.                                 'type' => 'NUMERIC',
  76.                                 'value' => $priceRange,
  77.                                 'compare' => 'BETWEEN'
  78.                                 )
  79.                         );
  80.         }
  81.  
  82.         if (isset($sortBy) && !empty($sortBy)) {
  83.                 $queryArgs['meta_key'] = '_price';
  84.                 $queryArgs['orderby'] = array('meta_value_num' => $sortBy['terms']);
  85.         }
  86.  
  87.         // Add any extra modifications required by the specific page this is called on
  88.         $queryArgs = get_page_category($_REQUEST['page'], $queryArgs);
  89.         var_dump($queryArgs);
  90.  
  91.         $filteredQuery = new WP_Query($queryArgs);
  92.        
  93.         // Bestsellers may have applied a filter to ordering...try to check and remove it if so
  94.         if (array_key_exists('edit_bestsellers_orderby', $GLOBALS['wp_filter'])) {
  95.                 remove_filter('posts_orderby', 'edit_bestsellers_orderby');
  96.         }
  97.         if (array_key_exists('edit_bestsellers_join', $GLOBALS['wp_filter'])) {
  98.                 remove_filter('posts_join', 'edit_bestsellers_join');
  99.         }
  100.         if (array_key_exists('edit_bestsellers_where', $GLOBALS['wp_filter'])) {
  101.                 remove_filter('posts_where', 'edit_bestsellers_where');
  102.         }
  103.  
  104.         if ($filteredQuery->have_posts()) {
  105.                 do_action( 'woocommerce_before_shop_loop' );
  106.                 while ($filteredQuery->have_posts()) {
  107.                         $filteredQuery->the_post();
  108.                         ?>
  109.                         <?php wc_get_template_part( 'content', 'product' ); ?>
  110.                         <?php
  111.                 }
  112.         }
  113.  
  114.         die();
  115. }
  116.  
  117. function parse_filter_option($option, $character = '=') {
  118.         $option = explode($character, $option);
  119.  
  120.         return array($option[0], $option[1]);
  121. }
  122.  
  123. function get_page_category($href, $queryArgs) {
  124.         $url = parse_url($href);
  125.        
  126.         // Sort out path here to get any special WP_Query arguments to pass back
  127.         switch ($url['path']) {
  128.                 case '/product-category/mens-watches/':
  129.                         $queryArgs['product_cat'] = 'mens-watches';
  130.                 break;
  131.  
  132.                 case '/product-category/womens-watches/':
  133.                         $queryArgs['product_cat'] = 'womens-watches';
  134.                 break;
  135.  
  136.                 case '/mens-watches-sale/':
  137.                         $queryArgs['product_cat'] = 'mens-watches';
  138.                         $product_ids_on_sale = wc_get_product_ids_on_sale();
  139.                         $meta_query   = array();
  140.                         $meta_query[] = WC()->query->visibility_meta_query();
  141.                         $meta_query[] = WC()->query->stock_status_meta_query();
  142.                         $meta_query   = array_filter( $meta_query );
  143.  
  144.                         $queryArgs['meta_query'] = $meta_query;
  145.                         $queryArgs['post__in'] = array_merge( array( 0 ), $product_ids_on_sale );
  146.                 break;
  147.  
  148.                 case '/mens-watches-bestsellers/':
  149.                         $queryArgs['posts_per_page'] = 4;
  150.                         $queryArgs['product_cat'] = 'mens-watches';
  151.                         //$queryArgs['meta_key'] = 'total_sales';
  152.                         //$queryArgs['orderby'] = 'meta_value_num';
  153.                         //$queryArgs['meta_query'][] = array('key' => 'total_sales');
  154.  
  155.                         /*$firstFilteredArg = new WP_Query($queryArgs);
  156.                         $firstFilterPostIDs = array();
  157.                         if ($firstFilteredArg->have_posts()) {
  158.                                 while ($firstFilteredArg->have_posts()) {
  159.                                         $firstFilteredArg->the_post();
  160.                                         $firstFilterPostIDs[] = get_the_ID();
  161.                                 }
  162.                         }
  163.                         var_dump($firstFilterPostIDs);
  164.  
  165.                         $queryArgs['meta_key'] = '_price';
  166.                         $queryArgs['post__in'] = $firstFilterPostIDs;
  167.                         add_filter('posts_orderby', 'edit_bestsellers_orderby');
  168.                         add_filter('posts_join', 'edit_bestsellers_join');
  169.                         add_filter('posts_where', 'edit_bestsellers_where');
  170.                         add_filter('posts_clauses', 'edit_bestsellers_clauses');*/
  171.                 break;
  172.  
  173.                 case '/womens-watches-sale/':
  174.                         $queryArgs['product_cat'] = 'womens-watches';
  175.                         $product_ids_on_sale = wc_get_product_ids_on_sale();
  176.                         $meta_query   = array();
  177.                         $meta_query[] = WC()->query->visibility_meta_query();
  178.                         $meta_query[] = WC()->query->stock_status_meta_query();
  179.                         $meta_query   = array_filter( $meta_query );
  180.  
  181.                         $queryArgs['meta_query'] = $meta_query;
  182.                         $queryArgs['post__in'] = array_merge( array( 0 ), $product_ids_on_sale );
  183.                 break;
  184.  
  185.                 case '/womens-watches-bestsellers/':
  186.                         $queryArgs['posts_per_page'] = 10;
  187.                         $queryArgs['product_cat'] = 'womens-watches';
  188.                         $queryArgs['meta_key'] = 'total_sales';
  189.                         $queryArgs['orderby'] = 'meta_value_num';
  190.                 break;
  191.  
  192.                 case '/newest-products/':
  193.                 $queryArgs['posts_per_page'] = 10;
  194.                 $queryArgs['tax_query'][] = array(
  195.                                                                                 'taxonomy' => 'product_cat',
  196.                                                                                 'field' => 'slug',
  197.                                                                                 'terms' => 'mens-watches'
  198.                                                                                 );
  199.                 $queryArgs['tax_query'][] = array(
  200.                                                                                         'taxonomy' => 'product_cat',
  201.                                                                                         'field' => 'slug',
  202.                                                                                         'terms' => 'womens-watches'
  203.                                                                                 );
  204.                 $queryArgs['orderby'][] = array('date' => 'DESC');
  205.                 break;
  206.         }
  207.  
  208.         return $queryArgs;
  209. }
  210.  
  211. function orderby_newest_filter($orderby) {
  212.         global $wpdb;
  213.         return " {$wpdb->postmeta}.meta_value, mta.meta_value";
  214. }
  215.  
  216. add_action('wp_ajax_product_filter_ajax', 'product_filter_ajax');
  217. add_action('wp_ajax_nopriv_product_filter_ajax', 'product_filter_ajax');
  218.  
  219. /*
  220.  * Test orderby filters
  221.  */
  222. function edit_bestsellers_clauses($clauses) {
  223.         var_dump($clauses);
  224.         return $clauses;
  225. }
  226. function edit_bestsellers_where($where_statement) {
  227.         //$where_statement = substr($where_statement, 0, -1);
  228.         //$where_statement .= " AND (mt99.meta_key='total_sales'))"
  229.         //var_dump($where_statement);
  230.         return $where_statement;
  231. }
  232. function edit_bestsellers_join($join_statement) {
  233.         //$join_statement .= " INNER JOIN wp_postmeta AS mt99 ON (wp_posts.ID = mt99.post_id)"
  234.         //var_dump($join_statement);
  235.         return $join_statement;
  236. }
  237. function edit_bestsellers_orderby($orderby_statement) {
  238.         //$orderby_statement .= " , mt2.meta_value+0 DESC";
  239.         //var_dump($orderby_statement);
  240.         return $orderby_statement;
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement