Home › Forums › WoodMart support forum › add unit prices into the ajax search dropdown
add unit prices into the ajax search dropdown
- This topic has 7 replies, 2 voices, and was last updated 4 years, 3 months ago by Artem Temos.
-
AuthorPosts
-
July 27, 2020 at 10:22 am #214510
florianibeParticipantHi, how can i add unit prices into the ajax search dropdown?
The normal price but not the unit price “https://www.eingenuss.de/”. We use Germanized. Check the search with “leo …”
July 27, 2020 at 11:31 am #214535
Artem TemosKeymasterHi,
Please, send us your admin and FTP access so we can check why the price is not displayed there.
Kind Regards
July 27, 2020 at 11:42 am #214547
florianibeParticipantHere the ftp login
July 27, 2020 at 1:49 pm #214590
Artem TemosKeymasterTry to add the following PHP code snippet to the child theme functions.php file to do this
function woodmart_ajax_suggestions() { $allowed_types = array( 'post', 'product', 'portfolio' ); $post_type = 'product'; if ( apply_filters( 'woodmart_search_by_sku', woodmart_get_opt( 'search_by_sku' ) ) && woodmart_woocommerce_installed() ) { add_filter( 'posts_search', 'woodmart_product_ajax_search_sku', 10 ); } $query_args = array( 'posts_per_page' => 5, 'post_status' => 'publish', 'post_type' => $post_type, 'no_found_rows' => 1, ); if ( ! empty( $_REQUEST['post_type'] ) && in_array( $_REQUEST['post_type'], $allowed_types ) ) { $post_type = strip_tags( $_REQUEST['post_type'] ); $query_args['post_type'] = $post_type; } if ( $post_type == 'product' && woodmart_woocommerce_installed() ) { $product_visibility_term_ids = wc_get_product_visibility_term_ids(); $query_args['tax_query'][] = array( 'taxonomy' => 'product_visibility', 'field' => 'term_taxonomy_id', 'terms' => $product_visibility_term_ids['exclude-from-search'], 'operator' => 'NOT IN', ); if ( ! empty( $_REQUEST['product_cat'] ) ) { $query_args['product_cat'] = strip_tags( $_REQUEST['product_cat'] ); } } if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) && $post_type == 'product' ) { $query_args['meta_query'][] = array( 'key' => '_stock_status', 'value' => 'outofstock', 'compare' => 'NOT IN' ); } if ( ! empty( $_REQUEST['query'] ) ) { $query_args['s'] = sanitize_text_field( $_REQUEST['query'] ); } if ( ! empty( $_REQUEST['number'] ) ) { $query_args['posts_per_page'] = (int) $_REQUEST['number']; } $results = new WP_Query( apply_filters( 'woodmart_ajax_search_args', $query_args ) ); if ( woodmart_get_opt( 'relevanssi_search' ) && function_exists( 'relevanssi_do_query' ) ) { relevanssi_do_query( $results ); } $suggestions = array(); if ( $results->have_posts() ) { if ( $post_type == 'product' && woodmart_woocommerce_installed() ) { $factory = new WC_Product_Factory(); } while ( $results->have_posts() ) { $results->the_post(); if ( $post_type == 'product' && woodmart_woocommerce_installed() ) { $product = $factory->get_product( get_the_ID() ); ob_start(); echo '<span class="price price-unit">'; woocommerce_gzd_template_single_price_unit(); echo '</span>'; $unit = ob_get_clean(); $suggestions[] = array( 'value' => html_entity_decode( get_the_title() ), 'permalink' => get_the_permalink(), 'price' => $product->get_price_html() . $unit, 'thumbnail' => $product->get_image(), 'sku' => $product->get_sku() ? esc_html__( 'SKU:', 'woodmart' ) . ' ' . $product->get_sku() : '', ); } else { $suggestions[] = array( 'value' => html_entity_decode( get_the_title() ), 'permalink' => get_the_permalink(), 'thumbnail' => get_the_post_thumbnail( null, 'medium', '' ), ); } } wp_reset_postdata(); } else { $suggestions[] = array( 'value' => ( $post_type == 'product' ) ? esc_html__( 'No products found', 'woodmart' ) : esc_html__( 'No posts found', 'woodmart' ), 'no_found' => true, 'permalink' => '' ); } if ( woodmart_get_opt( 'enqueue_posts_results' ) && 'post' !== $post_type ) { $post_suggestions = woodmart_get_post_suggestions(); $suggestions = array_merge( $suggestions, $post_suggestions ); } echo json_encode( array( 'suggestions' => $suggestions, ) ); die(); } add_action( 'wp_ajax_woodmart_ajax_search', 'woodmart_ajax_suggestions', 10 ); add_action( 'wp_ajax_nopriv_woodmart_ajax_search', 'woodmart_ajax_suggestions', 10 );
and use the following CSS code to change the font. Add it to the Custom CSS section in Theme Settings
.autocomplete-suggestion .price-unit .amount, .autocomplete-suggestion .price-unit .price-unit { color: #666; font-weight: 400; } .autocomplete-suggestion .price-unit .price-unit { margin-top: 3px; margin-bottom: 0; } .autocomplete-suggestion .suggestion-thumb { height: 100%; }
July 29, 2020 at 9:48 am #215061
florianibeParticipantThank you but i get this error “Cannot redeclare function woodmart_ajax_suggestions.”
July 29, 2020 at 11:12 am #215078
Artem TemosKeymasterAre you trying to place it inside the child theme? Could you please send us a screenshot where you add it?
July 30, 2020 at 8:05 am #215332
florianibeParticipantSorry my mistake. It works only in functions.php.
Now everything is good.July 30, 2020 at 8:17 am #215338
Artem TemosKeymasterGreat, you are welcome!
-
AuthorPosts
Tagged: unit price
- You must be logged in to create new topics. Login / Register