Answer the question
In order to leave comments, you need to log in
How to display products in a cycle on a separate page template?
Good evening. in general, I want to display products that are at a discount on a separate page (the usual shortcode will not work because I can’t display pagination), and in general I came across such an “ article ”
and copied this code onto the page
class WC_sales {
public static function sale_products( $atts ) {
global $woocommerce_loop;
$atts = shortcode_atts( array(
'per_page' => '12',
'columns' => '4',
'orderby' => 'title',
'order' => 'asc'
), $atts );
// Get products on sale
$product_ids_on_sale = wc_get_product_ids_on_sale();
$meta_query = WC()->query->get_meta_query();
$args = array(
'posts_per_page' => $atts['per_page'],
'orderby' => $atts['orderby'],
'order' => $atts['order'],
'no_found_rows' => 1,
'post_status' => 'publish',
'post_type' => 'product',
'meta_query' => $meta_query,
'post__in' => array_merge( array( 0 ), $product_ids_on_sale )
);
ob_start();
$products = new WP_Query( apply_filters( 'woocommerce_shortcode_products_query', $args, $atts ) );
$columns = absint( $atts['columns'] );
$woocommerce_loop['columns'] = $columns;
if ( $products->have_posts() ) : ?>
<?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 endif;
wp_reset_postdata();
return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
}
}
Answer the question
In order to leave comments, you need to log in
Create a separate template file in the theme and make a valid query in it https://wp-kama.ru/function/wp_query
Example:
$saleProducts = wc_get_product_ids_on_sale();
$args = array(
'post_type' => 'product',
'post__in' => array_merge(array(0), $saleProducts)
);
$query = new WP_Query($args);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question