Customize the product layout with specific attributes.

PHP Code Snippets/Functions

Insert this code into functions.php

remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
add_action('woocommerce_after_shop_loop_item_title', 'woocommerce_after_shop_loop_item_title_custom', 10);
function woocommerce_after_shop_loop_item_title_custom() {
	global $product;
	$attributes = $product->get_attributes();
	$colors = $attributes["pa_color"];
	$weight = $attributes["pa_fabric-weight"];
	$width = $attributes["pa_fabric-width"];
	
	$attributes = $product->get_attributes();
    $attribute_count = count( $attributes );

	if( $colors ){
		$colorTerms = $colors->get_terms();
		$selectedAttributesCount = count( $colorTerms );
		echo '<div class="product-attributes">';
		echo '<span class="product-attributes-text" id="color-text"></span>';
		echo '<span class="product-attributes-count">' . $selectedAttributesCount . '</span>';
		echo '<div class="product-colors">';
		foreach ( $colorTerms as $term ) :
			$color_value = get_term_meta( $term->term_id, 'cfvsw_color', true ); // Change 'color_value' to the actual field name storing the color value
			echo '<span class="product-color" style="background-color: ' . esc_attr( $color_value ) . ';"></span>';
		endforeach;
		echo '</div>';
		
		echo '</div>';
	}
		// Check if ACF field 'technical_specification' exists for Composition
		if ( have_rows( 'technical_specification', $product->get_id() ) ) {
			while ( have_rows( 'technical_specification', $product->get_id() ) ) {
				the_row();
				// Get sub field values.
				$term_other_descriptions = get_sub_field( 'other_descriptions' );

				if ( $term_other_descriptions ) {
					echo '<div class="product-attributes product-attributes-composition">';
					echo '<span class="product-attributes-text" id="composition-text">Composition:</span>';
					echo '<div class="product-data">' . esc_html( $term_other_descriptions ) . '</div>';
					echo '</div>';
					
				}
			}
		}	
	if( $weight ){
		$weightTerms = $weight->get_terms();
		echo '<div class="product-attributes">';
		echo '<span class="product-attributes-text">Poids:</span>';
		echo '<div class="product-data">';
		foreach ( $weightTerms as $term ) :
				$termVar = get_term( $term->term_id );
				$name = $termVar->name;
				echo '<span>'.$name.'<span class="small">g/m²</span></span>';
				break;
			endforeach;
		echo '</div>';
		echo '</div>';
	}
	if( $width ){
		$widthTerms = $width->get_terms();
		echo '<div class="product-attributes">';
		echo '<span class="product-attributes-text">Laize:</span>';
		echo '<div class="product-data">';
		foreach ( $widthTerms as $term2 ) :
				$termVar2 = get_term( $term2->term_id );
				$name2 = $termVar2->name;
				echo '<span>'.$name2.'<span class="small">cm</span></span>';
				break;
			endforeach;
		echo '</div>';
		echo '</div>';
	}
	
	wc_get_template( 'loop/price.php' );
}

Leave a Reply

Your email address will not be published. Required fields are marked *