在woocommerce中覆盖产品搜索代码并不起作用

时间:2013-09-16 12:33:26

标签: php wordpress search woocommerce product

我正在尝试修改产品搜索表单以包含3个“选择字段”而不是默认文本框。根据所选的选项(选项是从我添加到每个产品的自定义属性中填充的),搜索应该返回相关产品。 以下是我的“product-searchform.php”代码,我将其置于mystile主题文件夹中以覆盖默认搜索。我收到所有帖子(产品)而不是具体的。所以某处我的搜索不对劲。另外get_search_query()函数的代码在哪里?

<form role="search" method="get" id="searchform" action="<?php echo esc_url( home_url( '/'  ) ); ?>">
<div>
            <!-- Default Markup
            <label class="screen-reader-text" for="s"><?php _e( 'Search for:', 'woocommerce' ); ?></label>
            <input type="text" value="<?php echo get_search_query(); ?>" name="s" id="s" placeholder="<?php _e( 'Search for products', 'woocommerce' ); ?>" />
            <input type="submit" id="searchsubmit" value="<?php echo esc_attr__( 'Search' ); ?>" />
            <input type="hidden" name="post_type" value="product" />
            */-->
<?php 
$terms = get_terms("pa_country");
$count = count($terms);
if($count > 0){
    echo '<select class="select" id="destination" tabindex="11" name="destination">';
    echo '<option value="Destination">--Destination--</option>';
    foreach($terms as $term ){
        echo '<option value="' . $term->name . '">' . $term->name . '</option>';
    }
    echo '</select>';
}

$terms = get_terms("pa_days");
$count = count($terms);
if($count > 0){
    echo '<select class="select" id="days" tabindex="11" name="days">';
    echo '<option value="Days">--Days--</option>';
    foreach($terms as $term ){
        echo '<option value="' . $term->name . '">' . $term->name . '</option>';
    }
    echo '</select>';
}

$terms = get_terms("pa_budget");
$count = count($terms);
if($count > 0){
    echo '<select class="select" id="budget" tabindex="11" name="budget">';
    echo '<option value="Budget">--Budget--</option>';
    foreach($terms as $term ){
        echo '<option value="' . $term->name . '">' . $term->name . '</option>';
    }
    echo '</select>';
}
?>
<input type="submit" id="searchsubmit" value="<?php echo esc_attr__( 'Search' ); ?>" />
<input type="hidden" name="post_type" value="product" />
</div>

2 个答案:

答案 0 :(得分:0)

看看这篇文章它可能对你有所帮助 10 Useful WordPress Search Code Snippets

答案 1 :(得分:0)

<form role="search" method="get" class="woocommerce-product-search" action="<?php
echo esc_url(home_url('/'));
?>">
    <label class="screen-reader-text" for="s"><?php
_e('Search for:', 'woocommerce');
?></label>
    <input type="search" class="search-field" placeholder="<?php
echo esc_attr_x('Search Products&hellip;', 'placeholder', 'woocommerce');
?>" value="<?php
echo get_search_query();
?>" name="s" title="<?php
echo esc_attr_x('Search for:', 'label', 'woocommerce');
?>" />


    <!-- Dropdown Product Category -->

        <?php
if (class_exists('WooCommerce')):
?>
  <?php
    if (isset($_REQUEST['product_cat']) && !empty($_REQUEST['product_cat'])) {
        $optsetlect = $_REQUEST['product_cat'];
    } else {
        $optsetlect = 0;
    }
    $args             = array(
        'show_option_all' => esc_html__('All Categories', 'woocommerce'),
        'hierarchical' => 1,
        'class' => 'cat',
        'echo' => 1,
        'value_field' => 'slug',
        'selected' => $optsetlect
    );
    $args['taxonomy'] = 'product_cat';
    $args['name']     = 'product_cat';
    $args['class']    = 'cate-dropdown hidden-xs';
    wp_dropdown_categories($args);

?>

<?php
endif;
?>
<?php
$terms = get_terms("pa_color");
$count = count($terms);
if ($count > 0) {
    echo '<select class="select" id="brands" tabindex="11" name="pa_color">';
    echo '<option value="brand">--Brand--</option>';
    foreach ($terms as $term) {
        echo '<option value="' . $term->slug . '">' . $term->slug . '</option>';
    }
    echo '</select>';
}
?>

    <input type="submit" value="<?php
echo esc_attr_x('Search', 'submit button', 'woocommerce');
?>" />
    <input type="hidden" name="post_type" value="product" />
</form>