• Resolved beau321

    (@beau321)


    i use elementor form to create a custom search form and a custom query code to point to a post widget used in a elementor search result template. The post widget has query id the reason why it was used. I also use elementor custom skins pro to customize how the product should show in results. after applying the code: the results show up empty. before correcting the parameters and query id, it wasn’t filtering between the two products i’ve created. the default elementor form work great filtering the product, but the custom form, idk. Please take a look and help me please. Here is the custom query code:

    add_action(‘elementor/query/my_custom_filter’, function ($query) {

    $meta_query = array('relation' => 'AND');
    
    // Check if CAS Number is provided, using '=' comparison
    if (!empty($_GET['cas_number'])) {
        $meta_query[] = array(
            'key' => 'CAS Number',  // Correct key name without underscore
            'value' => sanitize_text_field($_GET['cas_number']),
            'compare' => '='  // Exact match
        );
    }
    
    // Check if Supplier is provided, using LIKE comparison
    if (!empty($_GET['supplier'])) {
        $meta_query[] = array(
            'key' => 'Supplier',  // Correct key name for supplier
            'value' => sanitize_text_field($_GET['supplier']),
            'compare' => 'LIKE'  // Partial match
        );
    }
    
    // Check if Other Search Term is provided, using '=' comparison
    if (!empty($_GET['other_search_term'])) {
        $meta_query[] = array(
            'key' => 'Other Search Term',  // Correct key name without underscore
            'value' => sanitize_text_field($_GET['other_search_term']),
            'compare' => '='  // Exact match
        );
    }
    
    // Check if Estimated Delivery Time is provided, using LIKE comparison
    if (!empty($_GET['estimated_delivery_time'])) {
        $meta_query[] = array(
            'key' => 'Estimated Delivery Time',  // Correct key name for delivery time
            'value' => sanitize_text_field($_GET['estimated_delivery_time']),
            'compare' => 'LIKE'  // Partial match
        );
    }
    
    // Check if Minimum Price and Maximum Price are provided, using BETWEEN comparison
    if (!empty($_GET['min_price']) && !empty($_GET['max_price'])) {
        $meta_query[] = array(
            'key' => 'Price',  // WooCommerce standard price field
            'value' => array(sanitize_text_field($_GET['min_price']), sanitize_text_field($_GET['max_price'])),
            'type' => 'NUMERIC',
            'compare' => 'BETWEEN'
        );
    }
    
    // Only apply the meta query if it's not empty
    if (!empty($meta_query)) {
        $query->set('meta_query', $meta_query);
    }

    });

    this is the redirect url: https://nordchem.se/?elementor_library=search-results&cas_number=%5Bfield id=”cas_number”]&supplier=[field id=”supplier”]&other_search_term=[field id=”other_search_term”]&estimated_delivery_time=[field id=”estimated_delivery_time”]&min_price=[field id=”min_price”]&max_price=[field id=”max_price”]

    the only other custom code i have is to hide and reveal the hidden container of a product to reveal details of the product which works sometimes idk why. here is the code:

    document.addEventListener(‘DOMContentLoaded’, function() {
    // Get all the buttons and chemical detail containers
    const detailsBtns = document.querySelectorAll(‘.details-btn’);
    const chemicalDetailsContainers = document.querySelectorAll(‘.chemical-details’);

    // Hide all chemical details containers by default
    chemicalDetailsContainers.forEach(function(container) {
        container.style.display = 'none';
    });
    
    // Attach click event to each button
    detailsBtns.forEach(function(btn, index) {
        btn.addEventListener('click', function() {
            const chemicalDetails = chemicalDetailsContainers[index];
            if (chemicalDetails.style.display === 'none') {
                chemicalDetails.style.display = 'block';
            } else {
                chemicalDetails.style.display = 'none';
            }
        });
    });

    });

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘custom query not filtering’ is closed to new replies.