0
votes

I changed the core query of woocommerce and trying to show products based on custom query string pro_type_sws=32

Now when I supplied whole querystring it works

wp/?min_price=35&max_price=120&post_type=product&pro_type_sws=32

but when I just use

localhost/wp/?post_type=product&pro_type_sws=32

it does not work, I don't know how can I activate the pricefilter code without using widget FILTER button.

In short I want to apply filter over products page so it shows only those products which are related to custom product data types of woocommerce.

Code Changed in : class-wc-query.php

    if (( isset( $_GET['max_price'] ) && isset( $_GET['min_price'] )) or isset( $_GET['pro_type_sws'])) {


        $matched_products = array();
        $min    = floatval( $_GET['min_price'] );
        $max    = floatval( $_GET['max_price'] );
            $pro_type = $_GET['pro_type_sws'];

            /*
                Applying Filter based on product type query string
             *  If Product type query string set then render only those products which are related to given product data type.
             */
           if (isset( $pro_type ) ){
                // Query Update : Product type based filter applied through $pro_type query string 
                $matched_products_query = apply_filters( 'woocommerce_price_filter_results', $wpdb->get_results( $wpdb->prepare("
            SELECT DISTINCT ID, post_parent, post_type FROM $wpdb->posts
            INNER JOIN $wpdb->postmeta ON ID = post_id
                            LEFT JOIN wp_term_relationships AS rel ON object_id = post_id 
            WHERE post_type IN ( 'product', 'product_variation' ) AND post_status = 'publish' AND meta_key = %s AND meta_value BETWEEN %d AND %d AND rel.term_taxonomy_id = %d
        ", '_price', $min, $max, $pro_type ), OBJECT_K ), $min, $max );

           }
           else{
                $matched_products_query = apply_filters( 'woocommerce_price_filter_results', $wpdb->get_results( $wpdb->prepare("
            SELECT DISTINCT ID, post_parent, post_type FROM $wpdb->posts
            INNER JOIN $wpdb->postmeta ON ID = post_id
            WHERE post_type IN ( 'product', 'product_variation' ) AND post_status = 'publish' AND meta_key = %s AND meta_value BETWEEN %d AND %d
        ", '_price', $min, $max ), OBJECT_K ), $min, $max );

           }


        if ( $matched_products_query ) {
            foreach ( $matched_products_query as $product ) {
                if ( $product->post_type == 'product' )
                    $matched_products[] = $product->ID;
                if ( $product->post_parent > 0 && ! in_array( $product->post_parent, $matched_products ) )
                    $matched_products[] = $product->post_parent;
            }
        }
1
Can we see your custom query code?rnevius
@rnevius Please check now but this activate only when I pass the full URL but is there any other suitable way so I can use localhost/wp/?post_type=product&pro_type_sws=32 SO i just pass pro_type_sws=32 querystring and price filter get activatedBilal

1 Answers

1
votes

Changing core files is never a good idea, at each update (and WC has many per year) you'll have to apply your mods again.

Try hooking on the same filter (on a custom plugin or the theme's functions.php) and doing your stuff:

add_filter( 'loop_shop_post_in', function(){
    if (
        ( isset( $_GET['max_price'] ) && isset( $_GET['min_price'] ) ) 
        or isset( $_GET['pro_type_sws'] )
    ){
        // do your thing
        // adjust the issets to integrate to WC behavior
        // etc
    }
});