【问题标题】:Woocommerce - extend product search formWoocommerce - 扩展产品搜索表单
【发布时间】:2021-10-25 11:40:13
【问题描述】:

我需要扩展产品搜索表单的功能,以便能够按 SKU 搜索产品。这仅在标题和内容中搜索。不知道。

<?php 
if ( isset( $_REQUEST['product_cat'] ) && ! empty( $_REQUEST['product_cat'] ) ) {
    $optsetlect = $_REQUEST['product_cat'];
} else {
    $optsetlect = 0;  
}

$args = array(
    'show_option_all' => __( 'All categories', 'domain' ),
    'hierarchical' => 1,
    'hide_empty' => 0,
    'class' => 'cat',
    'echo' => 0,
    'value_field' => 'slug',
    'selected' => $optsetlect
);

$args['taxonomy'] = 'product_cat';
$args['name'] = 'product_cat';              
$args['class'] = 'selectpicker dropdown-select';
?>

<form role="search" method="get" action="<?php echo esc_url( home_url() ); ?>">
    <input type="hidden" name="post_type" value="product">

    <div class="input-group">
        <input id="woocommerce-product-search-field-<?php echo isset( $index ) ? absint( $index ) : 0; ?>" name="s" placeholder="Search products" type="text" autocomplete="off">

        <div class="input-group-append">

            <?php echo wp_dropdown_categories( $args ); ?>

            <button class="btn btn-primary" type="submit">
                <span class="ec ec-search"></span>
            </button>
        </div>
    </div>
</form>

【问题讨论】:

    标签: woocommerce


    【解决方案1】:

    你应该尝试这样的事情:

    add_filter( 'get_product_search_form' , 'woo_custom_product_searchform' );
    function woo_custom_product_searchform( $form ) {
        $form = '<form role="search" method="get" id="searchform" action="' . esc_url( home_url( '/'  ) ) . '">
        <div>
            <label class="screen-reader-text" for="s">' . __( 'Search for:', 'woocommerce' ) . '</label>
            <input type="text" value="' . get_search_query() . '" name="s" id="s" placeholder="' . __( 'My Search form', 'woocommerce' ) . '" />
            <input type="submit" id="searchsubmit" value="'. esc_attr__( 'Search', 'woocommerce' ) .'" />
            <input type="hidden" name="post_type" value="product" />
        </div>
        </form>';
    return $form;
    }
    

    【讨论】:

      猜你喜欢
      • 2022-01-19
      • 2016-01-06
      • 1970-01-01
      • 2017-05-02
      • 1970-01-01
      • 2018-08-28
      • 2016-09-26
      • 2018-09-05
      • 2020-12-07
      相关资源
      最近更新 更多