Woocommerce搜索基于具有自定义过滤器的类别

时间:2016-07-18 09:22:08

标签: php jquery ajax wordpress woocommerce

在我的WooCommerce网站的主页上,我希望用户/客户可以使用他们的邮政编码进行搜索并显示为结果,以及他们可以订购的匹配类别。

屏幕截图:我的主页带有搜索表单

enter image description here

显示搜索表单代码的模板:

  <?php
  /*The template for displaying the search form in */
  ?>
  <!-- <?php echo get_permalink( woocommerce_get_page_id( 'shop' ) ); ?> -->
  <form role="search" method="get" class="search-form">
    <?php
      if ( function_exists( 'woocommerce_product_search' ) ) {
        echo woocommerce_product_search( array( 'limit' => 40 ) );
      }
    ?>
    <div class="col-lg-4 col-lg-offset-4">
      <div class="input-group">
        <input type='textbox' id="item-search" name="s" class="form-control form-inline" value="<?php the_search_query();?>" placeholder="Enter your postcode e.g. SW1Y4LG"/>
        <span class="input-group-btn">
          <button class="item-search-btn">search</button>
        </span>
      </div>
    </div>
  </form>

搜索结果的屏幕截图(我想要的)

enter image description here

为此,我在后端WooCommerce类别页面上创建了一个自定义字段,其中我为每个类别(与我的产品相关)指定匹配的逗号分隔的邮政编码

WooCommerce后端类别页面中&#34;发布代码&#34; 自定义字段的屏幕截图:

enter image description here

以下是我在 function.php 文件中使用的代码:

function custom_product_taxonomy_add_new_meta_field() { //add term page
//this will add the custom meta field to the add new term page
  ?>
  <div class="form-field">
    <label for="term_meta[custom_term_meta]"><?php _e( 'Post Code(s)' ); ?></label>
    <input type="text" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="">
    <p class="description"><?php _e( 'Enter the delivery post codes available for this store.','my-text-domain' ); ?></p>
  </div>
<?php
}
add_action( 'product_cat_add_form_fields', 'custom_product_taxonomy_add_new_meta_field', 10, 2 );


//edit term page
function custom_product_taxonomy_edit_meta_field($term) {
//put the term ID into a variable
  $t_id = $term->term_id;
  //retrieve the existing value(s) for this meta field. This returns an array
  $term_meta = get_option( "taxonomy_$t_id" ); ?>
  <tr class="form-field">
  <th scope="row" valign="top"><label for="term_meta[custom_term_meta]"><?php _e( 'Post Code(s)' ); ?></label></th>
    <td>
      <input type="text" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="<?php echo esc_attr( $term_meta['custom_term_meta'] ) ? esc_attr( $term_meta['custom_term_meta'] ) : ''; ?>">
      <p class="description"><?php _e( 'Enter the delivery post codes available for this store.' ); ?></p>
    </td>
  </tr>
<?php
}
add_action( 'product_cat_edit_form_fields', 'custom_product_taxonomy_edit_meta_field', 10, 2 );


//save extra taxonomy fields callback function.
function save_taxonomy_custom_meta( $term_id ) {
  if ( isset( $_POST['term_meta'] ) ) {
      $t_id = $term_id;
      $term_meta = get_option( "taxonomy_$t_id" );
      $cat_keys = array_keys( $_POST['term_meta'] );
      foreach ( $cat_keys as $key ) {
          if ( isset ( $_POST['term_meta'][$key] ) ) {
              $term_meta[$key] = $_POST['term_meta'][$key];
          }
      }
      update_option( "taxonomy_$t_id", $term_meta ); //save the option array.
  }
}
add_action( 'edited_product_cat', 'save_taxonomy_custom_meta', 10, 2 );
add_action( 'create_product_cat', 'save_taxonomy_custom_meta', 10, 2 );

是否有人可以在搜索表单中添加哪些内容来实现此限制类别搜索?

可能需要一个能够执行ajax功能的新模板页面吗?

不完全确定,但非常欢迎所有建议和答案。

感谢。

0 个答案:

没有答案