0

I use the function wc_get_products to get the products I want.

$paged = (get_query_var('paged')) ? absint(get_query_var('paged')) : 1;
$ordering = WC()->query->get_catalog_ordering_args();
$ordering_explode = explode(' ', $ordering['orderby']);
$ordering['orderby'] = array_shift($ordering_explode);
$ordering['orderby'] = stristr($ordering['orderby'], 'price') ? 'meta_value_num' : $ordering['orderby'];
$products_per_page = apply_filters('loop_shop_per_page', wc_get_default_products_per_row() * wc_get_default_product_rows_per_page());

$product_ids_on_sale = wc_get_product_ids_on_sale();
$args = array(
  'status' => 'publish',
  'include' => $product_ids_on_sale,
  'stock_status' => 'instock',
  'limit' => $products_per_page,
  'page'  => $paged,
  'paginate' => true,
  'return' => 'ids',
  'orderby' => $ordering['orderby'],
  'order' => $ordering['order'],
);

$query_results = wc_get_products( $args );

wc_set_loop_prop('current_page', $paged);
wc_set_loop_prop('is_paginated', wc_string_to_bool(true));
wc_set_loop_prop('page_template', get_page_template_slug());
wc_set_loop_prop('per_page', $products_per_page);
wc_set_loop_prop('total', $query_results->total);
wc_set_loop_prop('total_pages', $query_results->max_num_pages);

And here is the loop where I display the products

    woocommerce_product_loop_start();

    foreach ( $query_results->products as $product ) {
        $post_object = get_post( $product );
        setup_postdata( $GLOBALS['post'] =& $post_object );
        wc_get_template_part( 'content', 'product' );
    }

    woocommerce_product_loop_end();

The problem is that the pagination doesn't work.

the /page/2/ redirects to 404

Your Answer

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

Browse other questions tagged or ask your own question.