Sửa file up đè tại :
/wp-content/themes/flatsome/inc/extensions/flatsome-live-search/flatsome-live-search.php
flatsome-live-search.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
<?php function flatsome_live_search_script() { global $extensions_uri; $theme = wp_get_theme( get_template() ); $version = $theme->get( 'Version' ); wp_enqueue_script( 'flatsome-live-search', $extensions_uri . '/flatsome-live-search/flatsome-live-search.js', false, $version, true ); } add_action( 'wp_enqueue_scripts', 'flatsome_live_search_script' ); /** * Search for posts and pages. * * @param array $args * * @return array */ function flatsome_ajax_search_posts( $args ) { $defaults = $args; $args['s'] = apply_filters( 'flatsome_ajax_search_query', esc_attr( $_REQUEST['query'] ) ); $args['post_type'] = apply_filters( 'flatsome_ajax_search_post_type', array( 'post', 'page' ) ); $search_query = http_build_query( $args ); $query_function = apply_filters( 'flatsome_ajax_search_function', 'get_posts', $search_query, $args, $defaults ); return ( ( $query_function == 'get_posts' ) || ! function_exists( $query_function ) ) ? get_posts( $args ) : $query_function( $search_query, $args, $defaults ); } /** * Search for products. * * @param array $args * * @return array */ function flatsome_ajax_search_products( $args ) { global $woocommerce; $ordering_args = $woocommerce->query->get_catalog_ordering_args( 'title', 'asc' ); $defaults = $args; $args['s'] = apply_filters( 'flatsome_ajax_search_products_search_query', esc_attr( $_REQUEST['query'] ) ); $args['post_type'] = 'product'; $args['orderby'] = $ordering_args['orderby']; $args['order'] = $ordering_args['order']; $args['meta_query'] = WC()->query->get_meta_query(); $args['tax_query'] = array( 'relation' => 'AND' ); $args = flatsome_ajax_search_catalog_visibility( $args ); if ( isset( $_REQUEST['product_cat'] ) ) { $args['tax_query'][] = array( 'relation' => 'AND', array( 'taxonomy' => 'product_cat', 'field' => 'slug', 'terms' => esc_attr( $_REQUEST['product_cat'] ), ), ); } $search_query = http_build_query( $args ); $query_function = apply_filters( 'flatsome_ajax_search_function', 'get_posts', $search_query, $args, $defaults ); return ( ( $query_function == 'get_posts' ) || ! function_exists( $query_function ) ) ? get_posts( $args ) : $query_function( $search_query, $args, $defaults ); } /** * Search for products by SKU. * * @return array */ function flatsome_ajax_search_products_by_sku() { $query = apply_filters( 'flatsome_ajax_search_products_by_sku_search_query', esc_attr( $_REQUEST['query'] ) ); $query_args = array( 'post_status' => 'publish', 'post_type' => array( 'product', 'product_variation' ), 'meta_query' => array( array( 'key' => '_sku', 'value' => $query, ), ), 'tax_query' => array( 'relation' => 'AND', ), ); $query_args = flatsome_ajax_search_catalog_visibility( $query_args ); $results = new WP_Query( $query_args ); return $results->get_posts(); } /** * Checks product catalog visibility with custom tax_query. (only queries the exclude-from-search term) and checks WC hide out of stock option. * * @param array $args * * @return array */ function flatsome_ajax_search_catalog_visibility( $args ) { $product_visibility_term_ids = wc_get_product_visibility_term_ids(); // Catalog visibility $args['tax_query'][] = array( 'taxonomy' => 'product_visibility', 'field' => 'term_taxonomy_id', 'terms' => $product_visibility_term_ids['exclude-from-search'], 'operator' => 'NOT IN', ); // Hide out of stock if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ) { $args['tax_query'][] = array( 'relation' => 'AND', array( 'taxonomy' => 'product_visibility', 'field' => 'term_taxonomy_id', 'terms' => $product_visibility_term_ids['outofstock'], 'operator' => 'NOT IN', ), ); } return $args; } /** * Search AJAX handler. * * @return array */ function flatsome_ajax_search() { // The string from search text field. $query = apply_filters( 'flatsome_ajax_search_query', esc_attr( $_REQUEST['query'] ) ); $products = array(); $posts = array(); $sku_products = array(); $args = array( 's' => $query, 'orderby' => '', 'post_type' => array(), 'post_status' => 'publish', 'posts_per_page' => 100, 'ignore_sticky_posts' => 1, 'post_password' => '', 'suppress_filters' => false, ); if ( is_woocommerce_activated() ) { $products = flatsome_ajax_search_products( $args ); $sku_products = get_theme_mod( 'search_by_sku', 0 ) ? flatsome_ajax_search_products_by_sku() : array(); } if ( get_theme_mod( 'search_result', 1 ) && ! isset( $_REQUEST['product_cat'] ) ) { $posts = flatsome_ajax_search_posts( $args ); } $results = array_merge( $products, $sku_products, $posts ); $suggestions = array(); foreach ( $results as $key => $post ) { if (is_woocommerce_activated() && ($post->post_type === 'product' || $post->post_type === 'product_variation') ) { $product = wc_get_product( $post ); $product_image = wp_get_attachment_image_src( get_post_thumbnail_id( $product->get_id() ) ); $suggestions[] = array( 'type' => 'Product', 'id' => $product->get_id(), 'value' => $product->get_title(), 'url' => $product->get_permalink(), 'img' => $product_image[0], 'price' => $product->get_price_html(), ); } else { $suggestions[] = array( 'type' => 'Page', 'id' => $post->ID, 'value' => get_the_title( $post->ID ), 'url' => get_the_permalink( $post->ID ), 'img' => get_the_post_thumbnail_url( $post->ID, 'thumbnail' ), 'price' => '', ); } } if ( empty( $results ) ) { $no_results = is_woocommerce_activated() ? __( 'No products found.', 'woocommerce' ) : __( 'No matches found', 'flatsome' ); $suggestions[] = array( 'id' => -1, 'value' => $no_results, 'url' => '', ); } echo json_encode( array( 'suggestions' => $suggestions ) ); die(); } add_action( 'wp_ajax_flatsome_ajax_search_products', 'flatsome_ajax_search' ); add_action( 'wp_ajax_nopriv_flatsome_ajax_search_products', 'flatsome_ajax_search' ); |
Xem thêm:
- 21 Cách tăng 3 lần tỷ lệ chuyển đổi của các trang E-Commerce
- Plugin tính phí vận chuyển cho quận/huyện trong woocommerce
- Lập Kế Hoạch Digital Marketing – Phiên Bản Tinh Gọn
- Tùy Biến Form CF7 – Chia cột & Chèn icon lên Form tạo bằng CF7 bắt mắt
- DevVN Woocommerce Reviews – Plugin đánh giá cho WooCommerce tuyệt đẹp