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

Search results content

Viewing 5 posts - 31 through 35 (of 35 total)
  • Author
    Posts
  • #53301
    erikaerika
    Participant

    You cannot access this content.

    #53308
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Thank you!

    I think I actually got it. There was a tiny mistake in the last code snippet, I fixed it, now it seems to show the context correctly. Please note that this is not perfect, but it should work in most cases. I can’t guarantee anything though, as this is beyond normal support, but I think it should work all right.

    For future reference, the complete current solution is:

    
    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, 200, 9999999, $args['s'], explode(' ', $args['s']));
    						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
    );
    
    
    #53314
    erikaerika
    Participant

    Thank you, it is working. I very much appreciate the extra support!! This will help us very much.

    Will you integrate this code into future versions of the plugin or do I need to update functions.php with the code if I do a restore on my side?

    #53317
    Ernest MarcinkoErnest Marcinko
    Keymaster

    You are very welcome!

    This I probably can’t integrate, as it would require a lot of settings to configure correctly.

    Keep the code snippets, they will work even after update. The best place to have them is by using the Code Snippets plugin. It keeps them active inbetween updates as well.

    #53333
    erikaerika
    Participant

    great, will do.

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