search-result.php
150 lines
| 1 | <?php |
| 2 | |
| 3 | if(empty($_GET['nonce']) || !wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['nonce']) ), 'wp_rest')){ |
| 4 | return; |
| 5 | } |
| 6 | |
| 7 | if( !empty($_GET['s'])) { |
| 8 | |
| 9 | $product_query = array( |
| 10 | 'orderby' => 'date', |
| 11 | 'post_status' => 'publish', |
| 12 | 'post_type' => 'product', |
| 13 | 's' => sanitize_text_field( wp_unslash($_GET['s']) ), |
| 14 | 'posts_per_page' => 30, |
| 15 | ); |
| 16 | |
| 17 | if( !empty( $_GET['product_cat']) ) { |
| 18 | |
| 19 | $cats = explode(',', trim(sanitize_text_field( wp_unslash($_GET['product_cat'])), ',')); |
| 20 | |
| 21 | $sanitized = array_map(function ($val){ |
| 22 | |
| 23 | return intval($val); |
| 24 | }, |
| 25 | $cats); |
| 26 | |
| 27 | $product_query['tax_query'] = array( |
| 28 | array( |
| 29 | 'taxonomy' => 'product_cat', |
| 30 | 'field' => 'term_id', |
| 31 | 'terms' => $sanitized, |
| 32 | 'operator' => 'IN' |
| 33 | ) |
| 34 | ); |
| 35 | } |
| 36 | |
| 37 | $products = new WP_Query($product_query); |
| 38 | |
| 39 | |
| 40 | global $wpdb; |
| 41 | |
| 42 | $categories = $wpdb->get_results($wpdb->prepare("SELECT terms.* from {$wpdb->prefix}terms AS terms INNER JOIN {$wpdb->prefix}term_taxonomy AS term_taxonomy |
| 43 | ON terms.term_id = term_taxonomy.term_id WHERE term_taxonomy.taxonomy = 'product_cat' |
| 44 | AND terms.name LIKE %s LIMIT 5", '%'. sanitize_text_field( wp_unslash($_GET['s']) ) .'%')); |
| 45 | } |
| 46 | |
| 47 | if( $products->have_posts() || !empty($categories)): ?> |
| 48 | |
| 49 | <div class="shopengine-search-product"> |
| 50 | |
| 51 | <?php foreach($categories as $category): ?> |
| 52 | |
| 53 | |
| 54 | <div class="shopengine-category-name shopengine-search-product__item"> |
| 55 | <div class="shopengine-search-product__item--image"> |
| 56 | <img src="<?php |
| 57 | $term_meta = get_term_meta($category->term_id); |
| 58 | if(!empty($term_meta['thumbnail_id'][0])) { |
| 59 | $attachment = wp_get_attachment_image_src($term_meta['thumbnail_id'][0]); |
| 60 | echo isset($attachment[0]) ? esc_url($attachment[0]) : esc_url(wc_placeholder_img_src()); |
| 61 | } else { |
| 62 | echo esc_url(wc_placeholder_img_src()); |
| 63 | } |
| 64 | |
| 65 | ?>" alt="<?php echo isset($term_meta['thumbnail_id'][0]) ? esc_attr(trim( strip_tags( get_post_meta( $term_meta['thumbnail_id'][0], '_wp_attachment_image_alt', true ) ) ) ) : ""; ?>"> |
| 66 | </div> |
| 67 | |
| 68 | <?php $category_url = get_term_link($category->slug, 'product_cat'); ?> |
| 69 | |
| 70 | <div class="shopengine-search-product__item--content"> |
| 71 | <h4 class="shopengine-search-product__item--title"> |
| 72 | <a title="<?php echo esc_html($category->name, 'shopengine');?>" href="<?php echo esc_url($category_url) ?>"> <?php echo esc_html($category->name) ?> </a> |
| 73 | </h4> |
| 74 | </div> |
| 75 | |
| 76 | <a title="<?php esc_html_e('Show Category','shopengine')?>" class="shopengine-search-more-btn" href="<?php echo esc_url($category_url); ?>"><i class="fas fa-chevron-right"></i></a> |
| 77 | </div> |
| 78 | |
| 79 | |
| 80 | <?php endforeach;?> |
| 81 | |
| 82 | <?php |
| 83 | while ( $products->have_posts() ) : $products->the_post(); |
| 84 | $id = esc_attr( get_the_ID() ); |
| 85 | $image_url = get_the_post_thumbnail_url( $id ); |
| 86 | $attachment_id = get_post_thumbnail_id($id); |
| 87 | $alt = trim( strip_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) ); |
| 88 | $product = wc_get_product( $id ); |
| 89 | $price = wc_price($product->get_price()); |
| 90 | $reg_price = wc_price($product->get_regular_price()); |
| 91 | ?> |
| 92 | |
| 93 | <div class="shopengine-search-product__item"> |
| 94 | <div class="shopengine-search-product__item--image"> |
| 95 | <img src="<?php echo esc_url( $image_url ) ?>" alt="<?php esc_attr_e($alt,'shopengine'); ?>"> |
| 96 | </div> |
| 97 | <div class="shopengine-search-product__item--content"> |
| 98 | <h4 class="shopengine-search-product__item--title"> |
| 99 | <a title="<?php esc_html_e('View Product Full Details','shopengine')?>" href="<?php the_permalink() ?>"> <?php the_title() ?> </a> |
| 100 | </h4> |
| 101 | <div class="shopengine-product-rating"> |
| 102 | <?php |
| 103 | if($product->get_rating_count() > 0){ |
| 104 | woocommerce_template_loop_rating(); |
| 105 | } else { |
| 106 | $rating_html = '<div class="star-rating">'; |
| 107 | $rating_html .= wc_get_star_rating_html( 0, 0 ); |
| 108 | $rating_html .= '</div>'; |
| 109 | |
| 110 | echo wp_kses_post($rating_html); |
| 111 | } |
| 112 | |
| 113 | // review count |
| 114 | $review_count = $product->get_review_count(); |
| 115 | echo "<span class='rating-count'>(". esc_html( $review_count ) .")</span>"; |
| 116 | ?> |
| 117 | </div> |
| 118 | <div class="shopengine-search-product__item--price"> |
| 119 | <?php echo wp_kses_post($product->get_price_html()); ?> |
| 120 | </div> |
| 121 | </div> |
| 122 | <a title="<?php esc_html_e('Searching More Products','shopengine')?>" class="shopengine-search-more-btn" href="<?php echo esc_url(get_the_permalink()); ?>"><i class="fas fa-chevron-right"></i></a> |
| 123 | </div> |
| 124 | |
| 125 | <?php |
| 126 | |
| 127 | endwhile; |
| 128 | wp_reset_query(); |
| 129 | wp_reset_postdata(); |
| 130 | |
| 131 | ?> |
| 132 | |
| 133 | <div class="shopengine-search-product__item"> |
| 134 | <a title="<?php esc_html_e('Searching a Products','shopengine')?>" class="shopengine-search-more-products" href="<?php echo esc_url( get_the_permalink(get_option('woocommerce_shop_page_id')) ); ?>"><i class="fas fa-retweet"></i><?php echo esc_html__('More Products', 'shopengine'); ?></a> |
| 135 | </div> |
| 136 | |
| 137 | </div> |
| 138 | |
| 139 | <?php |
| 140 | |
| 141 | else: ?> |
| 142 | <div class="shopengine-search-product"> |
| 143 | <div> |
| 144 | <?php echo esc_html__('No result found', 'shopengine'); ?> |
| 145 | </div> |
| 146 | </div> |
| 147 | <?php |
| 148 | |
| 149 | endif; |
| 150 |