This topic has 10 replies, 2 voices, and was last updated 3 months, 3 weeks ago ago by Luca Rossi
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;
}
}
}
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
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.
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
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.
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
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;
}
}
}
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
I understand that but that code isnt working on single product page nor archive page.
Anyways thanks for your support.
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;
}
}
}
Best Regards,
The 8Theme Team
The issue related to '‘Replace product featured image with brand image’' has been successfully resolved, and the topic is now closed for further responses