简体   繁体   English

修改Woocommerce模板文件以按产品类别显示产品

[英]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. 我在这里搜索并尝试修改WooCommerce核心文件。

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. 因此,我想做的是修改主题的woocommerce.php文件,因为我想按类别显示商店中的产品。

What I've figured out so far is that at woocommerce.php file in my theme's root folder there is this line 到目前为止,我发现主题的根文件夹中的woocommerce.php文件中存在以下行

<?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. 然后,我发现针对每个产品都运行了mytheme / woocommerce /目录中的content-product.php文件,并且用几句话说,该文件包含每个产品的样式和类。 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 做一些挖掘,我发现在wp-content / plugins / woocommerce / includes / wc-template-functions.php中有函数woocommerce_content(),其代码如下

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. 最后,我认为wc_get_loop_prop函数是初始化循环的原因,我试图找到一种将参数传递给该函数的方法(例如,产品类别ID),因此我可以调用woocommerce_content(118),其中118是产品类别并显示我想要的产品

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. 我在这部分上停留了很长时间,而且似乎找不到解决方案。

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 并在我的网站中的任何地方调用它,以按猫ID显示产品,例如

<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. 我希望这将对那些尝试这样做的人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM