שורטקוד חיפוש מוצרים לפי קטגוריה

שורטקוד חיפוש מוצרים לפי קטגוריה

 add_shortcode('gs_search','gs_search_shortcode');
	function gs_search_shortcode(){
	//extract( shortcode_atts(array(
	//	'count'        => '0',
	//	'hierarchical' => '0',
	//	'orderby' 	   => ''
	//), $atts ) );		
	ob_start();	
	?>
		<div class='searchHereDiv'>
			<div class='searchHere'>
				<h3>חיפוש מוצר/חלק</h3>
				<div class='searchCategory'>
					<div class='searchCategory1'>
					<?php 
						wc_product_dropdown_categories( array(
							'orderby'            => ! empty( $orderby ) ? $orderby : 'order',
							'hierarchical'       => $hierarchical,
							'show_uncategorized' => 0,
							'show_counts'        => $count,
							'show_option_none'   => __( 'Select a category', 'woocommerce' ),
							'value_field'        => 'id',
						) );
					?>			
					<h3>קטגוריה</h3>
					</div>	
					<div class="searchCategory1">
					<?php
					if($_GET['pc'] != null):
					$catid = $_GET['pc'];
					$args = array(
						'post_type' => 'product',
						'tax_query' => array(
							array(
								'taxonomy' => 'product_cat',
								'field' => 'id', //can be set to ID
								'terms' => $catid //if field is ID you can reference by cat/term number
							)
						)
					);
					else:
					$args = array(
						'post_type' => 'product',
						'posts_per_page' => -1,
						'order' => 'ASC',
						'orderby' => 'name',
					);
					endif;
						query_posts( $args );
						if ( have_posts() ):
							echo "<select name='products_search' class='dropdown_products_search'>";
							echo "<option value='0'>".__( 'חפש לפי מוצר', 'gs_search' )."</option>";
							while ( have_posts() ) {
							the_post();
							  echo "<option value='".get_permalink( get_the_ID() )."'>";
							  echo get_the_title(); 
							  echo "</option>";
							}
							echo "</select>";	
						endif;	
						wp_reset_postdata();
					?>
					<h3>מוצר</h3>
					</div>	
						<div class="freeSearch">
						   <h3><?php echo __( 'חיפוש חופשי', 'gs_search' ) ?></h3>
							<form role="search" method="get" action="<?php echo home_url( '/' ); ?>">
						   		<i id="searchbtn" class="fa fa-search" aria-hidden="true"></i><input type="text" value="" name="s" id="s" />
						   		<!--input type="submit" value="" style="width: 0; height: 0; visibility: hidden; display: none;"-->
							</form>
						</div>	
				</div>
			</div>
		</div>	
			
	<style>
	select {
		direction: rtl;
	}
	</style>	
	
	<script type='text/javascript'>
		/* <![CDATA[ */
		jQuery(function(){
			var url = window.location.search.substring(1);
				if(url !== ''){
					var result = url.split("=");	
					jQuery(this).attr('val')
					console.log(result[1]);
					if(jQuery(this).val() == result[1]){
						console.log('value is equal');
					}
					else{			
						$(".dropdown_product_cat option[value="+ result[1] +"]").attr('selected', 'selected');
					}
			}
			jQuery(".dropdown_product_cat").on('change',function(){
								
				if ( jQuery(".dropdown_product_cat").val() !=='' ) {
					var cat_id = jQuery(this).val();
					console.log(cat_id);
					//window.location.hash = cat_id;
					location.href = "/?pc=" + cat_id
				}
			});
			jQuery(".dropdown_products_search").on('change',function(){
								
				if ( jQuery(".dropdown_product_cat").val() !=='' ) {
					var permalink = jQuery(this).val();
					console.log(permalink);
					//window.location.hash = cat_id;
					location.href = permalink;
				}
			});
			jQuery('.fa-search').click(function(){
				
				if(jQuery(this).next().val() == ''){
					jQuery(this).next().attr('placeholder','אנא הזן ערך חיפוש!');
					jQuery(this).next().focus();
					return;
					//console.log('push');
				}
				jQuery(this).parent().submit();
			});
			
		});
	/* ]]> */
	</script>
	<?php
	return ob_get_clean();	
	}

 

כתיבת תגובה