Created
April 28, 2020 07:46
-
-
Save kreamweb/ab860d34c66efd6f66bc013fd91d6b7a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if ( defined( 'YITH_WCAS_PREMIUM' ) ) { | |
add_action( 'ywcas_before_do_the_request', 'yith_wcas_query_fix' ); | |
function yith_wcas_query_fix() { | |
global $yith_wcas, $wp_the_query; | |
remove_filter( 'posts_where', array( $yith_wcas, 'extend_search_where' ) ); | |
remove_filter( 'posts_join', array( $yith_wcas, 'extend_search_join' ) ); | |
if ( isset($_REQUEST['action']) && 'yith_ajax_search_products' == $_REQUEST['action'] ) { | |
add_filter( 'posts_where', 'ywcas_extend_search_where' ); | |
add_filter( 'posts_join', 'ywcas_extend_search_join' ); | |
} | |
} | |
add_action( 'pre_get_posts', 'ywcas_pre_get_posts' , 15 ); | |
function ywcas_pre_get_posts() { | |
global $yith_wcas, $wp_the_query; | |
remove_filter( 'posts_join', array( $yith_wcas, 'search_post_join' ) ); | |
remove_filter( 'posts_where', array( $yith_wcas, 'search_post_where' ) ); | |
if ( ! is_admin() && ! empty( $wp_the_query->query_vars['s'] ) && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { | |
add_filter( 'posts_where', 'ywcas_extend_search_where' ); | |
add_filter( 'posts_join', 'ywcas_extend_search_join' ); | |
} | |
} | |
function ywcas_extend_search_join( $join ) { | |
global $yith_wcas; | |
global $wpdb; | |
$search_string = $yith_wcas->get_search_string(); | |
if( $search_string == ''){ | |
return $join; | |
} | |
$join = ''; | |
// YITH WooCommerce Brands Compatibility. | |
$search_by_brand = isset( $yith_wcas->search_options['search_by_brand'] ) && 'yes' === $yith_wcas->search_options['search_by_brand']; | |
if ( 'yes' === $yith_wcas->search_options['search_by_cat'] || 'yes' === $yith_wcas->search_options['search_by_tag'] || $search_by_brand || apply_filters( 'yith_wcas_search_for_taxonomy', false ) ) { | |
$join .= " LEFT JOIN {$wpdb->term_relationships} tr ON {$wpdb->posts}.ID = tr.object_id LEFT JOIN {$wpdb->term_taxonomy} tt ON tt.term_taxonomy_id=tr.term_taxonomy_id LEFT JOIN {$wpdb->terms} tm ON tm.term_id = tt.term_id"; | |
} | |
$product_visibility_term_ids = wc_get_product_visibility_term_ids(); | |
$join .= " LEFT JOIN {$wpdb->term_relationships} tr_v ON {$wpdb->posts}.ID = tr_v.object_id LEFT JOIN {$wpdb->term_taxonomy} tt_v ON ( tt_v.term_taxonomy_id=tr_v.term_taxonomy_id AND tt_v.taxonomy LIKE 'product_visibility' AND tt_v.term_taxonomy_id NOT IN ( " . $product_visibility_term_ids['exclude-from-search'] . " ) ) LEFT JOIN {$wpdb->terms} tm_v ON tm_v.term_id = tt_v.term_id"; | |
if ( '_sku' === $yith_wcas->search_options['search_by_cf'] ) { | |
$join .= " LEFT JOIN {$wpdb->wc_product_meta_lookup} as cf1 ON {$wpdb->posts}.ID = cf1.product_id "; | |
} | |
return $join; | |
} | |
function ywcas_extend_search_where( $where ) { | |
global $yith_wcas, $wpdb; | |
$search_string = $yith_wcas->get_search_string(); | |
if( $search_string == ''){ | |
return $where; | |
} | |
$terms = array(); | |
$where = ''; | |
// YITH WooCommerce Brands Compatibility. | |
if ( class_exists( 'YITH_WCBR' ) ) { | |
if ( isset( $yith_wcas->search_options['search_by_brand'] ) && 'yes' === $yith_wcas->search_options['search_by_brand'] ) { | |
if ( ! in_array( YITH_WCBR::$brands_taxonomy, $terms ) ) { | |
$terms[] = YITH_WCBR::$brands_taxonomy; | |
} | |
} | |
} | |
// YITH WooCommerce Brands Compatibility. | |
$terms = apply_filters( 'yith_wcas_search_taxonomy_terms', $terms ); | |
$reverse = false; | |
$where .= " AND {$wpdb->posts}.post_type LIKE 'product' AND ("; | |
if ( $yith_wcas->search_options['like'] ) { | |
$where .= $reverse ? '(' : ''; | |
if ( 'yes' === $yith_wcas->search_options['search_by_title'] ) { | |
$where .= " ( LOWER( {$wpdb->posts}.post_title ) LIKE '" . $search_string . "') "; | |
$where .= $reverse ? " OR ( LOWER( {$wpdb->posts}.post_title ) LIKE '" . $search_string . "') )" : ''; | |
} else { | |
$where .= " 1=2 "; | |
} | |
if ( '_sku' === $yith_wcas->search_options['search_by_cf'] ) { | |
$where .= " OR ( LOWER(cf1.sku) LIKE '{$search_string}' ) "; | |
} | |
$addor = true; | |
if ( ! empty( $terms ) ) { | |
$where .= ( $addor ) ? ' OR ' : ''; | |
$where .= " (( LOWER(tm.name) LIKE '" . $search_string . "' OR LOWER(tm.slug) LIKE '" . $search_string . "') AND tt.taxonomy IN ('" . implode( "','", $terms ) . "')) "; | |
} | |
$where .= ' ) '; | |
} else { | |
$where .= $reverse ? '(' : ''; | |
if ( 'yes' === $yith_wcas->search_options['search_by_title'] ) { | |
$where .= " ( LOWER( {$wpdb->posts}.post_title ) REGEXP '" . $search_string . "') "; | |
$where .= $reverse ? " OR ( LOWER( {$wpdb->posts}.post_title ) REGEXP '" . $yith_wcas->search_reverse_string . "') )" : ''; | |
} else { | |
$where .= " 1=2 "; | |
} | |
if ( '_sku' === $yith_wcas->search_options['search_by_cf'] ) { | |
$where .= " OR ( LOWER(cf1.sku) REGEXP '{$search_string}' ) "; | |
} | |
$addor = true; | |
if ( ! empty( $terms ) ) { | |
$where .= ( $addor ) ? ' OR ' : ''; | |
$where .= " (( LOWER(tm.name) REGEXP '" . $search_string . "' OR LOWER(tm.slug) REGEXP '" . $search_string . "') AND tt.taxonomy IN ('" . implode( "','", $terms ) . "')) "; | |
} | |
$where .= ' ) '; | |
if ( 'yes' === $yith_wcas->search_options['search_by_author'] ) { | |
$where .= " OR us.user_nicename REGEXP '" . $search_string . "' "; | |
} | |
} | |
$where = apply_filters( 'yith_wcas_search_where', $where, $search_string ); | |
return $where; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment