duandi1636 2019-03-16 17:40
浏览 297

如何制作自定义的woocommerce类别模板?

my main problem is that I don't know which file is used as category template for all woocommerce categories. I'm trying to make my own woocommerce template (as wordpress template) and I have some problems.

This is my woocommerce.php file (as main shop page)

<?php  get_header(); ?>

<?php
if ( !is_shop()) {
     woocommerce_content();
} else {
?>

<!--Main Navigation-->
<header>

    <!--Main layout-->
<main>
    <div id="show" class="container">

        <!--Section: Dynamic Content Wrapper-->
        <section>
            <div class="dynamic-content"></div>
        </section>
        <!--Section: Dynamic Content Wrapper-->

        <!--Section: Products-->
        <section class="text-center">

            <!--Navbar-->
            <nav class="navbar navbar-expand-lg navbar-dark woobiboo-cyan my-5">

                <!-- Navbar brand -->
                <span class="navbar-brand">Categories:</span>

                <!-- Collapse button -->
                <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#basicExampleNav" aria-controls="basicExampleNav"
                    aria-expanded="false" aria-label="Toggle navigation">
                    <span class="navbar-toggler-icon"></span>
                </button>

                <!-- Collapsible content -->
                <div class="collapse navbar-collapse" id="basicExampleNav">

                    <!-- Links -->
                    <ul class="navbar-nav mr-auto">
                        <li class="nav-item active">
                            <a class="nav-link" href="#">All
                                <span class="sr-only">(current)</span>
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="#">Cat 1</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="#">Cat 2</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="#">Cat 3</a>
                        </li>

                    </ul>
                    <!-- Links -->

                    <form class="form-inline">
                        <div class="md-form mt-3 mb-0">
                            <input class="form-control mr-sm-2" type="text" placeholder="Search" aria-label="Search">
                        </div>
                        <button class="btn btn-outline-white btn-md mt-3 ml-3" type="submit"><i class="fas fa-search"></i></button>
                    </form>
                </div>
                <!-- Collapsible content -->

            </nav>
            <!--/.Navbar-->

            <!--Grid row-->

            <div class="row wow fadeIn">
            <?php
                // Define custom query parameters
                $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
                $args = array(
                    'post_type' => 'product',
                    'posts_per_page' => 30,
                    'paged'          => $paged
                    );
                $counter = 1;
                $loop = new WP_Query( $args );

                if ( $loop->have_posts() ) {
                    while ( $loop->have_posts() ) : $loop->the_post();
            ?>
                <!--Grid column-->
                <div class="col-lg-4 col-md-12 mb-4">

                    <!-- Product Card naked -->
                    <div class="card-naked">

                        <!--Featured image-->
                        <div class="view overlay hm-white-slight rounded mb-3">
                            <?php                                
                            $image = wp_get_attachment_image_src( get_post_thumbnail_id( $loop->post->ID ), 'single-post-thumbnail' );
                            $regular_price = get_post_meta( get_the_ID(), '_regular_price', true);
                            $sale_price = get_post_meta( get_the_ID(), '_sale_price', true);
                            $terms = get_the_terms( $post->ID, 'product_cat' );
                            foreach ($terms as $term) {
                                $product_cat_name = $term->name;
                                $product_cat_id = $term->term_id;
                                break;
                            }
                            ?>
                            <img src="<?php  echo $image[0]; ?>" class="img-fluid" data-id="<?php echo $loop->post->ID; ?>">
                            <a href ="<?php echo get_permalink() ?>">
                                <div class="mask"></div>
                            </a>
                        </div>

                        <!--Content-->
                        <h6 class="mb-3">
                            <a href="<?php echo esc_url( get_term_link( $product_cat_id, 'product_cat' ) ); ?>">
                                <span class="badge orange mr-1"><?php echo $product_cat_name ?></span>
                            </a>
                        </h6>
                        <h5 class="mb-3">
                            <strong><?php the_title() ?></strong>
                        </h5>
                        <p>
                            <?php if($sale_price) {
                            ?>
                            <span class="mr-1">
                                <del><?php echo $regular_price ?> zł</del>
                            </span>
                            <?php
                            }
                            ?>
                            <span>
                        <?php
                        echo  ($sale_price) ? $sale_price : $regular_price;
                        echo " zł";
                        ?></span>
                        </p>
                        <a href="<?php echo get_permalink(wc_get_page_id( 'cart' ))  . "?add-to-cart=" .  get_the_ID() ; ?> " class="btn woobiboo-cyan btn-sm" data-toggle="tooltip" data-placement="left" title="Add to cart">
                            <i class="fas fa-shopping-cart"></i>
                        </a>
                        <a href="<?php echo get_permalink() ?>" class="btn woobiboo-cyan btn-sm">Details</a>

                    </div>
                    <!-- Product Card naked -->

                </div>
                <!--Grid column-->
                <?php
                if ($counter % 3 == 0) {
                ?>
                    </div>
                    <!--Grid row-->
                    <!--Grid dynamic row-->
                    <div class="row wow fadeIn">
                <?php
                }
                $counter++;
                endwhile;
                } else {
                    echo __( 'No products found' );
                }
                // Custom query loop pagination

                ?>
            </div>
            <!--Grid row-->

        </section>
        <!--Section: Products-->

    </div>
</main>
<!--Main layout-->

</header>
<!--Main Navigation-->

<?php
} // end else (if single-product)
get_footer();
?>

And everything works perfect, but when I want to get into category page it's default woocommerce page. I tried to edit taxonomy-product_cat.php but it doesn't work (maybe because I'm using woocommerce.php file instead of archive-product.php?)

When I add something like:

'product_cat' => 'category_slug_here'

it works and shows just product from "category_slug_here" category. But I want to make it automatically (when I get to category it shows item from this category).

I hope you understand what is my problem. Maybe there is option I'll make custom php file (copy everything from woocommerce.php) and add this product_cat =>, but I think it is little stupid to make every category separated file.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 iqoo11 如何下载安装工程模式
    • ¥15 本题的答案是不是有问题
    • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
    • ¥15 C++使用Gunplot
    • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
    • ¥15 matlab数字图像处理频率域滤波
    • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
    • ¥15 ELGamal和paillier计算效率谁快?
    • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
    • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?