[Resolved] Woocommerce category image on single product

Home Forums Support [Resolved] Woocommerce category image on single product

Home Forums Support Woocommerce category image on single product

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #781595
    Wim

    Took a look at the examples of elements and page headers, but no result for the moment.
    I would like each product to have a fullwidth hero banner with the image of its category.
    What shortcode could be made/inserted in the element in order to get this done?
    Thanks a million!

    #781690
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    Give this function a shot:

    add_filter( 'generate_page_hero_background_image_url', function( $url ) {
        if ( function_exists( 'is_product' ) && function_exists( 'get_woocommerce_term_meta' ) && is_product() ) {
            $terms =  get_the_terms( get_the_ID(), 'product_cat' );
            $thumbnail_id = false;
    
            if ( $terms && ! is_wp_error( $terms ) ) {
                $id = $terms[0]->term_id;
                $thumbnail_id = get_woocommerce_term_meta( $id, 'thumbnail_id', true );
            }
    
            if ( $thumbnail_id ) {
                $url = wp_get_attachment_url( $thumbnail_id );
            }
        }
    
        return $url;
    } );

    Let me know πŸ™‚

    #782849
    Wim

    Thanks Tom. Took me some tries to get this working – changed it into add_shortcode, put it into functions.php.
    Inserted the shortcode into a new element between two img tags and got the image.
    <img src="[generate_page_hero_background_image_url]">
    But of course, this is no hero banner.
    Is there any way to override the page hero background image with this one, and still use the padding, alignment, …?
    Thank you.

    #783110
    Tom
    Lead Developer
    Lead Developer

    Did my code above not work? It should replace the background image you have set with the product category image.

    #783285
    Wim

    Hi Tom, got it working through functions.php and a new element – don’t know why it gave me issues.
    What I did encounter was that {{post_terms.taxonomy}} on the page hero gives me:
    Recoverable fatal error: Object of class WP_Error could not be converted to string in /app/public/wp-content/plugins/gp-premium/elements/class-hero.php on line 848
    The title, author and other template tags work just fine.

    But okay, solved it with {{post_terms.product_cat}}, probably because it was a product πŸ˜‰

    #783834
    Tom
    Lead Developer
    Lead Developer

    Ah yes, taxonomy needs to be replaced with the actual taxonomy name.

    Glad you got it working! πŸ™‚

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.