Ask your WordPress questions! Pay money and get answers fast! (more info)

Woocommerce add category as class WordPress

  • SOLVED

Hello, I am using a query to get my woocommerce products and output them to a page.
I want to add the woocommerce product categories as classes to the <li>.

This is the code that I am using at the moment, but it outputs the same category class to all products.

Can you see where I am going wrong?


<?php
$args = array (
'post_type' => array( 'product' ),
$termsString .= $term->slug.'',
);
$query = new WP_Query( $args );

if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>
<li class="<?php echo $termsString; ?> product">
<a href="<?php echo get_permalink(); ?>">
<?php $img_url = $image = wp_get_attachment_image_src( get_post_thumbnail_id( $loop->post->ID ), 'single-post-thumbnail' );?>
<img width="240" height="300" src="<?php echo $image[0]; ?>" class="attachment-shop_catalog size-shop_catalog wp-post-image" alt="<?php the_title(); ?>" sizes="(max-width: 240px) 85vw, 240px" data-id="<?php echo $loop->post->ID; ?>">
<h3><?php the_tile(); ?></h3>
<span class="price">
<?php if ( $price_html = $product->get_price_html() ) : ?>
<span class="amount"><?php echo $price_html; ?></span>
<?php endif; ?>
</span>
</a>
</li>
<?php }
} else {
}
wp_reset_postdata();
?>

Answers (2)

2016-05-17

Andrea P answers:

Hello!

I'm sorry but I can't understand what this bit is supposed to do?


$termsString .= $term->slug.'',


that is wrong sintax, as is not allowed as an array element.
also, that assumes that you have already set-up the variable $termString and $term somewhere else before that piece of code.
is this the case? if yes, can you post the previous part of the code please?

in any case, if you just retrieve all the products, with the query (regardless their category), then you can grab each post's categories and print the first one (you would put only one probably, but categories are potentially a list of cat).

something like this:
<?php
unset ($product_terms, $product_cat);
$product_terms = wp_get_post_terms( $post->ID, 'product_cat' );
$product_cat = $product_terms[0];
?>
<li class="<?php echo $product_cat->slug; ?> product">

2016-05-17

Rashad Aliyev answers:

Where's your <ul> ??

You should use ul tags before the loop. Also I can share with you a product by categories code with woocommerce.

<?php

$taxonomy = 'product_cat';
$orderby = 'name';
$show_count = 0; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';
$empty = 0;

$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$all_categories = get_categories( $args );
foreach ($all_categories as $cat) {
if($cat->category_parent == 0) {
$category_id = $cat->term_id;
echo '<br /><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a>';

$args2 = array(
'taxonomy' => $taxonomy,
'child_of' => 0,
'parent' => $category_id,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$sub_cats = get_categories( $args2 );
if($sub_cats) {
foreach($sub_cats as $sub_category) {
echo $sub_category->name ;
}
}
}
}
?>