• Resolved classiccars13

    (@classiccars13)


    I got this code from your website and it does exactlly what i need it to do. but now the client wants it split on the search result.

    add_filter( 'aws_excerpt_search_result', 'my_aws_excerpt_search_result', 10, 3 );
    function my_aws_excerpt_search_result( $excerpt, $post_id, $product ) {
        $attributes_to_show = array( 'pa_size' => 'Size', 'pa_color' => 'Color' );
        $attributes = $product->get_attributes();
        $attrs = array();
        if ( $product->is_type( 'simple' ) || $product->is_type( 'external' ) || $product->is_type( 'variation' ) ) {
            if ( $attributes ) {
                foreach ( $attributes as $attribute_slug => $attribute_values ) {
                    if ( isset( $attributes_to_show[$attribute_slug] ) ) {
                        $attr_name = $attributes_to_show[$attribute_slug];
                        if ( is_object( $attribute_values ) ) {
                            $terms = $attribute_values->get_terms();
                            if ( $terms ) {
                                foreach( $terms as $term ) {
                                    $attrs[$attr_name][] = $term->name;
                                }
                            }
                        } else {
                            $attrs[$attr_name][] = $attribute_values;
                        }
     
                    }
                }
            }
        }
        if ( ! empty( $attrs ) ) {
            foreach( $attrs as $attr_name => $attr_terms ) {
                $excerpt .=  '<br>' . $attr_name . ' ';
                foreach ( $attr_terms as $attr_term ) {
                    $excerpt .= '<span style="display: inline-block;padding: 0 5px;border: 1px solid #ccc;margin: 5px 5px 2px 0;">' . $attr_term . '</span>';
                }
            }
        }
     
        return $excerpt;
    }

    I used the following code to split it as per this page https://www.ads-software.com/support/topic/form-result-layout/#post-17024133

    but how can I make it that size in once column and color in the next, just to be clear I allready did the columns it just shows size and color as duplicates in the table.

Viewing 5 replies - 16 through 20 (of 20 total)
  • Plugin Author ILLID

    (@mihail-barinov)

    You are right – there are no any error messages that can help you fix the problem.

    Another thing – when testing the search form please open the browser debug window ( F12 ) and check for any error messages inside it.

    Thread Starter classiccars13

    (@classiccars13)

    Ok I did the debug thing on the browser as per your request and giot this

    Uncaught ReferenceError: size is not defined
        at Object.<anonymous> ((index):37157:52)
        at Function.each (jquery.min.js?ver=3.7.0:2:3129)
        at aws_results_html ((index):37138:22)
        at common.min.js?ver=2.87:1:844
        at Array.forEach (<anonymous>)
        at common.min.js?ver=2.87:1:811
        at Array.forEach (<anonymous>)
        at AwsHooks.apply_filters (common.min.js?ver=2.87:1:775)
        at Object.showResults (common.min.js?ver=2.87:1:5402)
        at Object.success (common.min.js?ver=2.87:1:2429)
    (index):37157 Uncaught ReferenceError: size is not defined
        at Object.<anonymous> ((index):37157:52)
        at Function.each (jquery.min.js?ver=3.7.0:2:3129)
        at aws_results_html ((index):37138:22)
        at common.min.js?ver=2.87:1:844
        at Array.forEach (<anonymous>)
        at common.min.js?ver=2.87:1:811
        at Array.forEach (<anonymous>)
        at AwsHooks.apply_filters (common.min.js?ver=2.87:1:775)
        at Object.showResults (common.min.js?ver=2.87:1:5402)
        at Object.onKeyup (common.min.js?ver=2.87:1:1665)

    one thing that I do not anderstand is why it stats only size as I have changed it to be the following

    add_action( 'wp_enqueue_scripts', 'aws_wp_enqueue_scripts', 999 );
    
    function aws_wp_enqueue_scripts() {
        $script = '
            function aws_results_html( html, options ) {
    
               html = "";
            
               if ( ( typeof options.response.products !== "undefined" ) && options.response.products.length > 0 ) {
               
                  html += "<div class=\"aws-results-table-container\" style=\"max-height:400px; overflow-y: auto;\">"; // Wrap the table in a div
                  html += "<table>"; 
                  
                  html += "<thead>"; 
                  html += "<tr><th bgcolor=\"#1b75bb\" style=\"color:#FFFFFF;\">Part No</th><th bgcolor=\"#1b75bb\" style=\"color:#FFFFFF;\">Image</th><th bgcolor=\"#1b75bb\" style=\"color:#FFFFFF;\">Name</th><th bgcolor=\"#1b75bb\" style=\"color:#FFFFFF;\">Dia/Size</th><th bgcolor=\"#1b75bb\" style=\"color:#FFFFFF;\">Lenght</th></tr>"; 
                  html += "</thead>"; 
    			  
                  html += "<tbody>"; 
                  
                  jQuery.each(options.response.products, function (i, result) {
                  
                      html += "<tr class=\"aws_result_item\">";  
                  
                      html += "<td>";  
                      if ( result.sku ) {
                          html += result.sku; 
                      }
                      html += "</td>";  
                      
                      html += "<td>";  
                      if ( result.image ) {
                          html += "<span class=\"aws_result_image\"><img src=\'" + result.image + "\'></span>";
                      }
                      html += "</td>";  
              
                      html += "<td><a href=\'" + result.link + "\'>" + result.title + "</a></td>";  
                      html += "<td>";  
                      <!-- Dia/Size here-->
                        if ( typeof result.pa_diameter-size !== "undefined") {
                         html += result.pa_diameter-size;  
                      }
                      html += "</td>";  
    				   html += "<td>";  
                      <!-- Lenght here -->
                      if ( typeof result.pa_lenght !== "undefined") {
                          html += result.pa_lenght; 
                      }
                      html += "</td>";  
                      html += "</tr>";  
    				   
                       
                  });
                  
                  html += "</tbody>"; 
                  html += "</table>"; 
                  html += "</div>"; // Close the table container div
                  
                  if ( options.data.showMore && options.data.showPage ) {
                      html += "<ul><li class=\"aws_result_item aws_search_more\"><a href=\"#\">Show More</a></li></ul>";
                  }
                
               } else {
                  html += "<ul><li class=\"aws_result_item aws_no_result\">No results</li></ul>";
               }
               
               return html;
               
            }
            AwsHooks.add_filter( "aws_results_html", aws_results_html );
        ';
        wp_add_inline_script( 'aws-script', $script);
        wp_add_inline_script( 'aws-pro-script', $script);
    }
    
    add_filter( 'aws_search_pre_filter_single_product', 'my_aws_search_pre_filter_single_product', 10, 3 );
    function my_aws_search_pre_filter_single_product( $result, $post_id, $product ) {
        $attributes_to_show = array( 'pa_diameter-size' => 'Diameter/Size', 'pa_length' => 'Length' );
    
        $attributes = $product->get_attributes();
        $attrs = array();
    
        if ( $product->is_type( 'simple' ) || $product->is_type( 'external' ) || $product->is_type( 'variation' ) ) {
            if ( $attributes ) {
                foreach ( $attributes as $attribute_slug => $attribute_values ) {
                    foreach ( $attributes_to_show as $attribute_to_show_slug => $attribute_to_show_name ) {
    
                        if ( $attribute_to_show_slug === $attribute_slug ) {
    
                            $attr_name = $attribute_slug;
                            if ( is_object( $attribute_values ) ) {
                                $terms = $attribute_values->get_terms();
                                if ( $terms ) {
                                    foreach( $terms as $term ) {
                                        $attrs[$attr_name][] = $term->name;
                                    }
                                }
                            } else {
                                $attrs[$attr_name][] = $attribute_values;
                            }
    
                        }
                    }
                }
            }
        }
    
        if ( ! empty( $attrs ) ) {
            foreach( $attrs as $attr_name => $attr_terms ) {
                $result[$attr_name] = '';
                foreach ( $attr_terms as $attr_term ) {
                    $result[$attr_name] .= '<span style="display: inline-block;padding: 0 5px;border: 1px solid #ccc;margin: 5px 5px 2px 0;">' . $attr_term . '</span>';
                }
            }
        }
    
        return $result;
    }
    Plugin Author ILLID

    (@mihail-barinov)

    Looks like I know what goes wrong here.

    Please in your code replace lines

    if ( typeof result.pa_diameter-size !== "undefined") {
                         html += result.pa_diameter-size;  
                      }

    with

      if ( typeof result["pa_diameter-size"] !== "undefined") {
                         html += result["pa_diameter-size"];  
                      }
    Thread Starter classiccars13

    (@classiccars13)

    @mihail-barinov YES THAT DID IT THANK YOU FOR ALL YOUR HELP

    Plugin Author ILLID

    (@mihail-barinov)

    Glad that now all is working fine for you.

Viewing 5 replies - 16 through 20 (of 20 total)
  • The topic ‘how to split excerpt in search result’ is closed to new replies.