This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

Search results content

Viewing 15 posts - 16 through 30 (of 35 total)
  • Author
    Posts
  • #53242
    erikaerika
    Participant

    Thank you so much Ernest, really appreciate this code! I think I may not be getting the config quite right with it. Sorry, i did not say the key is loaded under “erik” and not your name.

    I set “Index table option” to “Products” only (not attachment) and I “create a new index”. Under my search for a keyword, the results return the pdf that contains that keyword but does not have the excerpts. I notice that the pdf under media does not have “Content (not editable)” like the directly indexed pdfs. Also, I cannot set the remote server when choosing “Products” only – unlike “Attachment” that has the remote server set up.

    Should I choose “Attachment” as well? In fact, this message to you may be redundant. I have selected attachment as well and am currently reindexing. It takes awhile though so I wrote this message.

    #53243
    erikaerika
    Participant

    Thanks Ernest! Appreciate it! I wrote a response earlier and I am not sure where it is. So, please see the attachment called search.jpg. The search for “Chernobyl” correctly returns the product item with the attached pdf and the attachment with the excerpt. How can I get the product item to return the excerpt in the pdf indexed content?

    With the public key, I forgot to mention to use my name “erik”.

    • This reply was modified 1 year, 2 months ago by erika.
    #53263
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi!

    Well, that will need another bit of custom code for sure, as the media file ID needs to be fetched, but I think it’s possible. Try the snippet below. Make sure that the custom code I provided in my previous reply is also active, it contains a function that is needed for this code as well:

    add_filter( 'asp_results', 'asp_get_attachment_to_product_content', 10, 4 );
    function asp_get_attachment_to_product_content( $results, $search_id, $is_ajax, $args ) {
    	// --- DO NOT CHANGE ANYTHING BELOW ---
    	foreach ( $results as $k =>&$r ) {
    		if ( $r->post_type !== 'product' ) {
    			continue;
    		}
    		$product = wc_get_product( $r->id);
    
    		if ( $product->is_downloadable() ) {
    			// Loop through WC_Product_Download objects
    			foreach ( $product->get_downloads() as $key_download_id => $download ) {
    				$download_link = $download->get_file(); // File Url
    				$id            = asp_get_file_id_from_url($download_link);
    				if ( $id !== '' ) {
    					$field_value = get_post_meta( $id, '_asp_attachment_text', true );
    
    					if ( $field_value !== '' ) {
    						$r->content = wd_substr_at_word($field_value, 130);
    						break;
    					}
    				}
    			}
    		}
    	}
    
    	return $results;
    }
    #53264
    erikaerika
    Participant

    Thanks again Ernest! Do I need both settings as attached? As per your code, the pdf gets retrieved with the product – however, does it get sent to the remote server without the attachment option?

    #53266
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Yes – you need both of those settings and the files have to be uploaded to the media library, otherwise the plugin can’t access it (it has to have a valid ID in the database).

    So the best course of action is to uplod the file via the Media menu, then on the product editor add it from the library. I think when you only upload it via the product editor, it may not be registered in the media library.

    #53267
    erikaerika
    Participant

    I am doing something wrong, see attached. The product is item 1 in the search result – however it has no excerpts (I did index with the remote server option). Item 2 in the search I assume is the pdf directly accessed (not through the product). I need the product only with the excerpt and not the pdf yet. The user needs to go to the product with add to cart – which is working correctly from the search result. Thanks Ernest, I appreciate the help very much.

    #53269
    erikaerika
    Participant

    https://realacademy.net/the-knowing-field/ test with “Hutus”

    • This reply was modified 1 year, 2 months ago by erika.
    #53270
    erikaerika
    Participant

    i turned off “Return media files as results”, as attached. So nearly there. Now just for the excerpts to appear in the search results for the product with the attached pdf. By excerpt I mean indexed content/description.

    • This reply was modified 1 year, 2 months ago by erika.
    #53273
    erikaerika
    Participant

    The content has definitely been added to the attachment details, attached.

    #53278
    Ernest MarcinkoErnest Marcinko
    Keymaster

    All right, the setup seems to be okay, all you need the the excerpt.

    Make sure that this custom code is also applied with the previous ones. I can’t check as I can only see the development environment:

    add_filter( 'asp_results', 'asp_get_attachment_to_product_content', 10, 4 );
    function asp_get_attachment_to_product_content( $results, $search_id, $is_ajax, $args ) {
    	// --- DO NOT CHANGE ANYTHING BELOW ---
    	foreach ( $results as $k =>&$r ) {
    		if ( $r->post_type !== 'product' ) {
    			continue;
    		}
    		$product = wc_get_product( $r->id);
    
    		if ( $product->is_downloadable() ) {
    			// Loop through WC_Product_Download objects
    			foreach ( $product->get_downloads() as $key_download_id => $download ) {
    				$download_link = $download->get_file(); // File Url
    				$id            = asp_get_file_id_from_url($download_link);
    				if ( $id !== '' ) {
    					$field_value = get_post_meta( $id, '_asp_attachment_text', true );
    
    					if ( $field_value !== '' ) {
    						$r->content = wd_substr_at_word($field_value, 130);
    						break;
    					}
    				}
    			}
    		}
    	}
    
    	return $results;
    }

    This fetches the attachment text and replaces it in the live product results.

    #53290
    erikaerika
    Participant

    Hi Ernest, the code below is what’s in my production functions.php. It is not returning the text/content for the search results unfortunately – I did a recreate index. See attached.

    add_filter( 'asp_results', 'asp_get_attachment_to_product_content', 10, 4 );
    function asp_get_attachment_to_product_content( $results, $search_id, $is_ajax, $args ) {
    	// --- DO NOT CHANGE ANYTHING BELOW ---
    	foreach ( $results as $k =>&$r ) {
    		if ( $r->post_type !== 'product' ) {
    			continue;
    		}
    		$product = wc_get_product( $r->id);
    
    		if ( $product->is_downloadable() ) {
    			// Loop through WC_Product_Download objects
    			foreach ( $product->get_downloads() as $key_download_id => $download ) {
    				$download_link = $download->get_file(); // File Url
    				$id            = asp_get_file_id_from_url($download_link);
    				if ( $id !== '' ) {
    					$field_value = get_post_meta( $id, '_asp_attachment_text', true );
    
    					if ( $field_value !== '' ) {
    						$r->content = wd_substr_at_word($field_value, 130);
    						break;
    					}
    				}
    			}
    		}
    	}
    
    	return $results;
    }
    
    function asp_get_file_id_from_url($file_url) {
        $file_path = ltrim(str_replace(wp_upload_dir()['baseurl'], '', $file_url), '/');
    
        global $wpdb;
        $statement = $wpdb->prepare("SELECT <code>ID</code> FROM <code>wp_posts</code> WHERE guid='%s';",
            $file_url,
            $file_path);
    
        $attachment = $wpdb->get_col($statement);
        if (count($attachment) < 1) {
            return '';
        }
    
        return $attachment[0]; 
    }
    add_filter(
    	'asp_post_content_before_tokenize',
    	function ( $content, $post ) {
    		if ( $post->post_type !== 'product' ) {
    			return $content;
    		}
    		$product = wc_get_product( $post->ID );
    
    		if ( $product->is_downloadable() ) {
    			// Loop through WC_Product_Download objects
    			foreach ( $product->get_downloads() as $key_download_id => $download ) {
    				$download_link = $download->get_file(); // File Url
    				$id            = asp_get_file_id_from_url($download_link);
    				var_dump($id, $download_link);
    				if ( $id !== '' ) {
    					$field_value = get_post_meta( $id, '_asp_attachment_text', true );
    
    					if ( $field_value !== '' ) {
    						$content .= ' ' . $field_value;
    					}
    				}
    			}
    		}
    
    		return $content;
    	},
    	10,
    	2
    );
    • This reply was modified 1 year, 2 months ago by erika.
    #53293
    erikaerika
    Participant

    You cannot access this content.

    #53294
    Ernest MarcinkoErnest Marcinko
    Keymaster

    I see. On the screenshot however it seems to be the first 130characters of the PDF content though, so that is correct. I guess you want like the context around the search keyword? For that maybe try this variation, but I’m not sure if that’s even possible:

    add_filter( 'asp_results', 'asp_get_attachment_to_product_content', 10, 4 );
    function asp_get_attachment_to_product_content( $results, $search_id, $is_ajax, $args ) {
    	// --- DO NOT CHANGE ANYTHING BELOW ---
    	foreach ( $results as $k =>&$r ) {
    		if ( $r->post_type !== 'product' ) {
    			continue;
    		}
    		$product = wc_get_product( $r->id);
    
    		if ( $product->is_downloadable() ) {
    			// Loop through WC_Product_Download objects
    			foreach ( $product->get_downloads() as $key_download_id => $download ) {
    				$download_link = $download->get_file(); // File Url
    				$id            = asp_get_file_id_from_url($download_link);
    				if ( $id !== '' ) {
    					$field_value = get_post_meta( $id, '_asp_attachment_text', true );
    
    					if ( $field_value !== '' ) {
    						$r->content = \WPDRMS\ASP\Utils\Str::getContext($field_value, 130, 9999999, $args['s'], explode($args['s']));
    						break;
    					}
    				}
    			}
    		}
    	}
    
    	return $results;
    }
    #53298
    erikaerika
    Participant

    Thanks Ernest, by context I mean the same as what appears in the results with a search against a pdf. With the new code there is an error in the dropdown, please see attached. You are welcome to try ftp in. the code is under /data/realacademy…

    #53300
    Ernest MarcinkoErnest Marcinko
    Keymaster

    I see, I’m not sure if that’s possible, but let’s try to correct the code on site.

    Can you please check the back-end access details? I tried to log-in but username/pass is incorrect. Thank you!

Viewing 15 posts - 16 through 30 (of 35 total)
  • You must be logged in to reply to this topic.