Advertisement
Guest User

Untitled

a guest
Nov 16th, 2014
693
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.89 KB | None | 0 0
  1. <?php
  2.  
  3. add_filter('et_search_get_result','ae_search_get_result');
  4. function ae_search_get_result($query) {
  5.     if ($query->query['post_type'] != 'product')
  6.         return $query;
  7.  
  8.     $posts_with_tags = search_by_product_tag($query->posts,$query->query['s']);
  9.     foreach ($posts_with_tags as $post) {
  10.         $query->posts[] = $post;
  11.  
  12.     }
  13.     $query->query_vars['no_found_rows'] = 3;
  14.     $query->post_count = 3;
  15.     //var_dump($query);
  16.     return $query;
  17. }
  18.  
  19.  
  20. add_filter('the_posts', 'search_by_product_tag');
  21.  
  22. function search_by_product_tag($posts, $query = '') {
  23.     if (is_search()) {
  24.         $tags = explode(',', get_search_query());
  25.     }
  26.     if ($query != '') {
  27.         $tags = explode(',', $query);
  28.     }
  29.     if (!$tags)
  30.         return $posts;
  31.  
  32.     foreach($tags as $tag)
  33.     {
  34.         //Ignore already found posts from query..
  35.         $ignoreIds = array(0);
  36.         foreach($posts as $post)
  37.         {
  38.             $ignoreIds[] = $post->ID;
  39.         }
  40.  
  41.         $matchedTags = get_post_by_tag(trim($tag), $ignoreIds);
  42.         //var_dump($ignoreIds,$matchedTags);
  43.         //die();
  44.         if ($matchedTags)
  45.         {
  46.             foreach($matchedTags as $product_id)
  47.             {
  48.                 $posts[] = get_post($product_id->post_id);
  49.             }
  50.  
  51.         }
  52.     }
  53.  
  54.     //var_dump($posts);
  55.     return $posts;
  56.  
  57. }
  58.  
  59. function get_post_by_tag($tag, $ignoreIds) {
  60.     //Check for
  61.     global $wpdb, $wp_query;
  62.  
  63.     //$ordering_args = WC()->query->get_catalog_ordering_args( $orderby, $order );
  64.  
  65.  
  66.     /**
  67.      * Special code for removing duplicates if WPML plugin is installed
  68.      * Its popular translation plugin that causes lots of product duplicate products that have different product ids
  69.      * We need to check the wpml meta data of the product to work out the duplicates
  70.      */
  71.  
  72.     $wmplEnabled = false;
  73.  
  74.     if(defined('WPML_TM_VERSION') && defined('WPML_ST_VERSION') && class_exists("woocommerce_wpml")){
  75.         $wmplEnabled = true;
  76.         //What language should we search for...
  77.         $languageCode = ICL_LANGUAGE_CODE;
  78.     }
  79.     //  $wmplEnabled = false;
  80.     $ignoreIdsForMySql = implode(",", $ignoreIds);
  81.     //var_dump($ignoreIdsForMySql);
  82.     $query = "
  83.            select p.ID as post_id from $wpdb->terms t
  84.            join $wpdb->term_taxonomy tt
  85.            on t.term_id = tt.term_id
  86.            join $wpdb->term_relationships tr
  87.            on tt.term_taxonomy_id = tr.term_taxonomy_id
  88.            join $wpdb->posts p
  89.            on p.ID = tr.object_id
  90.            join $wpdb->postmeta visibility
  91.            on p.ID = visibility.post_id
  92.            and visibility.meta_key = '_visibility'
  93.            and visibility.meta_value <> 'hidden'
  94.            ";
  95.  
  96.     //IF WPML Plugin is enabled join and get correct language product.
  97.     if($wmplEnabled)
  98.     {
  99.         $query .=
  100.             "join ".$wpdb->prefix."icl_translations trans on
  101.         trans.element_id = p.ID
  102.         and trans.element_type = 'post_product'
  103.         and trans.language_code = '$languageCode'";
  104.         ;
  105.     }
  106.  
  107.  
  108.     $query .= "
  109.            WHERE
  110.            tt.taxonomy = 'product_tag' and
  111.            t.name LIKE '%$tag%'
  112.            and p.post_status = 'publish'
  113.            and p.post_type = 'product'
  114.            and (p.post_parent = 0 or p.post_parent is null)
  115.            and p.ID not in ($ignoreIdsForMySql)
  116.            group by p.ID
  117.             ;
  118. ";
  119.  
  120.     //Search for the sku of a variation and return the parent.
  121.     $matchedProducts = $wpdb->get_results($query) ;
  122.  
  123.     //var_dump($matchedProducts);
  124.     if(is_array($matchedProducts) && !empty($matchedProducts))
  125.     {
  126.         //var_dump($matchedProducts);
  127.         $wp_query->found_posts += sizeof($matchedProducts);
  128.         //var_dump($wp_query->found_posts, sizeof($matchedProducts));
  129.         return $matchedProducts;
  130.  
  131.     }
  132.  
  133.     //return get_post($product_id);
  134.  
  135.     return null;
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement