0

I have written this code to display WooCommerce products

<div class="products-container container mb-4">

<div class="products-filter-section mt-3">
    <?php do_action('woocommerce_before_shop_loop'); ?>
</div>

<?php
while (have_posts()) :
    the_post();
    global $product;
    ?>
    <div class="product card shadow-lg mb-4">
        <a href="<?php the_permalink(); ?>">
            <img src="<?php echo get_the_post_thumbnail_url(); ?>" class="card-img-top image border-0 p-0" alt="...">
        </a>
        <div class="body card-body">
            <h3 class="h6 mb-1 title"><a class="fw-bold" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
            <?php
               if(get_field('hide_product_author_name', 'options') != '1') {
            ?>
                <div class="author">By <a href="#"><?php echo get_the_author(); ?></a></div>
            <?php
                }
            ?>
        </div>
        <div class="footer card-footer border-0 pt-0 bg-white d-flex flex-wrap">
            <div class="info align-items-center flex-grow-1">
                <div class="price">
                    <?php if ($product->get_sale_price()) : ?>
                        <?php
                           if(get_field('hide_product_author_name', 'options') != '1') {
                        ?>
                        <span class="original text-decoration-line-through fw-bold me-1"><?php echo wc_price($product->get_regular_price()); ?></span>
                        <?php
                           }
                        ?>
                        <span class="with-off fw-bold"><?php echo wc_price($product->get_sale_price()); ?></span>
                    <?php else : ?>
                        <span class="original fw-bold"><?php echo wc_price($product->get_regular_price()); ?></span>
                    <?php endif; ?>
                </div>
                <div class="d-flex rate">
                    <?php
                    for ($i = 0; $i < 5; $i++) {
                        if ($i < round($product->get_average_rating())) {
                            echo '<span class="mdi mdi-18px mdi-star"></span>';
                        } else {
                            echo '<span class="mdi mdi-18px mdi-star text-secondary"></span>';
                        }
                    }
                    ?>
                    <?php
                       if(get_field('hide_product_rate', 'options') != '1') {
                    ?>
                    (<?php echo $product->get_rating_count(); ?>)
                    <?php
                       }
                    ?>
                </div>
                <?php
                   if(get_field('hide_product_sale_count', 'options') != '1') {
                ?>
                <div class="sale"><?php echo $product->get_total_sales(); ?> Sales</div>
               <?php
                    }
                ?>
            </div>
            <div class="align-items-end d-flex gap-2 action ms-auto">

                <?php
                   if(get_field('preview_link')) {
                ?>
                <a href="<?php echo esc_url( get_field('preview_link') ); ?>" class="btn rounded px-3 preview" target="_blank">Preview</a>
                <?php
                   }
                ?>
                <?php
                   if(get_field('hide_product_add_to_cart', 'options') != '1') {
                ?>
                <?php
                $product_id = $product->get_id();
                $add_to_cart_url = wc_get_checkout_url() . '?add-to-cart=' . $product_id;
                ?>
                       <a href="<?php echo esc_url( $product->add_to_cart_url() ); ?>" class="btn rounded add-to-cart">
                           <span class="mdi mdi-cart-plus mdi-18px"></span>
                       </a>
                <?php
                    }
                ?>
            </div>
        </div>
    </div>

<?php endwhile; ?>

I have also written this code for the pagination of my products

function custom_pagination()
{
    global $wp_query;
    $big = 999999999; // need an unlikely integer

    $pagination = paginate_links(array(
        'base'      => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
        'format'    => '?paged=%#%',
        'current'   => max(1, get_query_var('paged')),
        'total'     => $wp_query->max_num_pages,
        'prev_next' => true,
        'prev_text' => '<span class="mdi mdi-18px mdi-chevron-left"></span>',
        'next_text' => '<span class="mdi mdi-18px mdi-chevron-right"></span>',
        'type'      => 'array',
        'mid_size'  => 3,
        'end_size'  => 1
    ));

    if ($wp_query->max_num_pages > 1) :
        ?>
        <ul class="pagination justify-content-center">
            <?php foreach ($pagination as $page_link) : ?>
                <li class="page-item <?php echo strpos($page_link, 'current') ? 'active' : ''; ?>">
                    <?php echo str_replace('page-numbers', 'page-link', $page_link); ?>
                </li>
            <?php endforeach; ?>
        </ul>
    <?php endif;
}

I have 10 products, but when I set the number of posts to 2 in WordPress settings and want my products to be displayed on 5 pages, nothing happens and the pagination is not displayed. Thank you for your help and tell me what is wrong with my code

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.