【问题标题】:Overriden product search code in woocommerce doesn't workwoocommerce中覆盖的产品搜索代码不起作用
【发布时间】:2013-09-16 12:33:25
【问题描述】:

我正在尝试修改产品搜索表单以包含 3 个“选择字段”而不是默认文本框。根据选择的选项(这些选项是从我添加到每个产品的自定义属性中填充的),搜索应该返回给我相关的产品。 以下是我放置在 mystile 主题文件夹中以覆盖默认搜索的“product-searchform.php”代码。我得到所有的帖子(产品)而不是具体的。所以在某个地方我的搜索不正确。还有 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>

【问题讨论】:

    标签: php wordpress search woocommerce product


    【解决方案1】:
    <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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-18
      • 2013-09-06
      • 2020-10-18
      • 2016-08-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多