强制Woocommerce按属性过滤以仅显示标题中包含搜索词的属性

时间:2019-01-25 22:20:04

标签: wordpress search woocommerce widget

我正在使用以下代码仅按产品标题进行wordpress搜索。但是,woocommerce“按属性过滤”小部件仍然显示过滤器,就像搜索也是在正文上进行的一样。

因此,例如,如果我搜索“脸部”,则所有显示的结果的标题中都带有“脸部”,但“按属性过滤”小部件仍将“巧克力”显示为属性,因为有一个巧克力条的描述为“脸部”它,我们都喜欢巧克力”

然后,如果您单击“巧克力”过滤器,它将不会显示任何结果,因为搜索仅显示标题中带有“面部”的结果。

我什至不知道从哪里开始寻找通过属性小部件更改过滤器的行为。

onItemSelected

1 个答案:

答案 0 :(得分:0)

我整理了自己的过滤器,该过滤器可以很好地进行上述搜索(仅标题),并且不显示任何品牌商品中标题中没有关键字的品牌。现在我唯一的问题是弄清楚如何获取项目数。

<?php
 $searchterm = htmlentities($_GET["s"]);
 $filter_brand = htmlentities($_GET["filter_brand"]);
 $current = $_SERVER['REQUEST_URI'];
 $site_url = home_url();
 $args = array(
                'post_type' => 'product',
                'posts_per_page' => -1,
                'order' => 'ASC',
                'orderby' => 'title',
                's' => $searchterm
            );
    // The Query
  $the_query = new WP_Query( $args );



if( $the_query->have_posts() ) :
echo'<div id="woocommerce_layered_nav-3" class="widget woocommerce widget_layered_nav woocommerce-widget-layered-nav">
<ul class="woocommerce-widget-layered-nav-list">';
$unique_singler = array();
    while ( $the_query->have_posts() ) : $the_query->the_post();
     $brand_terms = get_the_terms( $post, 'pa_brand' );
     $singler = array_values($brand_terms)[0];
     $name = $singler->name;
     $name_string = strtolower($name);
     $name_slug = str_replace(" ","-",$name_string);
// only create option if city hasn't been added yet
        if( ! in_array( $name, $unique_singler ) ) :
            // add city to array so it doesn't repeat
            $unique_singler[] = $name;
       ;?>
    <li class="woocommerce-widget-layered-nav-list__item wc-layered-nav-term <?php if (!($filter_brand == $name_slug) && !empty($filter_brand)) { echo 'hidden'; }?>">
    <?php echo '<a class="remover ';
    if ($filter_brand == $name_slug) { echo 'shown'; }
    echo '" href="'.$site_url.'/?s='.$searchterm.'&tags=1&ixwps=1&post_type=product"><i class="fas fa-times"></i></a>';
 ?><?php
    echo '<a class="dup" href="'.$current.'&filter_brand='.$name_slug.'">'.$name.'</a> <span class="count">(' .count($XXXXXXX). ')</span>';
 ?></li>
            <?php
    endif;?>                
                            <?php endwhile;?>
                </ul>       
</div>              
                            <?php else :?>
    <p>Nothing to see</p>
<?php endif; ?>

 <style>
 li.woocommerce-widget-layered-nav-list__item.wc-layered-nav-term a.remover {
    display:none;
}

li.woocommerce-widget-layered-nav-list__item.wc-layered-nav-term a.remover.shown {
    display:inline-block;
}

li.woocommerce-widget-layered-nav-list__item.wc-layered-nav-term.hidden
{
    display:none;
}

.x-sidebar .widget ul li {
    border-top: 1px solid rgba(0,0,0,0.085) !important;
}

ul.woocommerce-widget-layered-nav-list
{
     border-top: 1px solid rgba(0,0,0,0.085) !important;
     border-bottom: 1px solid rgba(0,0,0,0.085) !important;
}

.woocommerce-widget-layered-nav-list, li.woocommerce-widget-layered-nav-list__item.wc-layered-nav-term {
    margin-left:0 !important;
}

 .x-sidebar .widget ul li a {
    padding-top: 8px;
    padding-bottom: 8px;
 }
 div#woocommerce_layered_nav-3 {
    margin: 0;
}

a.remover.shown i {
    text-decoration: underline !important;
    margin-right:0.5em;
}
</style>