Replace product featured image with brand image

This topic has 10 replies, 2 voices, and was last updated 4 months, 3 weeks ago ago by Luca Rossi

  • Avatar: Ahmed
    Ahmed
    Participant
    July 25, 2024 at 12:38

    Dear Team,

    We want to write a function which will replace product featured image with brand image incase there is no product featured image present.

    I have found this code snippet, can you suggest what changes would be required in the same to make it work with the xstore theme?

    Snippet:

    // run the function on only product pages and category pages
    add_action(‘init’, ‘dwd_run_brands_on_product_and_categories_only’);
    function dwd_run_brands_on_product_and_categories_only() {
    if ( (!is_admin()) && (is_product() || is_product_category())) {
    add_filter(‘woocommerce_placeholder_img_src’,’dwd_replace_placeholder_image_with_brand’);
    }
    }

    // Add brand image to products that have no image
    function dwd_replace_placeholder_image_with_brand() {
    global $product;

    $productImage = wp_get_attachment_url( $product->get_image_id() );
    if (empty($productImage) && (is_product_category() || is_product())) {
    $productBrands = wp_get_object_terms( $product->get_id(), ‘pwb-brand’ );

    foreach( $productBrands as $brand ) {
    $brandLogo = get_term_meta( $brand->term_id, ‘pwb_brand_image’, true );
    $brandLogo = wp_get_attachment_url( $brandLogo );
    return $brandLogo;
    }
    }
    }

    Please, contact administrator
    for this information.
    9 Answers
    Avatar: Justin
    Luca Rossi
    Support staff
    July 25, 2024 at 18:05

    Hi @Ahmed,

    You can try with this code:

    
    add_action('init', 'dwd_run_brands_on_product_and_categories_only');
    function dwd_run_brands_on_product_and_categories_only() {
    	if ( (!is_admin()) && (is_product() || is_product_category())) {
    		add_filter('woocommerce_placeholder_img_src','dwd_replace_placeholder_image_with_brand');
    	}
    }
    
    // Add brand image to products that have no image
    function dwd_replace_placeholder_image_with_brand() {
    	global $product;
    
    	$productImage = wp_get_attachment_url( $product->get_image_id() );
    	if (empty($productImage) && (is_product_category() || is_product())) {
    		$productBrands = wp_get_object_terms( $product->get_id(), 'brand' );
    
    		foreach( $productBrands as $brand ) {
    			$brandLogo = get_term_meta( $brand->term_id, 'brand_thumbnail_id', true );
    			$brandLogo = wp_get_attachment_url( $brandLogo );
    			return $brandLogo;
    		}
    	}
    }
    

    Please note that the issues related the custom codes must be directed to the author. They fall out of scope our support standard services.

    Thanks for understanding!

    Best Regards,
    8Theme’s Team

    Avatar: Ahmed
    Ahmed
    Participant
    July 25, 2024 at 19:16

    Dear Team,

    Thank you for the reply but the purpose of asking this question here was that you must be aware of the taxamony and meta key to use in this code…

    If found in the documentation that brands taxamony is etheme_brands and can you tell me what key should be used to make this code work.

    Avatar: Justin
    Luca Rossi
    Support staff
    July 26, 2024 at 04:25

    Hi @Ahmed,

    Did you even check our custom codes?

    We’ve changed the taxonomy and meta_key in the codes already:

    
    'brand'
    'brand_thumbnail_id'
    

    Best Regards,
    8Theme’s Team

    Avatar: Ahmed
    Ahmed
    Participant
    July 26, 2024 at 11:40

    Yes i noticed you have changed the taxamony and meta_key but the code doesnt seem to work.

    Created a test product and added new brand to test the same.

    Avatar: Justin
    Luca Rossi
    Support staff
    July 26, 2024 at 11:54

    Dear Ahmed,

    We apologize for any confusion caused in our previous communication.

    Please be informed that the correct meta_key is: thumbnail_id.

    Kindly confirm if this resolves the issue. Should you require further assistance, please do not hesitate to contact us.

    Best Regards,
    8Theme’s Team

    Avatar: Ahmed
    Ahmed
    Participant
    July 26, 2024 at 12:46

    Hi,

    I changed to code to the following but its not working either

    add_action(‘init’, ‘dwd_run_brands_on_product_and_categories_only’);
    function dwd_run_brands_on_product_and_categories_only() {
    if ( (!is_admin()) && (is_product() || is_product_category())) {
    add_filter(‘woocommerce_placeholder_img_src’,’dwd_replace_placeholder_image_with_brand’);
    }
    }

    // Add brand image to products that have no image
    function dwd_replace_placeholder_image_with_brand() {
    global $product;

    $productImage = wp_get_attachment_url( $product->get_image_id() );
    if (empty($productImage) && (is_product_category() || is_product())) {
    $productBrands = wp_get_object_terms( $product->get_id(), ‘brand’ );

    foreach( $productBrands as $brand ) {
    $brandLogo = get_term_meta( $brand->term_id, ‘thumbnail_id’, true );
    $brandLogo = wp_get_attachment_url( $brandLogo );
    return $brandLogo;
    }
    }
    }

    Avatar: Justin
    Luca Rossi
    Support staff
    July 27, 2024 at 13:43

    Hi @Ahmed,

    Please note that the custom codes will work on category archive and single product page only.

    Should you require further assistance please do not hesitate to contact our customization department at: https://www.8theme.com/account/#etheme_customization_panel.

    Thank you for your attention, and we look forward to supporting you through this upgrade process.

    Best Regards,
    The 8Theme Team

    Avatar: Ahmed
    Ahmed
    Participant
    July 27, 2024 at 15:17

    I understand that but that code isnt working on single product page nor archive page.

    Anyways thanks for your support.

    Avatar: Justin
    Luca Rossi
    Support staff
    July 27, 2024 at 18:59

    Hi @Ahmed,

    For the archive/shop page, you have to use another hook: https://stackoverflow.com/a/65992469/2660153.

    Here the code is working on the single product page:

    
    add_action('init', 'dwd_run_brands_on_product_and_categories_only');
    function dwd_run_brands_on_product_and_categories_only() {
    	if ( !is_admin() ) {
    		add_filter('woocommerce_placeholder_img_src','dwd_replace_placeholder_image_with_brand');
    	}
    }
    
    // Add brand image to products that have no image
    function dwd_replace_placeholder_image_with_brand() {
    	global $product;
    
    	$productImage = wp_get_attachment_url( $product->get_image_id() );
    	if (empty($productImage)) {
    		$productBrands = wp_get_object_terms( $product->get_id(), 'brand' );
    
    		foreach( $productBrands as $brand ) {
    			$brandLogo = get_term_meta( $brand->term_id, 'thumbnail_id', true );
    			$brandLogo = wp_get_attachment_url( $brandLogo );
    			return $brandLogo;
    		}
    	}
    }
    

    https://prnt.sc/nkEwB22rYxR_

    Best Regards,
    The 8Theme Team

  • Viewing 10 results - 1 through 10 (of 10 total)

The issue related to '‘Replace product featured image with brand image’' has been successfully resolved, and the topic is now closed for further responses

We're using our own and third-party cookies to improve your experience and our website. Keep on browsing to accept our cookie policy.