• Hi ILLID,

    Hope you are doing good.

    Using your plugin for Woocommerce product search.

    I have a Custom Attribute called “Domains” (like: “Math”, “Physics” ,”Computer”)

    Every product should have this custom attribute. I want to search products within a specific Custom attribute.

    Use case: User will choose the domain when the page loads for first time. Once domain is selected, the search bar should produce results only for that custom attribute(“Domain”). Example, if “math” is selected, any search results should produce only Math related products.

    Is there any hooks available for this. I will get the “domain”(attribute/taxonomy) value via session/cookie in php. I want to add this additional query with this taxonomy for every search.

    I have gone through the documentation, but couldn’t fine. Sorry if I have missed it.

    Thank you for your support in advance.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter muthuvel2

    (@muthuvel2)

    Simple just want to make search with in a specific product attribute

    • This reply was modified 4 years, 10 months ago by muthuvel2.
    Plugin Author ILLID

    (@mihail-barinov)

    Hi,

    So, as I understand, you want option to limit search results only to products with certain attributes.
    It is possible to achieve with this code snippet

    add_filter( 'aws_search_query_array', 'aws_search_query_array' );
    function aws_search_query_array( $query ) {
        $args = array(
            'posts_per_page'      => -1,
            'fields'              => 'ids',
            'post_type'           => 'product',
            'post_status'         => 'publish',
            'ignore_sticky_posts' => true,
            'suppress_filters'    => true,
            'no_found_rows'       => 1,
            'orderby'             => 'ID',
            'order'               => 'DESC',
            'lang'                => '',
            'tax_query'           => array(
                array(
                    'taxonomy'            => 'pa_size',
                    'field'               => 'id',
                    'terms'               => array( 42 ),
                    'operator'            => 'IN',
                    'include_children' => true
                )
            )
        );
    
        $posts = get_posts( $args );
        $filtered = array();
    
        foreach( $posts as $post_id ) {
            $filtered[] = $post_id;
        }
    
        $query['type'] = $query['type'] . sprintf( ' AND ( id IN ( %s ) )', implode( ',', $filtered ) );
    
        return $query;
    }

    In this example we include to search results only products with attribute ‘pa_size’ and term ID 42.

    Regards

    Thread Starter muthuvel2

    (@muthuvel2)

    Hi,

    Thanks for the code.

    I have cleared the Cache and reindexed before trying it.

    But it is not working.

    But I am able to see the applied filters while debugging inside class-wp-query.php->get_posts() (Refer the screen shot : DebuggingWindow)

    I have added the code snippet you have shared :(Refer the screen shot updatedHook)

    When I type the key word “Formula” both the products are appearing in the search result. (Refer : Search Results )

    Attribute Seetings (AttributeSettings)

    Plugin Author ILLID

    (@mihail-barinov)

    Hi,

    On your last screenshot I see Count: 0 for both attributes terms.
    Are you sure that you add this attributes to the needed products?

    Regards

    Thread Starter muthuvel2

    (@muthuvel2)

    Hi

    Yes, I have configured each product with different attributes.

    Screenshot might be taken before assigning.

    I have verified it again.

    Refer this link for better understanding : Updated Search image

    I already have a filter similar to what you have shared yesterday for “pre_get_posts”. So my entire site displays the products assigned with a particular domain. Even the default search bar displays the products of selected domain.

    Thanks & Regards

    • This reply was modified 4 years, 10 months ago by muthuvel2.
    Thread Starter muthuvel2

    (@muthuvel2)

    I have added Advanced Woo Search and Default Search bar in the same page.
    Have added 1 Product for Math
    2 Product for Physics

    Advanced Woo Search Results :
    Search Results

    Default Search bar Results :
    Default Search Results

    Thanks & Regards

    • This reply was modified 4 years, 10 months ago by muthuvel2.
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Search products within a custom attribute’ is closed to new replies.