Home › Forums › WoodMart support forum › clickable brand title in meta fields
clickable brand title in meta fields
- This topic has 3 replies, 2 voices, and was last updated 3 years, 10 months ago by Artem Temos.
-
AuthorPosts
-
February 3, 2021 at 3:40 am #263417
smarterwebParticipantwe already have the brans attribute in place and it works great with brand logos.
however for brands that do not have any logos, there should be an option to display a clickable brand name, ideally under the meta tags.I found this code in template-tags.php
if( ! function_exists( ‘woodmart_get_product_attributes_label’ ) ) {
function woodmart_get_product_attributes_label(){
global $product;
$attributes = $product->get_attributes();
$output = array();
foreach ( $attributes as $attribute ) {
if ( !isset( $attribute[‘name’] ) ) continue;
$show_attr_on_product = woodmart_wc_get_attribute_term( $attribute[‘name’], ‘show_on_product’ );
if ( $show_attr_on_product == ‘on’ ) {
$terms = wc_get_product_terms( $product->get_id(), $attribute[‘name’], array( ‘fields’ => ‘all’ ) );
foreach ( $terms as $term ) {
$content = esc_attr( $term->name );
$classes = ‘label-term-‘ . $term->slug;
$classes .= ‘ label-attribute-‘ . $attribute[‘name’];$image = get_term_meta( $term->term_id, ‘image’, true );
if ( $image ) {
$classes .= ‘ label-with-img’;
$content = ‘slug ) . ‘” alt=”‘ . esc_attr( $term->slug ) . ‘” />’;
}$output[] = ‘<span class=”attribute-label product-label ‘ . esc_attr( $classes ) . ‘”>’. $content .'</span>’;
}
}
}
return $output;
}Could this be used for what we are trying to achieve?
ThanksFebruary 3, 2021 at 2:23 pm #263604
Artem TemosKeymasterHello,
Try to add the following PHP code snippet to the child theme functions.php file to do this
function woodmart_product_brand() { global $product; $attr = woodmart_get_opt( 'brands_attribute' ); if( ! $attr || ! woodmart_get_opt( 'product_page_brand' ) ) return; $attributes = $product->get_attributes(); if( ! isset( $attributes[ $attr ] ) || empty( $attributes[ $attr ] ) ) return; $brands = wc_get_product_terms( $product->get_id(), $attr, array( 'fields' => 'all' ) ); $taxonomy = get_taxonomy( $attr ); if( empty( $brands ) ) return; if ( woodmart_is_shop_on_front() ) { $link = home_url(); } else { $link = get_post_type_archive_link( 'product' ); } $classes = ( woodmart_get_opt( 'product_brand_location' ) == 'sidebar' && ! woodmart_loop_prop( 'is_quick_view' ) ) ? 'widget sidebar-widget' : ''; echo '<div class="woodmart-product-brands '. esc_attr( $classes ) .'">'; foreach ($brands as $brand) { $image = get_term_meta( $brand->term_id, 'image', true); $filter_name = 'filter_' . sanitize_title( str_replace( 'pa_', '', $attr ) ); $attrs = ''; if ( get_term_meta( $brand->term_id, 'image_id', true ) ) { $data = wp_get_attachment_image_src( get_term_meta( $brand->term_id, 'image_id', true ) ); $attrs = ' width="' . $data['1'] . '" height="' . $data['2'] . '"'; } if ( is_object( $taxonomy ) && $taxonomy->public ) { $attr_link = get_term_link( $brand->term_id, $brand->taxonomy ); } else { $attr_link = add_query_arg( $filter_name, $brand->slug, $link ); } $content = esc_attr( $brand->name ); if ( $image ) { $content = '<img src="' . esc_url( $image ) . '" title="' . esc_attr( $brand->name ) . '" alt="' . esc_attr( $brand->name ) . '" ' . $attrs . '>'; } echo '<div class="woodmart-product-brand">'; echo '<a href="' . esc_url( $attr_link ) . '">' . $content . '</a>'; echo '</div>'; } echo '</div>'; }
Regards
February 3, 2021 at 8:15 pm #263700
smarterwebParticipantThank you! That worked for us.
February 4, 2021 at 7:04 am #263781
Artem TemosKeymasterGreat, you are welcome! Feel free to contact us if you have any questions.
-
AuthorPosts
Tagged: Attributes, brands, meta
The topic ‘clickable brand title in meta fields’ is closed to new replies.
- You must be logged in to create new topics. Login / Register