How to display main category image on product detail page without addon?

Hi everyone,
I want to display the main category image of a product directly on the product detail page. I have access to $product.main_category which gives me the main category ID, but I don’t know how to load and display the category image in the product template (templates/blocks/product_templates/main.tpl) without using any addon or modifying too many files.

I tried including the image template like this:
{include file=“common/image.tpl” images=$category_images.$product.main_category.main_pair show_detailed_link=false}

But $category_images is not available in the template by default.

How can I properly load the main category data, including the image, in the controller so it’s available in the template? Or is there a simpler way to do this directly in the template?

Thanks in advance for your help!

Please try


{if $product.main_category}
    {$main_pair = $product.main_category|fn_get_image_pairs:'category':'M':true:true}
    {if $main_pair}
        {include file="common/image.tpl"
            show_detailed_link=false
            images=$main_pair
            no_ids=true
            image_id="category_image"
            image_width=$settings.Thumbnails.category_lists_thumbnail_width
            image_height=$settings.Thumbnails.category_lists_thumbnail_height
        }
    {/if}
{/if}

(!) Not tested

Thank you. I asked chatgpt :

{if $product.main_category}
{assign var=“main_pair” value=fn_get_image_pairs($product.main_category, ‘category’, ‘M’, true, true)}
{if $main_pair}
{include file=“common/image.tpl”
show_detailed_link=false
images=$main_pair
no_ids=true
image_id=“category_image”
image_width=$settings.Thumbnails.category_lists_thumbnail_width
image_height=$settings.Thumbnails.category_lists_thumbnail_height
}
{/if}
{/if}

Smarty assign instead of filter:
Instead of

{$main_pair = $product.main_category|fn_get_image_pairs:…}

use

{assign var=“main_pair” value=fn_get_image_pairs(…)}

→ This is cleaner and the standard method in CS-Cart.

Direct function call:
fn_get_image_pairs must look like this:

fn_get_image_pairs($category_id, ‘category’, ‘M’, true, true)

$category_id is $product.main_category.

Image type ‘M’:
‘M’ stands for “Main image.” If your category only has one “Additional image,” you must specify ‘A’.

Which is better and also good for performance?

There is no difference here. These are just different ways of calling the same php function