Custom shortcode displaying a loop of WooCommerce product variations - php

Im trying to make a custom shortcode in order to display some product variations that have no stock quantity with stock <= 0.
Here's what I did so far:
if( ! function_exists('preorable_products') ) {
// Add Shortcode
function preorable_products( $atts ) {
global $woocommerce_loop;
// Attributes
$atts = shortcode_atts(
array(
'columns' => '4',
'limit' => '20',
'preordable' => "yes",
'stock' => 0,
),
$atts, 'preorable_products'
);
$woocommerce_loop['columns'] = $atts['columns'];
// The WP_Query
$products_variation = new WP_Query( array (
'post_type' => 'product_variation',
'post_status' => 'publish',
'fields' => 'id=>parent',
'posts_per_page' => $atts['limit'],
'meta_query' => array(
'relation' => 'AND',
'preordable' => array(
'key' =>'_ab_preorder_checkbox',
'value' => "yes",
'compare' => '=='
),
'stock' => array(
'key' =>'_stock',
'value' => 0,
'compare' => '<='
),
)
));
$products = $products_variation;
ob_start();
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
} else {
do_action( "woocommerce_shortcode_products_loop_no_results", $atts );
echo "<p>Aucun article disponible à la précommande.</p>";
}
woocommerce_reset_loop();
wp_reset_postdata();
return '<div class="woocommerce columns-' . $atts['columns'] . '">' . ob_get_clean() . '</div>';
}
add_shortcode( 'preorable_products', 'preorable_products' );
}
The WP Query part seems to work perfectly, but the code part to display the product seems wrong. I feel like $product variable doesn't have have_post method :
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
} else {
do_action( "woocommerce_shortcode_products_loop_no_results", $atts );
echo "<p>Aucun article disponible à la précommande.</p>";
}
woocommerce_reset_loop();
wp_reset_postdata();
return '<div class="woocommerce columns-' . $atts['columns'] . '">' . ob_get_clean() . '</div>';
Any help is welcome.

There are some errors and missing things in your code, use instead the following revisited code:
if( ! function_exists('get_preordable_products') ) {
function get_preordable_products( $atts ) {
// Shortcode Attributes
extract( shortcode_atts( array(
'columns' => '4',
'limit' => '20',
'preordable' => "yes",
'stock' => 0,
), $atts, 'preordable_products' ) );
// The WP_Query
$query = new WP_Query( array (
'post_type' => 'product_variation',
'post_status' => 'publish',
'posts_per_page' => $limit,
'meta_query' => array(
'relation' => 'AND',
'preordable' => array(
'key' =>'_ab_preorder_checkbox',
'value' => "yes",
'compare' => '=='
),
array(
'key' =>'_stock',
'value' => 0,
'compare' => '<='
),
)
) );
global $woocommerce_loop;
$woocommerce_loop['columns'] = $columns;
$woocommerce_loop['is_shortcode'] = 1;
$woocommerce_loop['name'] = 'preordable_products';
$woocommerce_loop['total'] = $query->post_count;
$woocommerce_loop['total_pages'] = $query->max_num_pages;
$woocommerce_loop['per_page'] = $limit;
ob_start();
if ( $query->have_posts() ) {
woocommerce_product_loop_start();
while ( $query->have_posts() ) {
$query->the_post();
wc_get_template_part( 'content', 'product' );
}
woocommerce_product_loop_end();
woocommerce_reset_loop();
wp_reset_postdata();
} else {
do_action( "woocommerce_shortcode_products_loop_no_results", $atts );
echo '<p>'. __("Aucun article disponible à la précommande.") . '</p>';
}
$content = ob_get_clean();
return '<div class="woocommerce columns-' . $columns . '">' . $content . '</div>';
}
add_shortcode( 'preordable_products', 'get_preordable_products' );
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
Usage: [preordable_products] or in Php code echo do_shortcode('[preordable_products]');

Related

How to display the easy digital download $atts inside a custom shortcode?

I'm trying to create a custom easy digital downloads (EDD) shortcode and I can't make the atts working. Meaning I would like to be able to change the category and tag names while using the shortcode, e.g :
<?php echo do_shortcode( '[my_shortcode taxonomy="download_tag" field="name" terms="Premium"]' ); ?>
But all I get is the default term ( Free ).
The code:
function my_shortcode_function($product_args, $content = null) {
global $post;
$current_page = get_query_var('paged');
$offset = $current_page > 0 ? $per_page * ($current_page-1) : 0;
$product_args = array(
'post_type' => 'download',
'posts_per_page' => '6',
'tax_query' => array(
array(
'taxonomy' => 'download_tag',
'field' => 'name',
'terms' => 'Free'
),
),
'offset' => $offset
);
$products = new WP_Query($product_args);
if ($products->have_posts()) : $i = 1;
while ($products->have_posts()) : $products->the_post();
$tags = get_the_term_list( get_the_ID(), 'download_tag' );// get tags link
$tags = strip_tags( $tags ); // get rid of the tag link
$category = get_the_term_list( get_the_ID(), 'download_category' ); // get category link
echo '<div class="col-md-4 col-lg-4">';
get_template_part( 'template-parts/content-download-card-post' );
echo '</div>';
$i+=1;
endwhile;
else :
get_template_part( 'template-parts/content', 'none' );
endif;
}
add_shortcode( 'my_shortcode', 'my_shortcode_function' );
I'm sure there are better ways to do it but I'm still new in php and the EDD docs lacks of useful examples for newbies like me. So I'll appreciate any help. Thanks a lot.
It's funny that I implemented this from a woocommerce answer and it works like a charm. Now I can change the tag, category, or the number of posts. If anyone needs it here it is:
// Creating a shortcode that displays a random product image/thumbail
if( !function_exists('prod_listing_params') ) {
function prod_listing_params( $atts ) {
ob_start();
$atts = shortcode_atts( array (
'type' => 'download',
'order' => 'date',
'orderby' => 'title',
'posts' => 6,
'category' => '', // category name
'tag' => '', // tag name
), $atts, 'list_products' );
$query = new WP_Query( array(
'post_type' => $atts['type'],
'order' => $atts['order'],
'orderby' => $atts['orderby'],
'posts_per_page' => $atts['posts'],
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'download_category',
'field' => 'name',
'terms' => $atts['category'],
),
array(
'taxonomy' => 'download_tag',
'field' => 'name',
'terms' => $atts['tag'],
)
),
) );
if ( $query->have_posts() ) { $i = 1;
?>
<?php while ( $query->have_posts() ) : $query->the_post();
$tags = get_the_term_list( get_the_ID(), 'download_tag' );// get tags link
$tags = strip_tags( $tags ); // get rid of the tag link
$category = get_the_term_list( get_the_ID(), 'download_category' ); // get category link
?>
<?php echo '<div class="col-md-4 col-lg-4">';
get_template_part( 'template-parts/content-download-card-post' );
echo '</div>'; $i+=1; ?>
<?php endwhile;
wp_reset_postdata(); ?>
<?php
$myvar = ob_get_clean();
return $myvar;
}
}
add_shortcode( 'list_products', 'prod_listing_params' );
And the use:
<?php echo do_shortcode( '[list_products tag="Free" posts="3"]' ); ?>

How to check if a product is onsale - WooCommerce

I'm trying to get onsale products in a custom query, anybody can help to form the query or the condition to make it happen...
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
echo '<ul class="owl-carousel">';
$products_onsale = array();
while ( $query->have_posts() ) : $query->the_post();
// get_template_part('template-parts/product-slider');
$product_id = get_the_ID();
$product = new WC_Product( $product_id );
if ( $product->is_on_sale() ) {
echo 'on sale';
}
endwhile;
echo '</ul>';
print_r( $products_onsale );
endif;
here is the working i'm working on
global $product;
if ( $product->is_on_sale() ) {
do_something();
}
I have two types of code for done this thing
<!-- Get WooCommerce On-Sale Products fetch -->
<ul class="get-onsale-product">
<?php
$args_for_onsale_product = array(
'post_type' => 'product',
'posts_per_page' => 4, //If you want all the post replace 4 with -1.
'meta_query' => array(
'relation' => 'OR',
array( // Simple products type
'key' => '_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
),
array( // Variable products type
'key' => '_min_variation_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
)
)
);
$onsale_product_items = new WP_Query( $args_for_onsale_product );
if ( $onsale_product_items->have_posts() ) {
while ( $onsale_product_items->have_posts() ) : $onsale_product_items->the_post();
woocommerce_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'Sorry We have no products.' );
}
wp_reset_postdata();
?>
</ul>
<!-- End WooCommerce On-Sale Products fetch -->
And second is following, Before you getting this code please review this link
<!-- Get WooCommerce On-Sale Products fetch -->
<ul class="get-onsale-product">
<?php
$args_for_onsale_product = array(
'post_type' => 'product',
'posts_per_page' => 4,
'post__in' => wc_get_product_ids_on_sale(),
);
$onsale_product_items = new WP_Query( $args_for_onsale_product );
if ( $onsale_product_items->have_posts() ) {
while ( $onsale_product_items->have_posts() ) : $onsale_product_items->the_post();
woocommerce_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'Sorry We have no products.' );
}
wp_reset_postdata();
?>
</ul>
<!-- End WooCommerce On-Sale Products fetch -->
You can use following to check if product has sale price:
$sale_price = get_post_meta( $product_id, '_sale_price', true);
If the $sale_price is greater than 0 and not empty, the product is on sale.
Hope this helps!
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => '_sale_price',
'value' => 0,
'compare' => '>'
)
),
);
$query = new WP_Query($args);
if ($query->have_posts()) :
echo '<ul class="owl-carousel">';
$products_onsale = array();
while ($query->have_posts()) : $query->the_post();
echo 'on sale';
endwhile;
echo '</ul>';
print_r($products_onsale);
endif;
It will fetch only products with sale price greater than zero ( onsale )

Including Advanced Custom Fields (ACF) in a Custom Search - Wordpress

I have created / mashed together a cool search through a specified category for my blog. Using Ajax to load the results without the reload.
When I search - no matter the term I search. I receive all posts.
I use ACF for the content & the author. I also reference products using the field featured_product_title. These fields are used within my page like this:
<?php if ( have_rows('case_study_page_content') ): ?>
<?php
while (have_rows('case_study_page_content')): the_row();
$title = get_sub_field('title');
$author = get_sub_field('author');
$content = get_sub_field('content');
?>
<div class="">
<h1 class=""><?php echo $title; ?></h3>
<h3 class=""><?php echo $author; ?></h4>
<p><?php echo $content; ?></p>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php
while (have_rows('featured_products')): the_row();
$featured_product_title = get_sub_field('featured_product_title', 'featured_products');
?>
With these in mind my current search looks like this (functions.php):
// CASE STUDY SEARCH
function my_search(){
$args = array(
'orderby' => 'date',
'order' => $_POST['date']
);
if( isset( $_POST['s'] ) ):
/*
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
's' => $_POST['s']
);
*/
if( have_rows('case_study_page_content') ):
while( have_rows('case_study_page_content') ) : the_row();
$title = get_sub_field('title');
$author = get_sub_field('author');
$content = get_sub_field('content');
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => $title,
'compare' => 'like',
'value' => '%'.$_POST['s'].'%',
),
array(
'key' => $author,
'compare' => 'like',
'value' => '%'.$_POST['s'].'%',
),
array(
'key' => $content,
'compare' => 'like',
'value' => '%'.$_POST['s'].'%',
)
)
);
endwhile;
endif;
$query = new WP_Query($args);
if( $query->have_posts() ):
while( $query->have_posts() ):
$query->the_post();
echo "<article class=\"post-box " . get_post_class() . "\">";
echo "";
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' );
echo "<img src=\"" . $url . "\" />";
echo "<h2>" . get_the_title() . "</h2>";
$case_study = get_field('case_study_page_content');
if( $case_study ):
while( have_rows('case_study_page_content') ): the_row();
$case_study_author = get_sub_field('author');
echo "<p>" . $case_study_author . "</p>";
endwhile;
endif;
echo "</article>";
endwhile;
wp_reset_postdata();
else :
echo 'No case studies found';
endif;
die();
endif;
}
add_action('wp_ajax_customsearch', 'my_search');
add_action('wp_ajax_nopriv_customsearch', 'my_search');
I guess my question is how do I add ACF's into the $args array...?
Please can someone help me successfully compare the 'key' to the 'value' in my WP_Query($args)?
Thanks everyone, Jason.
test this but without conviction
// args
$args = array(
'numberposts' => -1,
'post_type' => 'post',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'case_study_page_content_title',
'compare' => 'like',
'value' => '%'.$_POST['s'].'',
),
array(
'key' => 'case_study_page_content_author',
'compare' => 'like',
'value' => '%'.$_POST['s'].'%',
),
array(
'key' => 'case_study_page_content_content',
'compare' => 'like',
'value' => '%'.$_POST['s'].'%',
)
)
);
function custom_search_query( $query ) {
if ( !is_admin() && $query->is_search ) {
$result = $query->query_vars['s'];
$query->query_vars['s'] = '';
$query->set('meta_query', array('relation' => 'OR',
array(
'key' => 'acf_name', // ACF FIELD NAME OR POST META
'value' => $result,
'compare' => 'LIKE',
)
));
$query->set('post_type', 'post'); // optional POST TYPE
}
}
add_filter( 'pre_get_posts', 'custom_search_query');

How to implement pagination is custom shortcode?

I am using woocommerce 3.0.3, wordpress 4.7.3 .
I have created a custom shortocde with code below ( taken reference from default [products] shortcode of woocommerce ), but the pagination is not working, page 2 going blank .
<?php
add_shortcode('poster_products', 'poster_products');
if ( ! function_exists('poster_products') ){
function poster_products( $atts ) {
$atts = shortcode_atts( array(
'columns' => '4',
'orderby' => 'title',
'order' => 'asc',
'ids' => '',
'skus' => '',
'posts_per_page' => -1
), $atts, 'products' );
$query_args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'orderby' => $atts['orderby'],
'order' => $atts['order'],
'posts_per_page' => $atts['posts_per_page'],
'paged' => max( 1, get_query_var( 'paged' ) ),
'post__in' => $atts['ids'],
//'meta_query' => WC()->query->get_meta_query(),
//'tax_query' => WC()->query->get_tax_query(),
);
if ( ! empty( $atts['skus'] ) ) {
$query_args['meta_query'][] = array(
'key' => '_sku',
'value' => array_map( 'trim', explode( ',', $atts['skus'] ) ),
'compare' => 'IN',
);
}
if ( ! empty( $atts['ids'] ) ) {
$query_args['post__in'] = array_map( 'trim', explode( ',', $atts['ids'] ) );
}
return poster_product_loop( $query_args, $atts, 'products' );
}
}
if ( ! function_exists('poster_product_loop') ){
function poster_product_loop( $query_args, $atts, $loop_name ) {
global $woocommerce_loop;
$columns = absint( $atts['columns'] );
$woocommerce_loop['columns'] = $columns;
$woocommerce_loop['name'] = $loop_name;
$query_args = apply_filters( 'woocommerce_shortcode_poster_product_query', $query_args, $atts, $loop_name );
$transient_name = 'wc_loop' . substr( md5( json_encode( $query_args ) . $loop_name ), 28 ) . WC_Cache_Helper::get_transient_version( 'product_query' );
$products = get_transient( $transient_name );
if ( false === $products || ! is_a( $products, 'WP_Query' ) ) {
$products = new WP_Query( $query_args );
set_transient( $transient_name, $products, DAY_IN_SECONDS * 30 );
}
ob_start();
if ( $products->have_posts() ) {
?>
<?php do_action( "woocommerce_shortcode_before_{$loop_name}_loop", $atts ); ?>
<?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_shortcode_after_{$loop_name}_loop", $atts ); ?>
<?php
do_action('woocommerce_pagination_after_poster_product_loop', $products);
} else {
do_action( "woocommerce_shortcode_{$loop_name}_loop_no_results", $atts );
}
woocommerce_reset_loop();
wp_reset_postdata();
do_action( 'woocommerce_after_shop_loop' );
return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
}
}
add_action('woocommerce_pagination_after_poster_product_loop', 'woocommerce_pagination_after_poster_product_loop');
if ( ! function_exists( 'woocommerce_pagination_after_poster_product_loop' ) ) {
function woocommerce_pagination_after_poster_product_loop($products) {
//wc_get_template( 'loop/pagination.php' );
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
global $wp_query;
if ( $products->max_num_pages <= 1 ) {
return;
}
?>
<nav class="woocommerce-pagination">
<?php
echo paginate_links( apply_filters( 'woocommerce_pagination_args', array(
'base' => esc_url_raw( str_replace( 999999999, '%#%', remove_query_arg( 'add-to-cart', get_pagenum_link( 999999999, false ) ) ) ),
'format' => '?paged=%#%',
'add_args' => false,
'current' => max( 1, get_query_var( 'paged' ) ),
'total' => $products->max_num_pages,
'prev_text' => '←',
'next_text' => '→',
'type' => 'list',
'end_size' => 3,
'mid_size' => 3,
) ) );
} ?>
</nav>
<?php
}
Pagination creating url structure like /page/2/
Page 2 source code displaying
</nav>
<!DOCTYPE html>
and
if ( ! function_exists('poster_products') ){
echo var_dump(get_query_var( 'page' )); // ""
echo var_dump(get_query_var( 'paged' )); // ""
and
function poster_products( $atts ) {
return "poster_products getting called";
returning nothing so poster_products is not getting called on page 2.
It has been more than a month but at that time, I was done with this code which is working for me now.
/**
* Creates poster_products shortcode
*/
add_shortcode('poster_products', 'poster_products');
if ( ! function_exists('poster_products') ){
function poster_products( $atts ) {
$atts = shortcode_atts( array(
'columns' => '3',
'orderby' => 'title',
'meta_key' => '',
'order' => 'asc',
'ids' => '',
'skus' => '',
'posts_per_page' => -1
), $atts, 'products' );
$page_number = is_front_page() ? max( 1, get_query_var( 'page' ) ) : max( 1, get_query_var( 'paged' ) );
$query_args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'orderby' => $atts['orderby'],
'meta_key' => $atts['meta_key'],
'order' => $atts['order'],
'posts_per_page' => $atts['posts_per_page'],
'paged' => $page_number,
//'meta_query' => WC()->query->get_meta_query(),
//'tax_query' => WC()->query->get_tax_query(),
);
if ( ! empty( $atts['skus'] ) ) {
$query_args['meta_query'][] = array(
'key' => '_sku',
'value' => array_map( 'trim', explode( ',', $atts['skus'] ) ),
'compare' => 'IN',
);
}
if ( ! empty( $atts['ids'] ) ) {
$query_args['post__in'] = array_map( 'trim', explode( ',', $atts['ids'] ) );
}
$query_args['tax_query'][] = array(
'taxonomy' => WC_PRODUCT_VENDORS_TAXONOMY,
'operator' => 'EXISTS'
);
return poster_product_loop( $query_args, $atts, 'products' );
}
}
/**
* Adds Poster Product Grid to poster_products shortcode
*/
if ( ! function_exists('poster_product_loop') ){
function poster_product_loop( $query_args, $atts, $loop_name ) {
global $woocommerce_loop;
$columns = absint( $atts['columns'] );
$woocommerce_loop['columns'] = $columns;
$woocommerce_loop['name'] = $loop_name;
$query_args = apply_filters( 'woocommerce_shortcode_poster_product_query', $query_args, $atts, $loop_name );
$transient_name = 'wc_loop' . substr( md5( json_encode( $query_args ) . $loop_name ), 28 ) . WC_Cache_Helper::get_transient_version( 'product_query' );
$products = get_transient( $transient_name );
if ( false === $products || ! is_a( $products, 'WP_Query' ) ) {
$products = new WP_Query( $query_args );
set_transient( $transient_name, $products, DAY_IN_SECONDS * 30 );
}
ob_start();
if ( $products->have_posts() ) {
?>
<?php do_action( "woocommerce_shortcode_before_{$loop_name}_loop", $atts ); ?>
<?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_shortcode_after_{$loop_name}_loop", $atts ); ?>
<?php
do_action('woocommerce_after_sms_user_poster_product_loop', $products);
} else {
do_action( "woocommerce_shortcode_{$loop_name}_loop_no_results", $atts );
}
woocommerce_reset_loop();
wp_reset_postdata();
//do_action( 'woocommerce_after_shop_loop' );
return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
}
}

How do I get woocommerce product listing to show all products in one page?

I have the following category within a site:
http://rivetnuttool.com/site/product-category/blue-pneumatic-rivet-nut-tools/
And I want it to show all of the 7 products within the first page, and even if I have 100 products to show them in one single page.
I have tried with different solutions like adding this to my functions.php file:
add_filter( 'loop_shop_per_page', create_function( '$cols', 'return -1;' ) );
Rewriting the shortcode like the solution proposed here http://ideas.woothemes.com/forums/133476-woocommerce/suggestions/4146798-add-pagination-support-for-list-of-products-render
And several other online options, and none of them seems to work, I allways get the pagination and the limit.
The theme used in the site is Divi.
Just add the conditional check to your functions.php file:
if( isset( $_GET['showall'] ) )
{
add_filter( 'loop_shop_per_page', create_function( '$cols', 'return -1;' ) );
}
else
{
add_filter( 'loop_shop_per_page', create_function( '$cols', 'return 12;' ) );
}
loop_shop_per_page is the correct filter. If you aren't seeing any effect, then add a later priority. In my test case my parent theme was filtering loop_shop_per_page so my child theme's filter wasn't doing anything.
add_filter( 'loop_shop_per_page', 'so_31843880_show_all_products', 20 );
function so_31843880_show_all_products(){
return -1;
}
To apply the filter to only product categories you would add some conditional logic:
add_filter( 'loop_shop_per_page', 'so_show_all_products' );
function so_31843880_show_all_products($per_page){
if( is_taxonomy('product_cat') ){
$per_page = -1;
}
return $per_page;
}
I corrected and changed the custom code from the above blog post to:
function shortcode_settori( $atts ) {
global $woocommerce_loop;
extract( shortcode_atts( array(
'per_page' => '24',
'columns' => '4',
'orderby' => 'title',
'order' => 'desc',
'settore' => '', // Slugs
'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
), $atts ) );
if ( ! $settore ) {
return '';
}
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
// Default ordering args
$ordering_args = WC()->query->get_catalog_ordering_args( $orderby, $order );
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => '999',
'meta_query' => array(
array(
'key' => '_visibility',
'value' => array('catalog', 'visible'),
'compare' => 'IN'
)
),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id', //This is optional, as it defaults to 'term_id'
'terms' => $settore,
'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
)
)
);
$products = new WP_Query($args);
$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;
if($products->max_num_pages>1){
?>
<nav class="woocommerce-pagination">
<?php
echo paginate_links( apply_filters( 'woocommerce_pagination_args', array(
'base' => esc_url( str_replace( 999999999, '%#%', remove_query_arg( 'add-to-cart', get_pagenum_link( 999999999, false ) ) ) ),
'format' => '',
'current' => max( 1, get_query_var( 'paged' ) ),
'total' => $products->max_num_pages,
'prev_text' => '←',
'next_text' => '→',
'type' => 'list',
'end_size' => 3,
'mid_size' => 3
) ) );
?>
</nav>
<?php }
woocommerce_reset_loop();
wp_reset_postdata();
echo $return;
$return = '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
// Remove ordering query arguments
WC()->query->remove_ordering_args();
return $return;
}
add_shortcode( 'prodotti_per_settore', 'shortcode_settori' );
And everything works now.
Now, in 2019, I just adjust a parameter in a Customize panel by:
Apperance>Customize>WooCommerce Options>Shop Archive sidebar layout>total products per page
Then, fill the number I want into that.

Categories