我开发了WooCommerce中的属性搜索,一切似乎都没问题。但是一个属性在搜索中不起作用。我发现的唯一区别是字母属性,其他属于数字。
例如,this URL正确地返回结果,但是当我通过vehiculo( pa_vehiculo )搜索时,会返回所有产品。
为什么会这样?
这是代码,一个小部件的插件:
// The filter for some categories
function pf_get_filter_options( $title, $taxonomy ) {
global $wp_query;
$options = '' . $title . '';
$data = get_terms( $taxonomy );
foreach ( $data as $term ) {
// Skip some weird terms
switch ( $taxonomy ) {
case 'product_cat':
if( in_array($term->slug, array('promociones')) )
continue( 2 );
break;
}
$selected = isset( $wp_query->query_vars[$taxonomy] ) && $wp_query->query_vars[$taxonomy] == $term->slug;
$options .= '<option value="' . $term->slug . '"' . $selected . '>' . $term->slug . '</option>';
}
return $options;
}
// the front-end display and the script for change search by categories because have different attributes
/**
* Front-end display of widget.
*
* @see WP_Widget::widget()
*
* @param array $args Widget arguments.
* @param array $instance Saved values from database.
*/
public function widget( $args, $instance ) {
echo $args['before_widget'];
if ( ! empty( $instance['title'] ) ) {
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'];
}
?>
<form id="form-neumaticos" action="<?php echo site_url( '/' ); ?>" class="search2" style="display:block">
<input type="hidden" name="s" value=""/>
<input type="hidden" name="post_type" value="product"/>
<input type="hidden" name="product_cat" value="neumaticos"/>
<div class="search_box">
<div class="select-categories-content-neumatic">
<label>TIPO</label>
<select id="select-categories-neumaticos" class="selectBox" name="categories" placeholder="Seleccione">
<option selected>neumaticos</option>
<option>llantas</option>
<option>accesorios</option>
<!-- <?php echo pf_get_filter_options( 'category-name', 'product_cat' ); ?> -->
</select>
</div>
<div id="select-neumaticos">
<div class="select-categories-content">
<label>VEHICULO</label>
<select class="selectBox selectneumaticos" name="pa_vehiculo">
<option value="">Seleccione</option>
<?php echo pf_get_filter_options( 'vehiculo', 'pa_vehiculo' ); ?>
</select>
</div>
<div class="select-categories-content">
<label>ANCHO</label>
<select class="selectBox selectneumaticos" name="pa_ancho">
<option value="">Seleccione</option>
<?php echo pf_get_filter_options( 'ancho', 'pa_ancho' ); ?>
</select>
</div>
<div class="select-categories-content">
<label>TALON</label>
<select class="selectBox selectneumaticos" name="pa_talon">
<option value="">Seleccione</option>
<?php echo pf_get_filter_options( 'talón', 'pa_talon' ); ?>
</select>
</div>
<div class="select-categories-content">
<label>RODADO</label>
<select class="selectBox selectneumaticos" name="pa_rodado">
<option value="">Seleccione</option>
<?php echo pf_get_filter_options( 'rodado', 'pa_rodado' ); ?>
</select>
</div>
</div>
<button class="search-button" type="submit"><i class="fa fa-search" aria-hidden="true"></i></button>
</div>
</form>
<form id="form-llantas" action="<?php echo site_url( '/' ); ?>" class="search2" style="display:none;">
<input type="hidden" name="s" value=""/>
<input type="hidden" name="post_type" value="product"/>
<input type="hidden" name="product_cat" value="llantas"/>
<div class="search_box">
<div class="select-categories-content">
<label>TIPO</label>
<select id="select-categories-llantas" class="selectBox" name="categories">
<option >neumaticos</option>
<option selected>llantas</option>
<option>accesorios</option>
<!-- <?php echo pf_get_filter_options( 'category-name', 'product_cat' ); ?> -->
</select>
</div>
<div id="select-llantas">
<div class="select-categories-content">
<label>VEHICULO</label>
<select class="selectBox selectllantas" name="pa_vehiculo">
<option value="">Seleccione</option>
<?php echo pf_get_filter_options( 'vehiculo', 'pa_vehiculo' ); ?>
</select>
</div>
<div class="select-categories-content">
<label>RODADO</label>
<select class="selectBox selectllantas" name="pa_rodado">
<option value="">Seleccione</option>
<?php echo pf_get_filter_options( 'rodado', 'pa_rodado' ); ?>
</select>
</div>
<div class="select-categories-content">
<label>AGUJEROS</label>
<select class="selectBox selectllantas" name="pa_agujeros">
<option value="">Seleccione</option>
<?php echo pf_get_filter_options( 'agujeros', 'pa_agujeros' ); ?>
</select>
</div>
<div class="select-categories-content">
<label>PCD</label>
<select class="selectBox selectllantas" name="pa_pcd">
<option value="">Seleccione</option>
<?php echo pf_get_filter_options( 'pcd', 'pa_pcd' ); ?>
</select>
</div>
</div>
<button class="search-button" type="submit"><i class="fa fa-search" aria-hidden="true"></i></button>
</div>
</form>
<?php
echo $args['after_widget'];
?>
<script>
jQuery.noConflict()(function ($){
$(document).ready(function(){
$('#select-categories-neumaticos').on('change', function() {
if ( this.value == 'neumaticos')
{
$("#form-llantas").hide();
$("#form-neumaticos").show();
}
if ( this.value == 'llantas')
{
$("#form-llantas").show();
$("#form-neumaticos").hide();
}
})
$('#select-categories-llantas').on('change', function(){
if ( this.value == 'llantas');
{
$("#form-neumaticos").hide();
$("#form-llantas").show();
}
if ( this.value == 'neumaticos')
{
$("#form-neumaticos").show();
$("#form-llantas").hide();
}
})
$('#select-categories-neumaticos').on('change', function(){
if (this.value == 'accesorios')
{
window.location = 'http://neumaticos.7vidas.com.ar/index.php/categoria-producto/accesorios/';
}
});
$('#select-categories-llantas').on('change', function(){
if (this.value == 'accesorios')
{
window.location = 'http://localhost/neumaticos/index.php/categoria-producto/accesorios/';
}
});
});
});
</script>
<?php
}
除了属性 vehiculo 之外,其他所有内容都有效,它会返回所有结果。
答案 0 :(得分:0)
我找到了解决方案!对于按属性搜索,似乎必须选中“如果您希望此属性在商店中包含产品档案,请启用此功能”。在属性中。 我查了一下,搜索工作正常。
感谢Raunak的帮助!h