Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function product_filter_ajax() {
- global $wpdb, $product, $post;
- if (!isset($_REQUEST['filters'])) die('none');
- $filters = array();
- $filtersAA = array();
- $possibleTaxonomies = array('price', 'product_brand', 'pa_colour', 'pa_size', 'pa_strap', 'pa_function', 'pa_movement');
- $postCount = 0;
- // Just placing into an easier data structure
- foreach($_REQUEST['filters'] AS $filter) {
- $filters[] = parse_filter_option($filter);
- }
- foreach($filters AS $selectedFilter) {
- $filtersAA[$selectedFilter[0]] = $selectedFilter[1];
- }
- // Set variable variable so each taxonomy can be accessed via $price, $product_brand, $pa_colour etc from $possibleTaxonomies list
- foreach($filtersAA AS $key => $val) {
- $$key = array(
- 'taxonomy' => $key,
- 'field' => 'slug',
- 'terms' => $val
- );
- }
- // Add all additional filters
- foreach($possibleTaxonomies AS $taxonomy) {
- if (!isset($$taxonomy)) {
- $$taxonomy = array();
- }
- }
- if (isset($price) && !empty($price)) {
- $priceRange = parse_filter_option($price['terms'], '-');
- }
- // WP_Query thing goes here to actually make use of filter...
- // Base query arguments go here
- $queryArgs = array(
- 'post_type' => 'product',
- 'posts_per_page' => -1,
- 'post_status' => 'publish',
- 'meta_query' => array(
- array(
- 'key' => '_visibility',
- 'value' => array('catalog', 'visible'),
- 'compare' => 'IN'
- )
- ),
- 'tax_query' => array(
- )
- );
- if (count($filtersAA) > 1) {
- $queryArgs['tax_query']['relation'] = 'AND';
- };
- foreach($possibleTaxonomies AS $taxonomy) {
- // Price is different
- if ($taxonomy == 'price') continue;
- if (isset($$taxonomy) && !empty($$taxonomy)) {
- $queryArgs['tax_query'][] = $$taxonomy;
- }
- }
- if (isset($priceRange) && !empty($priceRange)) {
- $queryArgs['meta_key'] = '_price';
- $queryArgs['meta_query'] = array(
- array(
- 'key' => '_price',
- 'type' => 'NUMERIC',
- 'value' => $priceRange,
- 'compare' => 'BETWEEN'
- )
- );
- }
- if (isset($sortBy) && !empty($sortBy)) {
- $queryArgs['meta_key'] = '_price';
- $queryArgs['orderby'] = array('meta_value_num' => $sortBy['terms']);
- }
- // Add any extra modifications required by the specific page this is called on
- $queryArgs = get_page_category($_REQUEST['page'], $queryArgs);
- var_dump($queryArgs);
- $filteredQuery = new WP_Query($queryArgs);
- // Bestsellers may have applied a filter to ordering...try to check and remove it if so
- if (array_key_exists('edit_bestsellers_orderby', $GLOBALS['wp_filter'])) {
- remove_filter('posts_orderby', 'edit_bestsellers_orderby');
- }
- if (array_key_exists('edit_bestsellers_join', $GLOBALS['wp_filter'])) {
- remove_filter('posts_join', 'edit_bestsellers_join');
- }
- if (array_key_exists('edit_bestsellers_where', $GLOBALS['wp_filter'])) {
- remove_filter('posts_where', 'edit_bestsellers_where');
- }
- if ($filteredQuery->have_posts()) {
- do_action( 'woocommerce_before_shop_loop' );
- while ($filteredQuery->have_posts()) {
- $filteredQuery->the_post();
- ?>
- <?php wc_get_template_part( 'content', 'product' ); ?>
- <?php
- }
- }
- die();
- }
- function parse_filter_option($option, $character = '=') {
- $option = explode($character, $option);
- return array($option[0], $option[1]);
- }
- function get_page_category($href, $queryArgs) {
- $url = parse_url($href);
- // Sort out path here to get any special WP_Query arguments to pass back
- switch ($url['path']) {
- case '/product-category/mens-watches/':
- $queryArgs['product_cat'] = 'mens-watches';
- break;
- case '/product-category/womens-watches/':
- $queryArgs['product_cat'] = 'womens-watches';
- break;
- case '/mens-watches-sale/':
- $queryArgs['product_cat'] = 'mens-watches';
- $product_ids_on_sale = wc_get_product_ids_on_sale();
- $meta_query = array();
- $meta_query[] = WC()->query->visibility_meta_query();
- $meta_query[] = WC()->query->stock_status_meta_query();
- $meta_query = array_filter( $meta_query );
- $queryArgs['meta_query'] = $meta_query;
- $queryArgs['post__in'] = array_merge( array( 0 ), $product_ids_on_sale );
- break;
- case '/mens-watches-bestsellers/':
- $queryArgs['posts_per_page'] = 4;
- $queryArgs['product_cat'] = 'mens-watches';
- //$queryArgs['meta_key'] = 'total_sales';
- //$queryArgs['orderby'] = 'meta_value_num';
- //$queryArgs['meta_query'][] = array('key' => 'total_sales');
- /*$firstFilteredArg = new WP_Query($queryArgs);
- $firstFilterPostIDs = array();
- if ($firstFilteredArg->have_posts()) {
- while ($firstFilteredArg->have_posts()) {
- $firstFilteredArg->the_post();
- $firstFilterPostIDs[] = get_the_ID();
- }
- }
- var_dump($firstFilterPostIDs);
- $queryArgs['meta_key'] = '_price';
- $queryArgs['post__in'] = $firstFilterPostIDs;
- add_filter('posts_orderby', 'edit_bestsellers_orderby');
- add_filter('posts_join', 'edit_bestsellers_join');
- add_filter('posts_where', 'edit_bestsellers_where');
- add_filter('posts_clauses', 'edit_bestsellers_clauses');*/
- break;
- case '/womens-watches-sale/':
- $queryArgs['product_cat'] = 'womens-watches';
- $product_ids_on_sale = wc_get_product_ids_on_sale();
- $meta_query = array();
- $meta_query[] = WC()->query->visibility_meta_query();
- $meta_query[] = WC()->query->stock_status_meta_query();
- $meta_query = array_filter( $meta_query );
- $queryArgs['meta_query'] = $meta_query;
- $queryArgs['post__in'] = array_merge( array( 0 ), $product_ids_on_sale );
- break;
- case '/womens-watches-bestsellers/':
- $queryArgs['posts_per_page'] = 10;
- $queryArgs['product_cat'] = 'womens-watches';
- $queryArgs['meta_key'] = 'total_sales';
- $queryArgs['orderby'] = 'meta_value_num';
- break;
- case '/newest-products/':
- $queryArgs['posts_per_page'] = 10;
- $queryArgs['tax_query'][] = array(
- 'taxonomy' => 'product_cat',
- 'field' => 'slug',
- 'terms' => 'mens-watches'
- );
- $queryArgs['tax_query'][] = array(
- 'taxonomy' => 'product_cat',
- 'field' => 'slug',
- 'terms' => 'womens-watches'
- );
- $queryArgs['orderby'][] = array('date' => 'DESC');
- break;
- }
- return $queryArgs;
- }
- function orderby_newest_filter($orderby) {
- global $wpdb;
- return " {$wpdb->postmeta}.meta_value, mta.meta_value";
- }
- add_action('wp_ajax_product_filter_ajax', 'product_filter_ajax');
- add_action('wp_ajax_nopriv_product_filter_ajax', 'product_filter_ajax');
- /*
- * Test orderby filters
- */
- function edit_bestsellers_clauses($clauses) {
- var_dump($clauses);
- return $clauses;
- }
- function edit_bestsellers_where($where_statement) {
- //$where_statement = substr($where_statement, 0, -1);
- //$where_statement .= " AND (mt99.meta_key='total_sales'))"
- //var_dump($where_statement);
- return $where_statement;
- }
- function edit_bestsellers_join($join_statement) {
- //$join_statement .= " INNER JOIN wp_postmeta AS mt99 ON (wp_posts.ID = mt99.post_id)"
- //var_dump($join_statement);
- return $join_statement;
- }
- function edit_bestsellers_orderby($orderby_statement) {
- //$orderby_statement .= " , mt2.meta_value+0 DESC";
- //var_dump($orderby_statement);
- return $orderby_statement;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement