Forums Forums Search & Filter Pro Additionally: help with code?

Viewing 9 posts - 1 through 9 (of 9 total)
  • Anonymous
    #229944

    Hi,

    I have these taxonomies:
    – Product
    – Merchant
    – Merchant offer

    I created a custom post template to make it possible to view all merchant offers for a product, with the possibility to filter on both merchant meta fields (review, payment methods..) as on merchant offer meta fields (price, delivery time..)
    Now I see that the S&F Pro plugin shows filter options for every existing post instead of the current viewed product. When you see my code below (this is my very first php-code, so when it is looking strange, that’s why), do you have a hint how to show only filter options for the current viewed product?

    <?php
    /*
     * Template Name: Product
     * Template Post Type: post, page, product, item
    */
    
    get_header(); ?>
    
    <?php
    $link = get_post_custom_values("link") [0];
    
    $color = get_post_custom_values("kleur") [0];
    //echo get_post_custom_values("product_group")[0];
    
    ?>
    
    <div id="content" class="site-content">
    	<section id="primary" class="content-area">
    		<main id="main" class="site-main">
    			<article id="post-2" class="post-2 page type-page status-publish hentry entry">
    			<div class = "pdp-full-top">
    			    <div class = "pdp-top-container entry-header">
    				<h1 class = "pdp-title">
    					<?php
    $brand = get_post_custom_values("brand") [0];
    $line = get_post_custom_values("line") [0];
    $type = get_post_custom_values("type") [0];
    $variant = get_post_custom_values("variant_value") [0];
    echo $brand . " " . $line . " " . $type . " " . $variant;
    ?>
    				</h1>
    				<div class = "pdp-image-container"><div class = "pdp-image-helper"></div><?php the_post_thumbnail(); ?>
    				</div>
    				<div class = "pdp-top-info-container">
    					<h2 class = "pdp-sub-title">
    						<?php
    $product_group = get_terms(array(
        'taxonomy' => 'productgroep',
        'hide_empty' => false,
    )) [0]->name;
    echo $product_group . ", " . $variant;
    ?>
    					</h2>
    					<h5 class = "pdp-choise-criteria">Keuzecriteria</h5>
    					<div class = "pdp-price">Prijs vanaf ...
    					</div>
    				</div>
    			    </div>
    			</div>
    			<div class = "pdp-lister-container entry-content">
    			<?php //Filter area ?>
    				<div class = "pdp-filter-container">
    					<?php echo do_shortcode('[searchandfilter id="165176"]'); ?>
    					<?php echo do_shortcode('[searchandfilter id="153395"]'); ?>
    				</div>
    				<div class = "pdp-shopping-list-container">
    					<?php
    //Get all merchants
    $ean = get_field("ean");
    $args_merchants = array(
        'post_type' => 'merchant_offer',
        'tax_query' => array(
            array(
                'taxonomy' => 'ean',
                'field' => 'slug',
                'terms' => $ean,
                'compare' => '='
            ) ,
        )
    );
    $offers = new WP_Query($args_merchants);
    $all_merchants = [];
    while ($offers->have_posts()):
        $offers->the_post();
        $get_merchant = get_post_meta(get_the_ID() , 'merchant_id') [0];
        array_push($all_merchants, $get_merchant);
    endwhile;
    //Get filtered merchants
    wp_reset_postdata();
    wp_reset_query();
    $args_offers = array(
        'post_type' => 'merchants',
        'search_filter_id' => 153395,
        'tax_query' => array(
            'relation' => 'OR',
            array(
                'taxonomy' => 'merchant_id',
                'field' => 'slug',
                'terms' => $all_merchants,
                'operator' => 'IN',
            ) ,
        )
    );
    $query2 = new WP_Query($args_offers);
    
    $filtered_merchants = [];
    $post_id_by_merchant = [];
    
    while ($query2->have_posts()):
        $query2->the_post();
        $get_filtered_merchant = get_post_meta(get_the_ID() , 'merchant_id') [0];
        array_push($filtered_merchants, $get_filtered_merchant);
        $post_id_by_merchant[$get_filtered_merchant] = get_the_ID();
    endwhile;
    //Get filtered offers by filtered merchants
    wp_reset_postdata();
    wp_reset_query();
    $args_filtered_offers = array(
        'post_type' => 'merchant_offer', //add filter id somewhere here!
        'search_filter_id' => 165176,
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'ean',
                'field' => 'slug',
                'terms' => $ean,
            ) ,
            array(
                'taxonomy' => 'merchant_id',
                'field' => 'slug',
                'terms' => $filtered_merchants,
            ) ,
        ) ,
    );
    
    $query3 = new WP_Query($args_filtered_offers);
    //Build shopping list
     ?>
    						<div class = "pdp-shopping-list">
    							<div class = "row header">
    								<div class = "item"></div>
    								<div class = "item">Winkel</div>
    								<div class = "item">Reviews</div>
    								<div class = "item">Basisprijs</div>
    								<div class = "item">Totaalprijs</div>
    								<div class = "item"></div>
    							</div>
    						<?php while ($query3->have_posts()):
        $query3->the_post();
        setlocale(LC_MONETARY, 'nl_NL');
        $current_merchant_id = $post->merchant_id;
        $stars = floatval(get_post_meta($post_id_by_merchant[$current_merchant_id], 'reviewsterren', true));
        $review_amount = get_post_meta($post_id_by_merchant[$current_merchant_id], 'aantal_reviews', true);
    ?>
    							    <div class = "row">
    								<div class = "item logo"><?php echo get_the_post_thumbnail($post_id_by_merchant[$current_merchant_id]); ?></div>
    								<div class = "name"><?php echo get_post_meta($post_id_by_merchant[$current_merchant_id], 'name', true) ?></div>
    								<div class = "review-container"><div class="stars" style="--rating: <?php echo $stars ?>;" aria-label="Rating of this product is <?php echo $stars ?> out of 5."></div><div class = "reviews"><?php echo $review_amount . " reviews" ?></div></div>
    								<div class = "item price-actual"><?php echo money_format("€%.2n", $post->price_actual); ?></div>
    								<div class = "item price-total"><?php echo money_format("€%.2n", $post->total_price); ?></div>
    								<div class = "item view"><button class = "button"><a href="<?php echo $post->link; ?>" target="blank">Bekijken</a></button>
    							    </div>
    							</div>
    							
    						<?php
    endwhile; ?>
    						</div>
    <?php
    wp_reset_postdata();
    wp_reset_query();
    ?>
    
    				</div>
    			</div>
    		</main>
    	</section>
    </div>
    

    Thanks again in advance.
    Best regards,
    Roeland van Oostenbrugge

    Anonymous
    #229956

    Edit: where I said “taxonomies” I meant “custom post types”.

    To narrow the question: Why does a query with 0 results (example below) give values in the auto-generated filter options? I would expect the filters to have no options because there are no results so also no attributes.

    $args_offers = array(
        'post_type' => 'merchants',
        'search_filter_id' => 153395,
        'tax_query' => array(
            //'relation' => 'OR',
            array(
                'taxonomy' => 'merchant_id',
                'field' => 'slug',
                'terms' => 'SOME NONE-EXISTING TERM TO GET NO RESULTS',//$all_merchants,
                'operator' => 'IN',
            ) ,
        )
    );
    Trevor
    #229966

    A quick thought. In the General settings tab of the form, do you have:

    Auto Count (both settings) set to ON

    … and then in the Form UI:

    in each field Hide Empty set to ON?

    Anonymous
    #230042

    Hi Trevor, thanks for your quick thought.
    Yes,both settings for auto count are set to on.
    Hide empty is only for “choise” fields, and the first two settings are number fields. Hide empty is set to on for the third field.

    – Can it have something to do with the fact that I use 3 wp-queries?
    – I don’t see any effect from adding 'search_filter_id' => 153395, to the $args. How can I check if this is set right?

    Please see this page, where the only offer should disappear when the first filter is set to less than 3: https://fietspitstop.nl/product/yepp-fietszitje-achter-maxi-easyfit-achter-zwart/

    This is the most recent template code:

    <?php
    /*
     * Template Name: Product
     * Template Post Type: post, page, product, item
    */
    
    get_header(); ?>
    
    <?php
    $link = get_post_custom_values("link") [0];
    
    $color = get_post_custom_values("kleur") [0];
    //echo get_post_custom_values("product_group")[0];
    
    ?>
    
    <div id="content" class="site-content">
    	<section id="primary" class="content-area">
    		<main id="main" class="site-main">
    			<article id="post-2" class="post-2 page type-page status-publish hentry entry">
    			<div class = "pdp-full-top">
    			    <div class = "pdp-top-container entry-header">
    				<h1 class = "pdp-title">
    					<?php
    $brand = get_post_custom_values("brand") [0];
    $line = get_post_custom_values("line") [0];
    $type = get_post_custom_values("type") [0];
    $variant = get_post_custom_values("variant_value") [0];
    echo $brand . " " . $line . " " . $type . " " . $variant;
    ?>
    				</h1>
    				<div class = "pdp-image-container"><div class = "pdp-image-helper"></div><?php the_post_thumbnail(); ?>
    				</div>
    				<div class = "pdp-top-info-container">
    					<h2 class = "pdp-sub-title">
    						<?php
    $product_group = get_the_terms( get_the_ID(), 'productgroep' )[0]->name ;
    echo $product_group . ", " . $variant;
    ?>
    					</h2>
    					<h5 class = "pdp-choise-criteria">Keuzecriteria</h5>
    					<div class = "pdp-price">Prijs vanaf ...
    					</div>
    				</div>
    			    </div>
    			</div>
    			<div class = "pdp-lister-container entry-content">
    			<?php //Filter area ?>
    				<div class = "pdp-filter-container">
    					<?php echo do_shortcode('[searchandfilter id=""]'); ?>
    					<?php echo do_shortcode('[searchandfilter id="153395"]'); ?>
    				</div>
    				<div class = "pdp-shopping-list-container">
    					<?php
    //Get all merchants
    $ean = get_field("ean");
    $args_merchants = array(
        'post_type' => 'merchant_offer',
        'tax_query' => array(
            array(
                'taxonomy' => 'ean',
                'field' => 'slug',
                'terms' => $ean,
                'compare' => '='
            ) ,
        )
    );
    $offers = new WP_Query($args_merchants);
    $all_merchants = [];
    while ($offers->have_posts()):
        $offers->the_post();
        $get_merchant = get_post_meta(get_the_ID() , 'merchant_id') [0];
        array_push($all_merchants, $get_merchant);
    endwhile;
    
    echo "all merchants: ";
    print_r($all_merchants);
    
    //Get filtered merchants
    //wp_reset_postdata();
    //wp_reset_query();
    $args_offers = array(
        'post_type' => 'merchants',
        'search_filter_id' => 153395,
        'tax_query' => array(
            //'relation' => 'AND',
            array(
                'taxonomy' => 'merchant_id',
                'field' => 'slug',
                'terms' => $all_merchants,
                'operator' => 'IN',
            ) ,
        )
    );
    
    $query2 = new WP_Query($args_offers);
    
    $filtered_merchants = [];
    $post_id_by_merchant = [];
    
    while ($query2->have_posts()):
        $query2->the_post();
        $get_filtered_merchant = get_post_meta(get_the_ID() , 'merchant_id') [0];
        array_push($filtered_merchants, $get_filtered_merchant);
        $post_id_by_merchant[$get_filtered_merchant] = get_the_ID();
    
    endwhile;
    //Get filtered offers by filtered merchants
    //wp_reset_postdata();
    //wp_reset_query();
    $args_filtered_offers = array(
        'post_type' => 'merchant_offer', //add filter id somewhere here!
        //'search_filter_id' => 165176,
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'ean',
                'field' => 'slug',
                'terms' => $ean,
            ) ,
            array(
                'taxonomy' => 'merchant_id',
                'field' => 'slug',
                'terms' => $filtered_merchants,
            ) ,
        ) ,
    );
    
    $query3 = new WP_Query($args_filtered_offers);
    //Build shopping list
     ?>
    						<div class = "pdp-shopping-list" id = "pdp-shopping-list">
    <br><?php
    echo "filtered merchants: ";
    print_r($filtered_merchants);
    ?>
    <br><?php
    echo "query 2: ";
    print_r($query2);
    ?>
    							<div class = "row header">
    								<div class = "item"></div>
    								<div class = "item">Winkel</div>
    								<div class = "item">Reviews</div>
    								<div class = "item">Basisprijs</div>
    								<div class = "item">Totaalprijs</div>
    								<div class = "item"></div>
    							</div>
    						<?php if ( have_posts() ) : while ($query3->have_posts()):
        $query3->the_post();
        setlocale(LC_MONETARY, 'nl_NL');
        $current_merchant_id = $post->merchant_id;
        $stars = floatval(get_post_meta($post_id_by_merchant[$current_merchant_id], 'reviewsterren', true));
        $review_amount = get_post_meta($post_id_by_merchant[$current_merchant_id], 'aantal_reviews', true);
    ?>
    							    <div class = "row">
    								<div class = "item logo"><?php echo get_the_post_thumbnail($post_id_by_merchant[$current_merchant_id]); ?></div>
    								<div class = "name"><?php echo get_post_meta($post_id_by_merchant[$current_merchant_id], 'name', true) ?></div>
    								<div class = "review-container"><div class="stars" style="--rating: <?php echo $stars ?>;" aria-label="Rating of this product is <?php echo $stars ?> out of 5."></div><div class = "reviews"><?php echo $review_amount . " reviews" ?></div></div>
    								<div class = "item price-actual"><?php echo money_format("€%.2n", $post->price_actual); ?></div>
    								<div class = "item price-total"><?php echo money_format("€%.2n", $post->total_price); ?></div>
    								<div class = "item view"><button class = "button"><a href="<?php echo $post->link; ?>" target="blank">Bekijken</a></button>
    							    </div>
    							</div>
    							
    						<?php
    endwhile; else : ?>
    	<p><?php esc_html_e( 'Geen producten gevonden.' ); ?></p>
    <?php endif; ?>
    						</div>
    <?php
    //wp_reset_postdata();
    //wp_reset_query();
    ?>
    
    				</div>
    			</div>
    		</main>
    	</section>
    </div>
    

    Thanks in advance.

    Anonymous
    #230052

    Hi, the site was down a while due to some mysql problem, but it is running again, so you can see https://fietspitstop.nl/product/yepp-fietszitje-achter-maxi-easyfit-achter-zwart/ again

    Anonymous
    #230075

    Hi,
    clicking your link says:
    Connection refused
    Error establishing a database connection

    Trevor
    #230077

    Hi

    As the previous poster says, the site is down. However, having many queries on one page, our form will not know which to connect to. Thus, you must use the ‘Custom’ method:

    https://searchandfilter.com/documentation/search-results/custom/

    … and add the form to the query arguments, as shown.

    Note that these is a possibility that our query will override some/all query arguments you make, so you might need to add to our query using this filter:

    https://searchandfilter.com/documentation/action-filter-reference/#edit-query-arguments

    I am not sure if you can add the same form arguments to more than one query on the page. You certainly cannot if you are using Ajax, nor for Infinite Scroll, and you might only be able to ‘send’ a search to one form’s query arguments.

    Anonymous
    #230355

    Hi,

    The site is up again:
    https://fietspitstop.nl/product/sigma-elastische-band-20311/?_sfm_reviewsterren=0+5&_sfm_aantal_reviews=88+668

    I use different form arguments for each query, so that shouldn’t be a problem. And I already implemented the ‘custom’ method by adding the S&F id to the query (see code above).

    I don’t completely understand the second part of your answer, about the filter part; where should I put this and what values should I put in it?

    Meanwhile I narrowed the problem: when I turn off Ajax and fill in the product url in the ‘result url’ field, filtering works. Of course this is for debugging becouse I don’t want all products to link to this product.

    Question: how should I configure things to make it work dynamically with Ajax?

    Thanks,
    Roeland

    Trevor
    #230668

    In my reference to that filter, such a filter would be used inside the child theme functions.php file.

    From your reply, it sounds as though you had not entered a Results URL? If the form ask for this, it must be entered. It is not possible to have a form that does not know where to send the results.

    You say that you are using a custom template for your page. Is this a custom post type archives template, or a custom taxonomy archives template, named in accordance with the WordPress Codex?

    There would normally only be one query (named $query). If you are using Ajax, you can attach a form only to one query on the page.

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