Skip to main content

Custom Search Page with search by location Wordpress



Step 1

First create a form into header file or where you want to show your form
ex(header.php) or you can create a custom search form in wordpress like this.
<form method="get" id="advanced-searchform" role="search" action="<?php echo esc_url( home_url( '/' ) ); ?>">  
    <input type="hidden" name="search" value="advanced">
    <select name="citysearch" id="search" class="themebtnsearch">
        <option value="City1"><?php _e( 'City1', 'textdomain' ); ?></option>
        <option value="City2"><?php  _e( 'City2', 'textdomain' ); ?></option>
    </select>
    <input type="text" value="" placeholder="<?php _e( 'Type the Product Name', 'textdomain' ); ?>" name="productname" class="home_searchbox" />

 

    <input type="submit" id="searchsubmit" value="Search" />

</form>


Step 2 -
then to add a custom field in your woocommerce seller registration add these functions into your function.php


add_action( 'register_form', 'adding_custom_registration_fields' );
function adding_custom_registration_fields( ) {

    //lets make the field required so that i can show you how to validate it later;
    $city = empty( $_POST['city'] ) ? '' : $_POST['city'];
   
    ?>
    <div class="form-row form-row-wide">
        <label for="reg_city"><?php _e( 'city', 'woocommerce' ) ?><span class="required">*</span></label>
        <input type="text" class="input-text" name="city" id="reg_city" size="30" value="<?php echo esc_attr( $city ) ?>" />
    </div>
    <?php
}



//Updating use meta after registration successful registration
add_action('woocommerce_created_customer','adding_extra_reg_fields');

function adding_extra_reg_fields($user_id) {
    extract($_POST);
    update_user_meta($user_id, 'city', $city);
   
    update_user_meta($user_id, 'billing_city', $city);
    update_user_meta($user_id, 'shipping_city', $city);
   
}



function wpse_load_custom_search_template(){
    if( isset($_REQUEST['search']) == 'advanced' ) {
        require('advanced-searchresult.php');
        die();
    }
}
add_action('init','wpse_load_custom_search_template');


Step 3 Now  create  a Advance search  result page

<?php
/*
Template Name: Search Page
*/
?>

<?php get_header(); ?>


 <div id="wrap">
  <div id="content">
  <div class="clearfix"></div>
 
  <?php
// Get data from URL into variables
$_name = $_GET['productname'] != '' ? $_GET['productname'] : '';
$seller_city = $_GET['citysearch'] != '' ? $_GET['citysearch'] : '';
//$_model = $_GET['sortby'] != '' ? $_GET['sortby'] : '';
//$_orderby = $_GET['orderby'] != '' ? $_GET['orderby'] : '';

echo "<h3 class='heading-head'> Search Reslts for : $_name</h3>";

global $wpdb;
global $post;
global $wp_query;
global $seller;
// Pagination Setup
/*
$posts_per_page = 20;
$start = 0;
$paged = get_query_var( 'paged') ? get_query_var( 'paged', 1 ) : 1; // Current page number
$start = ($paged-1)*$posts_per_page;
*/

$search_query = "SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.post_title LIKE '%$_name%' OR wp_posts.post_content LIKE '%$_name%' AND wp_posts.post_type = 'product' AND wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private'";
//echo $search_query;
/*
if {
$search_query = "SELECT SQL_CALC_FOUND_ROWS wplc_posts.ID FROM wplc_posts INNER JOIN wplc_postmeta ON ( wplc_posts.ID = wplc_postmeta.post_id and wplc_postmeta.meta_key='price' ) INNER JOIN wplc_postmeta AS mt1 ON ( wplc_posts.ID = mt1.post_id and wplc_postmeta.meta_key='price' ) WHERE 1=1 AND (wplc_posts.post_title LIKE '%$_name%' OR wplc_posts.post_content LIKE '%$_name%') OR ( wplc_postmeta.meta_key = 'price' AND ( ( ( mt1.meta_key = 'price' AND CAST(mt1.meta_value AS CHAR) LIKE '%$_name%' ) OR ( mt1.meta_key = 'manufacturer' AND CAST(mt1.meta_value AS CHAR) LIKE '%$_name%' ) ) ) ) AND wplc_posts.post_type = 'product' AND (wplc_posts.post_status = 'publish' OR wplc_posts.post_status = 'private') GROUP BY wplc_posts.ID ORDER BY wplc_posts.post_title $_orderby ";

}

 */

 function get_url_var($name)
{
    $strURL = $_SERVER['REQUEST_URI'];
    $arrVals = split("/",$strURL);
    $found = 0;
    foreach ($arrVals as $index => $value)
    {
        if($value == $name) $found = $index;
    }
    $place = $found + 1;
    return $arrVals[$place];
}

$page = get_url_var('page');
if(is_numeric($page))
{
    $countpage=$page;
}else { $countpage = 1;  }

 // ends here

 //next pagination starts

  $total_record = count($wpdb->get_results($search_query, ARRAY_A));
    $paged      = !empty($countpage) ? $countpage: 1;
    $post_per_page  = get_option('posts_per_page');
    $offset         = ($paged - 1)*$post_per_page;
    $max_num_pages  = ceil($total_record/ $post_per_page);
    $pagenew = get_query_var('page') ? get_query_var('page') : 1;


//echo "<pre>"; echo "paged = $paged"; echo "post_per_page = $post_per_page"; echo "offset $offset"; echo "max_num = $max_num_pages"; echo "pagenew = $pagenew";


//echo "<pre>"; print_r($page);
//paged = 1post_per_page = 10offset 0max_num = 5

    $wp_query->found_posts = $total_record;
    // number of pages
    $wp_query->max_num_pages = $max_num_pages;

    $limit_query    =   " LIMIT ".$post_per_page." OFFSET ".$offset;   

//echo $search_query.$limit_query;
   // $result = $wpdb->get_results($search_query.$limit_query,OBJECT);// return OBJECT
  $result = $wpdb->get_results($search_query,OBJECT);// return OBJECT
   if($result){
     ?><div class="contain_all_search_data"> <div class="int_store_container"> <?php
 
    $is_result_none=0;
    foreach ($result as $rowpost):
      //setup_postdata($post);
                
$row_pro_id = $rowpost->ID;
$row_pro_author = $rowpost->post_author;

$author     = get_user_by( 'id', $row_pro_author );
$authorname = $author->user_nicename; //echo "<pre>"; print_r($author);
  $user_id = $row_pro_author;
  $key = 'city';
  $single = true;
  $user_last = get_user_meta( $user_id, $key, $single );
if($user_last==$seller_city)
{
$is_result_none=1;
$price="";
$sale_price="";
$price_array = get_post_meta( $row_pro_id, '_regular_price');
$sale_price_array = get_post_meta( $row_pro_id, '_sale_price');
if($price_array!="") { $price = $price_array[0]; } else { $price=""; }
if($sale_price_array!="") {$sale_price = $sale_price_array[0]; } else { $sale_price=""; }
   
 $row_post_details = get_post($row_pro_id); //echo "<pre>"; print_r($row_post_details);
 $post_pro_title = $row_post_details->post_title;
 $thumb=""; $url="";
  $thumb = get_the_post_thumbnail( $row_pro_id, 'thumbnail' );



     if (strlen($post_pro_title) > 35) {
      $product_title = substr($post_pro_title, 0, 35);
     $product_title = substr($product_title, 0, strrpos($product_title, ' ')) . " ...";
   } else {
     $product_title = $post_pro_title;
   }

 //echo "<p>$row_pro_id</p>";
 ?>
 <div class="col-sm-3 product_wrapper">
      <div>
       <div class="product_image_container">
                            <a title="<?php echo $product_title; ?>" href="<?php echo $row_post_details->guid; ?>" class="product_thumb_link" id="id-<?php echo $row_pro_id; ?>">
                                <?php echo $thumb; ?>                          
                            </a>
                        </div>
      
      </div>
      <div class="title_wrapper">
        <a class="nodecor" href="<?php echo $row_post_details->guid; ?>" rel="bookmark" title="Permanent Link to <?php echo $product_title; ?> "><?php echo $product_title; ?></a>
        <div class="manu_title"><?php if(!empty($meta_manufacturer)) { echo $meta_manufacturer; } ?></div>
      </div>
      <p class="price_wrapper"> <?php if($price!="" && $sale_price!="" && $sale_price < $price){ echo "<b>$sale_price</b>&nbsp;&nbsp;<span style='text-decoration: line-through;'>$price</span>"; } else { echo $price; } ?></p>
      <p class="sellername"><?php echo $authorname; ?></p>
      <p class="add_tocart_cls"><a class="button product_type_simple add_to_cart_button ajax_add_to_cart" data-product_sku="" data-product_id="<?php echo $row_pro_id;  ?>" data-quantity="1" href="/?add-to-cart=<?php echo $row_pro_id;  ?>" rel="nofollow">Add to cart</a></p>
  </div>
  <?php } ?>        
          
        <?php
        endforeach;
        ?>
        <?php if($is_result_none==0) { echo "<h3>404 Nothing here. Sorry.</h3>"; } ?>
    </div></div>
       <div class="navigation" style="margin-bottom:20px;"><?php wp_pagenavi(); wp_reset_query(); ?></div>
        <?php
 




 //next pagination ends here




 } else{
?>
 <h3>404 Nothing here. Sorry.</h3>


<?php } ?>
 
   
   
</div>


</div>   
        <?php //get_sidebar(); ?>
 </div><!--/wrap-->
<?php get_footer(); ?>





Comments

Post a Comment

Popular posts from this blog

Run and compile sass scss file to css using node

  Today we learn how to use scss and generate css using node  or Run and compile sass scss file to css using node   So please follow simple  steps :-   Today we will create a project that can read scss file and generates css with it  Note: Make sure you have installed node in your system. If you want to help to install node js based on your system then check our other tutorial or check node js official website. Now create a blank folder and open  terminal(linux) or cmd(windows) and navigate to your current project folder by using cd command Now run below command npm init after enter it will ask you some package info that you can fill according to you or just keep enter until it finished. The above command will generate package.json file Now  we will install npm module that will convert our scss to css Run below command: npm install node-sass So we have installed node-sass package . Now open package.json file in your...

How to retrieve Facebook Likes, share , comment Counts

function facebook_count($url){     // Query in FQL     $fql  = "SELECT share_count, like_count, comment_count ";     $fql .= " FROM link_stat WHERE url = '$url'";     $fqlURL = "https://api.facebook.com/method/fql.query?format=json&query=" . urlencode($fql);     // Facebook Response is in JSON     $response = file_get_contents($fqlURL);     return json_decode($response); } $fb = facebook_count('https://www.facebook.com/BahutHoGyiPadhai'); // facebook share count echo $fb[0]->share_count;  echo "like"; // facebook like count echo $fb[0]->like_count ; echo "comment"; // facebook comment count echo $fb[0]->comment_count;  ?>

Solution-windows 'expo' is not recognized as an internal or external command

Solution for expo is not recognized as an internal or external command,operable program or batch file in Windows 10 Sometimes expo will not work globally mostly in windows 10, If you are facing same issue then follow the below Steps 1) Click on windows button and search for  " Environment variables"  and click on "Edit the system environment variables" 2) Now you will see a popup like below screen. Then you need to click on Environment Variables. (Please see highlight part in below image)     3)Then click on new button that i have highlighted in below image 4. Then a popup will open and you need to fill details like below mentioned Variable Name :Path Variable Value: %USERPROFILE%\AppData\Roaming\npm Here we are creating a new path variable and passing location of npm.   Now Click on OK and close all the terminal windows and open new CMD or terminal and type Expo . Great now you can access expo from any...