0

I have created a shortcode to show all woocommerce products in a new page with all woocommerce filter and pagination but it seems only products are showing

ob_start();

        $products = new WP_Query( apply_filters( 'woocommerce_shortcode_products_query', $args, $atts ) );

        if ( $products->have_posts() ) : ?>

            <?php do_action( 'woocommerce_before_shop_loop' ); //this would show post count and filter ?>
            <?php woocommerce_product_loop_start(); ?>

                <?php while ( $products->have_posts() ) : $products->the_post(); ?>

                    <?php wc_get_template_part( 'content', 'product' ); ?>

                <?php endwhile; // end of the loop. ?>

            <?php woocommerce_product_loop_end(); ?>
            <?php do_action( 'woocommerce_after_shop_loop' ); //this would show pagination ?>

        <?php endif;?>

        <?php
            wp_reset_postdata();
        ?>
        <?php
        return '<div class="woocommerce columns-' . $column . '">' . ob_get_clean() . '</div>';

According to woocommerce archive-products.php template woocommerce_before_shop_loop should show count and filter

And woocommerce_after_shop_loop show pagination but here its not working.Any alternative to show them?

Full shortcode for wc-visual composer : https://pastebin.com/crv4jwsz

1

In core WooCommerce, the pagination is added via the woocommerce_pagination() function hooked to woocommerce_after_shop_loop, and the sorting and result counts are output by the woocommerce_result_count() and woocommerce_catalog_ordering() functions. If you look at the source for these functions, you can see that they display according to the main $wp_query, which will not contain any products because you're querying posts with your own WP_Query.

So this might be one of those very rare occasions where query_posts() would be the right thing to do. If you use query_posts() in place of a secondary WP_Query, then those template functions should reflect your custom query.

ob_start();

query_posts( apply_filters( 'woocommerce_shortcode_products_query', $args, $atts ) );

if ( have_posts() ) : ?>

    <?php do_action( 'woocommerce_before_shop_loop' ); //this would show post count and filter ?>
    <?php woocommerce_product_loop_start(); ?>

        <?php while ( have_posts() ) : the_post(); ?>

            <?php wc_get_template_part( 'content', 'product' ); ?>

        <?php endwhile; // end of the loop. ?>

    <?php woocommerce_product_loop_end(); ?>
    <?php do_action( 'woocommerce_after_shop_loop' ); //this would show pagination ?>

<?php endif;?>

<?php 
wp_reset_postdata(); 
wp_reset_query(); 

return '<div class="woocommerce columns-' . $column . '">' . ob_get_clean() . '</div>';

Take note of the wp_reset_query(), that's very important here.

I'm not sure pagination will work correctly though, you might need to use your own instance of paginate_links(), refer to the documentation in the codex for examples of using paginate_links() with a custom query.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.