A
A
anriko2020-12-30 11:57:25
WooCommerce
anriko, 2020-12-30 11:57:25

How to remove category from woocommerce search?

5e09bbeb64bae966375450.png

код который выводит категории
<?php
            if (1 === $atts['show-cat']) :
                $cats = get_terms([
                    'hide_empty' => true,
                    'taxonomy' => 'product_cat'
                ]);
                if ($cats && !is_wp_error($cats)) :
                    ?>
                    <div class="wrap-list-cat-search">
                        <select class="zoo-product-cat-options" name="zoo-search-box-product-cat">
                            <option value="all"><?php esc_html_e('All Categories', 'cerato') ?></option>
                            <?php
                            foreach ($cats as $cat) {
                                ?>
                                <option value="<?php echo esc_attr($cat->term_id); ?>"><?php echo esc_html($cat->name); ?></option>
                                <?php
                            } ?>
                        </select>
                        <i class="zoo-icon-down"></i>
                        <span class="label-cat-search"><?php esc_html_e('All Categories', 'cerato') ?></span>
                    </div>
                <?php
                endif;
            endif;
            ?>

I installed the plugin there, I can’t understand such a function, maybe you can transfer functuin.php and delete it
function mwd_hwcosp_comma_separated_to_array($string, $separator = ',')
  {
    //Explode on comma
    $vals = explode($separator, $string);
   
    //Trim whitespace
    foreach($vals as $key => $val) {
      $vals[$key] = trim($val);
    }
    //Return empty array if no items found
    //http://php.net/manual/en/function.explode.php#114273
    return array_diff($vals, array(""));
  }
add_filter( 'get_terms', 'mwd_hwcosp_get_subcategory_terms', 10, 3 );

  function mwd_hwcosp_get_subcategory_terms( $terms, $taxonomies, $args ) {
    
    //$data = array();
    $opt_terms = get_option('hwcosp_global');
    
    $data = mwd_hwcosp_comma_separated_to_array($opt_terms);
    
    $new_terms = array();
  
    // if a product category and on the shop page
    // to hide from shop page, replace is_page('YOUR_PAGE_SLUG') with is_shop()
    //if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() && is_page('YOUR_PAGE_SLUG') ) {
    
    // Test some variations user may have for their shop
    // ToDo make a dropdown select of current pages
    
    // If you need a different page just uncomment out remove // in front of $mwd_opt4 and /* */
    // Then insert your page slug
    
    $mwd_opt1 = in_array( 'product_cat', $taxonomies ) && ! is_admin() && is_shop();
    $mwd_opt2 = in_array( 'product_cat', $taxonomies ) && ! is_admin() && is_front_page();
    $mwd_opt3 = in_array( 'product_cat', $taxonomies ) && ! is_admin() && is_home();
    //$mwd_opt4 = in_array( 'product_cat', $taxonomies ) && ! is_admin() && is_page('YOUR_PAGE_SLUG'),
    
      if ( $mwd_opt1 || $mwd_opt2 || $mwd_opt3 /*|| $mwd_opt4*/ ) {
      
        foreach ( $terms as $key => $term ) {
          
          if ( ! in_array( $term->slug, $data ) ) {
            
            $new_terms[] = $term;
          }
        }
        
        $terms = $new_terms;
      }
    
    return $terms;
    
  }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question