Support » Plugin: WC Ajax Product Filter » Timeouts with large product databases – I have the same issue

Viewing 1 replies (of 1 total)
  • I’ve updated my local copy of the plugin, and it seems my suggestion may have been taken into consideration.

    I did create a file and place it in wp-content/mu-plugins. It overwrites the wcapf_list_terms function to make it much more efficient, but lacks the ability to properly display hierarchical items. Let me know if it helps.

    <?php
    /**
     * Replace default wcapf_list_terms function to only have a single sql query
     * - No heirarchy possible at this point
     * @global type $wcapf
     * @global type $wpdb
     * @param type $attr_args
     * @return type
     */
    function wcapf_list_terms( $attr_args ) {
        global $wcapf;
    
        extract( $attr_args );
    
        $parent_args = array(
            'orderby'    => 'name',
            'order'      => 'ASC',
            'hide_empty' => true
        );
    
        if ( $enable_hierarchy === true ) {
            $parent_args['parent'] = 0;
        }
    
        $parent_terms = get_terms( $taxonomy, $parent_args );
    
        $html = '';
        $found = false;
    
        if ( sizeof( $parent_terms ) > 0 ) {
            $html .= '<div class="wcapf-layered-nav">';
            $html .= '<ul>';
    
            // store term ids from url for this attribute
            // example: attra_size=9,29,45
            $term_ids = array();
    
            if ( key_exists( $data_key, $url_array ) && ! empty( $url_array[$data_key] ) ) {
                $term_ids = explode( ',', $url_array[$data_key] );
            }
    
            // store the ancestor ids for this term
            $chosen_filters = $wcapf->getChosenFilters();
            $term_ancestors = $chosen_filters['term_ancestors'];
            $ancestors = array();
    
            if ( sizeof( $term_ancestors ) > 0 && key_exists( $data_key, $term_ancestors ) ) {
                foreach ( $term_ancestors[$data_key] as $chosen_filter ) {
                    foreach ( $chosen_filter as $ancestor ) {
                        array_push( $ancestors, $ancestor );
                    }
                }
            }
    
            $ancestors = array_unique( $ancestors );
            $prod_ids = ( $query_type === 'and' ) ? $wcapf->filteredProductIds() : $wcapf->unfilteredProductIds();
    
            global $wpdb;
            $all_prod_term_ids = array_count_values( $wpdb->get_col( "SELECT tr.term_taxonomy_id FROM $wpdb->term_relationships AS tr WHERE tr.object_id IN (" . implode( ',', $prod_ids ) . ")" ) );
    
            foreach ( $parent_terms as $parent_term ) {
                $parent_term_id = $parent_term->term_id;
    
                $count = ( array_key_exists( $parent_term_id, $all_prod_term_ids ) ) ? $all_prod_term_ids[ $parent_term_id ] : 0;
    
                $force_show = false;
    
                // if this term id is present in $term_ids array we will force
                if ( in_array( $parent_term_id, $term_ids ) ) {
                    $force_show = true;
                }
                // if child term is selected we will force
                elseif ( sizeof( $ancestors ) > 0 && in_array( $parent_term_id, $ancestors ) ) {
                    $force_show = true;
                }
    
                if ( $count > 0 || $force_show === true ) {
                    $found = true;
    
                    $html .= ( in_array( $parent_term_id, $term_ids ) ) ? '<li class="chosen dekk-attr">' : '<li class="dekk-attr">';
                    
                    $html .= '<a href="javascript:void(0)" data-key="' . $data_key . '" data-value="' . $parent_term_id . '" data-multiple-filter="' . $enable_multiple . '">' . $parent_term->name . '</a>';
                    
                    if ( $show_count === true ) {
                        $html .= '<span class="count">(' . $count . ')</span>';
                    }
                    
                    $html .= '</li>';
                }
            }
    
            $html .=  '</ul>';
            $html .= '</div>';
        }
    
        return array(
            'html'  => $html,
            'found' => $found
        );
    }
    
Viewing 1 replies (of 1 total)
  • The topic ‘Timeouts with large product databases – I have the same issue’ is closed to new replies.