Smolikas
Smolikas

Reputation: 344

Modify Woocommerce template files to display products by product category

I'm pulling my hair over here searching and trying to modify the WooCommerce core files.

So, what I am trying to do is modify my theme's woocommerce.php file because I want to display the products on my shop by category.

What I've figured out so far is that at woocommerce.php file in my theme's root folder there is this line

<?php woocommerce_content(); ?>

which displays all of the products. Then, I found out that the file content-product.php at mytheme/woocommerce/ is being ran for each product and in a few words, that file contains the styling and classes of each product. In other words, this file is being ran inside the loop.

So I now know that I have to modify some function that actually calls that file and I want to pass the product category to that function so I can display the products I want.

Doing some digging I have found that in wp-content/plugins/woocommerce/includes/wc-template-functions.php there is the function woocommerce_content() with the following code

function woocommerce_content() {

if ( is_singular( 'product' ) ) {

    while ( have_posts() ) :
        the_post();
        wc_get_template_part( 'content', 'single-product' );
    endwhile;

} else {
    ?>

    <?php if ( apply_filters( 'woocommerce_show_page_title', true ) ) : ?>

        <h1 class="page-title"><?php woocommerce_page_title(); ?></h1>

    <?php endif; ?>

    <?php do_action( 'woocommerce_archive_description' ); ?>

    <?php if ( woocommerce_product_loop() ) : ?>

        <?php do_action( 'woocommerce_before_shop_loop' ); ?>

        <?php woocommerce_product_loop_start(); ?>

        <?php if ( wc_get_loop_prop( 'total' ) ) : ?>
            <?php while ( have_posts() ) : ?>
                <?php the_post(); ?>
                <?php wc_get_template_part( 'content', 'product' ); ?>
            <?php endwhile; ?>
        <?php endif; ?>

        <?php woocommerce_product_loop_end(); ?>

        <?php do_action( 'woocommerce_after_shop_loop' ); ?>

    <?php else : ?>

        <?php do_action( 'woocommerce_no_products_found' ); ?>

    <?php
    endif;

    }
}

Finally, I think that wc_get_loop_prop function is what initializes the loop and I am trying to find a way to pass a param to that function (the product category ID for example) so I can call woocommerce_content(118) where 118 a product category and display my products the way I want.

Is there any way I can do this? I am stuck at this part for long now and can't seem to find a solution.

Upvotes: 3

Views: 6851

Answers (1)

Smolikas
Smolikas

Reputation: 344

SOLUTION:

Finally, what I had to do was to simply create a function

function getProductsByCat($theCat) {
    $args = array(
        'post_type' => 'product',
        'posts_per_page' => 50,
        'tax_query' => array(
                array(
                    'taxonomy' => 'product_cat',
                    'field' => 'id',
                    'terms' => $theCat
                )
            )
        );
    $loop = new WP_Query( $args );
    if ( $loop->have_posts() ) {
        while ( $loop->have_posts() ) : $loop->the_post();

            wc_get_template_part( 'content', 'product' );

        endwhile;
    } else {
        return false;
    }

    return true;
}

And call it anywhere in my website to display products by cat ID like

<div class="col-md-12"><h4>Special Offers</h4></div>
<?php 
    if(!getProductsByCat(191)){
        //echo "<p>No products available.</p>";
    }
?>

I hope this will be helpful for those out there who are trying to do the same.

Upvotes: 6

Related Questions