I have a php/wp script written which in theory should print a div with a category name, description, and all of its products inside. The code looks like this:
<?php
$args2 = array( 'taxonomy' => 'product_cat', 'parent' => 9 );
$sub_cats = get_categories( $args2 );
foreach( $sub_cats as $sub_category ) { ?>
<div class="treatments-description col-md-9" id="<?php echo $sub_category->term_id;?>">
<h2 class="section-heading">
<span class="line-behind-text"><?php echo $sub_category->name;?></span>
</h2>
<p class="section-text">
OPIS: <?php echo category_description(); ?> //this part does not work too, not sure why
</p>
<h3 class="section-heading"><p class="line-behind-text">Dostępne zabiegi</p></h3>
<table class="treatments-table table products">
<tr class="table-heading">
<th class="name" id="<?php echo $sub_category->term_id;?>">Usługa</th>
<th>Czas trwania</th>
<th>Cena</th>
<th></th>
</tr> <?php
$name = $sub_category->name;
$args = array( 'post_type' => 'product',
"product_cat" => $sub_category->term_id //PROBLEM HERE
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
$product = new WC_Product(get_the_ID()); ?>
<tr>
<td class="name"><?php the_title(); ?><p class="small"><?php the_content(); ?></p></td>
<td><?php the_excerpt(); ?></td>
<td><?php echo $product->price; ?>zł</td>
<td><button class="button-product materialbutton">Rezerwuj</button> </td>
</tr> <?php
endwhile;
}
else {
echo __( 'No products found' );
} ?>
<h1>THE END</h1> <?php
} //ALL UNCLOSED TAGS ARE GETTING CLOSED AFTERWARDS
Now in theory it should display a the divs like this:
- Category a: 1a. Category description 1b. Category table
- Category b: 2a. Category description 2b. Category table
But instead, the outcome looks like this:
So as you see not only does it not lay out the page correctly (the order is descrption1, description2, table1, table2, mind the location of <h1>THE END</h1>
), it also doesn't seem to correctly match the products of category. The same result happens when Id put in the array
"product_cat" => 14 //proven category id, containing posts
I'm experienced in wp, but fairly new to woocommerce. If anyone could help me with these issues, It would be gladly appreciated.