Home Forums WoodMart support forum Label on product doesn’t work Reply To: Label on product doesn’t work

#413918

Giovanni
Participant

ok i found the bug…se here:

/**
 * ------------------------------------------------------------------------------------------------
 * Attribute on product element
 * ------------------------------------------------------------------------------------------------
 */
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';

						if ( is_array( $image ) && isset( $image['id'] ) ) {
							$content = wp_get_attachment_image( $image['id'], 'full', false, array( 'title' => $term->slug ) );
						} else {
							$content = '<img src="' . esc_url( $image ) . '" title="' . esc_attr( $term->slug ) . '" alt="' . esc_attr( $term->slug ) . '" />';
						}
					}

					$output[] = '<span class="attribute-label product-label ' . esc_attr( $classes ) . '">' . $content . '</span>';
				}
			}
		}
		return $output;
	}
}

the problem i suppose is that $image always return something so is always true and so i solved using

if ( strlen($image['url'])>0 ) {

i hope that this is useful for you 🙂