1

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:

  1. Category a: 1a. Category description 1b. Category table
  2. Category b: 2a. Category description 2b. Category table

But instead, the outcome looks like this: Outcome

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.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
aln447
  • 981
  • 2
  • 15
  • 44
  • actually just a point you have a lot of open questions with no reponses / accepted answers? – David May 08 '16 at 14:37

2 Answers2

1

Hard to tell from your images but i'm nearly 100% you are not closing the table tags correctly and what you are describing is exactly what would happen in this case.

Problem is you open a table tag on one iteration point (foreach) and then add 2 rows, ok so far, on the next iteration point, you add a h2 tag inside the open table tag and so on.

The browser will attempt to fix these for you by closing the tags but it will not re-arrange your html for you, so the h tags are visible above the table in the rendered view.

See below: The table is only opened if there are posts. If you want one opened when no posts, you will need to close the table before declaring "no posts found" etc

<?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">Dostepne zabiegi</p></h3>
        <?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() ) {
                //only include the table if we have posts???

                    echo '<table class="treatments-table table products">';

                    echo '<tr class="table-heading">
                        <th class="name" id="<?php echo $sub_category->term_id;?>">Usluga</th>
                        <th>Czas trwania</th>
                        <th>Cena</th>
                        <th></th>
                        </tr>';



                 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; ?>zl</td>
                         <td><button class="button-product materialbutton">Rezerwuj</button> </td>
                    </tr> <?php

                 endwhile;

                echo '</table>'; 


             } 
             else {

                 echo __( 'No products found' );
             } ?>

             <h1>THE END</h1> <?php
} //ALL UNCLOSED TAGS ARE GETTING CLOSED AFTERWARDS -- must close tags in the loop, otherwise multiple open tags!!
David
  • 5,897
  • 3
  • 24
  • 43
  • Your code works, for everything exept displaying the products. It gives me "no products found" even if it displays the category info correctly – aln447 May 09 '16 at 11:33
  • The issue appears the same even if I give the args a static "product_cat" => 13, where 13 is a cat containing products, according to wp-admin at least – aln447 May 09 '16 at 11:41
  • FIXED! The problem was that product_cat takes the name not the id, so using ` $args = array( 'post_type' => 'product', "product_cat" => $sub_category->name //PROBLEM HERE );` Worked just fine – aln447 May 09 '16 at 11:44
1
  1. For your first problem, I am pretty sure that you need to replace category_description(); by $sub_category->description; (in the code below is $sub_cat->description;)

  2. For you second problem, I can't test it and i am not completely sure, but you need something more (see in here)…

  3. You need also closing your table with </table> (near the end) and also </div> (at the end)


<?php

    $args2 = array( 'taxonomy'  =>   'product_cat', 'parent' => 9 );
    $sub_cats = get_categories( $args2 );

    foreach( $sub_cats as $sub_cat ) { ?>
        $sub_cat_name = $sub_cat->name;
        $sub_cat_description = $sub_cat->description; // <= Here (1)
        $sub_cat_id = $sub_cat->term_id;

    <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_cat_name; ?></span>
        </h2>

        <p class="section-text">
            OPIS: <?php echo $sub_cat_description; ?>
        </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_cat_id; ?>">Usługa</th>
                <th>Czas trwania</th>
                <th>Cena</th>
                <th></th>
            </tr>
        <?php

            global $post; // Here (2)
            $terms = get_the_terms( $post->ID, 'product_cat' );
            foreach ($terms as $term) {
                $product_cat_id = $term->term_id;
                // $product_cat_name = $term->name;
                break;
            }
            $args = array( 
                'post_type' => 'product', 
                'product_cat' => $product_cat_id'
            );

            $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; ?>
        </table>
        <?php 
            } 
            else {
                echo __( 'No products found' );
            } ?>

         <h1>THE END</h1> 
         <?php
         } 
         ?>
     </div>
Community
  • 1
  • 1
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399