• Resolved danielmoorehbd

    (@danielmoorehbd)


    Hi,
    We have attributes for our products, Ingredients.
    We assign the attributes to a product and have set the ingredients to appear with image and URL, each ingredient has it’s own page, in WooCommerce terms, it is a “term” of an attribute, for example:
    /our-ingredients/pineapple
    /product_attribute/taxomony_term

    I’ve created a page under /our-ingredients/ and am looking to list ALL product attribute terms, but in A-Z order, so I can create 26 seperate loops, 1 loop for ingredients starting with A (like Apple) and one for B and so on…
    An example of the kind of thing we’re trying to pull off: https://www.templespa.com/discover-templespa-ingredients

    Note: That is not my site.

    So far, I’m using this loop which displays an array, so it can understand the fact that it’s on the “/our-ingredients/” attribute, even though I’ve cheated a bit and created a new PAGE with the slug of “our-ingredients” ??

    //product attributes
    
    $attribute_taxonomies = wc_get_attribute_taxonomies();
    $taxonomy_terms = array();
    
    //print_r($attribute_taxonomies);
    
    if ( $attribute_taxonomies ) :
        foreach ($attribute_taxonomies as $tax) :
    	
        if (taxonomy_exists(wc_attribute_taxonomy_name($tax->attribute_name))) :
            $taxonomy_terms[$tax->attribute_name] = get_terms( wc_attribute_taxonomy_name($tax->attribute_name), 'orderby=name' );
        endif;
    endforeach;
    endif;
    	
    $product_attributes = array();
    
    foreach ( $taxonomy_terms as $attribute ) {
    
    $j=0;
        foreach($attribute as $dfg){
    		//print_r($dfg->taxonomy);
    		$product_attributes[$dfg->taxonomy][$j] = $dfg->name;
    		$j++;
    	}
       
    }
    
     print_r($product_attributes);

    Appreciate the help in advance
    Kind Regards

Viewing 2 replies - 1 through 2 (of 2 total)
  • Howdy @danielmoorehbd,

    Thanks for the thorough description of what you’re wanting to do here. This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    I can also recommend the following places for more development-oriented questions:

    Cheers

    Thread Starter danielmoorehbd

    (@danielmoorehbd)

    Hi @3sonsdevelopment,

    I’ve manged to do this!
    I’ll detail my steps for any reader that wishes to follow:
    1) Make a new template file, I’ve created one called template-ouringredients.php
    2) Create a new page in WordPress, set this page template in page attributes
    (Find out how to create a template PHP file here >> https://www.cloudways.com/blog/creating-custom-page-template-in-wordpress/)
    3) Use this code:

    get_header(); ?>
    
    <center><h2 class="entry-title"><?php the_title(); ?></h2></center>
    
    	<div id="primary" class="content-area">
    		<main id="main" class="site-main" role="main">
    
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    			
    			<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>			
    			     
    			    
    			     <div class="entry">
    
    			       <?php the_content(); ?>
    
    			     </div>
    
    			     
    			 </div>
    
    			 <?php endwhile;?>
    
    			 <?php endif; ?>
    <?php
    
    $terms = get_terms("pa_our-ingredients");
    
    foreach ( $terms as $term ) {
    $image_id = get_term_meta( $term->term_id, 'image', true );
    $image_data = wp_get_attachment_image_src( $image_id, 'thumbnail' );
    echo '<div class="ingredient">';
    echo '<a href="'.get_term_link($term->slug, $term->taxonomy).'">';
    echo '<img src="' . esc_url( $image_data[0] ) . '">';
    echo "<caption>" . $term->name . "</caption>";
    echo '</a>';
    echo '</div>';
    }
    
    ?>
    
    		</main><!-- #main -->
    	</div><!-- #primary -->
    
    <style>
    div.ingredient {
    	width: 150px;
    	height: 300px;
    	float: left;
    	margin-left: 5px;
    	margin-right: 5px;
    	text-align: center;
    }
    </style>
    
    <?php
    get_footer();

    That’s it! I hope this helps someone ??

    • This reply was modified 5 years, 1 month ago by danielmoorehbd. Reason: Mark as resolved
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Creating an A-Z loop of Product Attribute Terms’ is closed to new replies.