Home Forums WoodMart support forum add unit prices into the ajax search dropdown

add unit prices into the ajax search dropdown

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #214510

    florianibe
    Participant

    Hi, 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 …”

    #214535

    Artem Temos
    Keymaster

    Hi,

    Please, send us your admin and FTP access so we can check why the price is not displayed there.

    Kind Regards

    #214547

    florianibe
    Participant

    Here the ftp login

    #214590

    Artem Temos
    Keymaster

    Try 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%;
    }
    #215061

    florianibe
    Participant

    Thank you but i get this error “Cannot redeclare function woodmart_ajax_suggestions.”

    #215078

    Artem Temos
    Keymaster

    Are you trying to place it inside the child theme? Could you please send us a screenshot where you add it?

    #215332

    florianibe
    Participant

    Sorry my mistake. It works only in functions.php.
    Now everything is good.

    #215338

    Artem Temos
    Keymaster

    Great, you are welcome!

Tagged: 

Viewing 8 posts - 1 through 8 (of 8 total)