First and Last class within product loop issue - woocommerce

Im running a simple custom product loop. The issue im having is that the LAST class is set at the wrong time. It seems the loop_index is not correct. I should have 4 products per row, but after the 3rd the last class is set. Any ideas what the issue could be?
Thanks
$args = array(
'post_type' => 'product',
'posts_per_page' => 40
);
$loop = new WP_Query($args);
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();

Please add a filter (loop_shop_columns) to overwrite shop columns.
function loop_columns() {
return 4;
}
add_filter('loop_shop_columns', 'loop_columns', 999); //overwrite shop column filter
$args = array(
'post_type' => 'product',
'posts_per_page' => 40
);
$loop = new WP_Query($args);
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
make sure to call add_filter in relevant function only. Otherwise it will affect to all archive/category pages.

woocommerce_reset_loop();
Add this before args

Related

WooCommerce custom WP rest api endpoint does not correctly return wc_get_template_part( 'content', 'product' ) content

any help would be greatly appreciated.
wc_get_template_part( 'content', 'product' ); inside the register_rest_route callback function does not return the full product info.
It only returns the empty containing li element with its classes. Product links, image, title etc is all missing from the rest api response.
What could cause this and am I missing something simple?
Register rest route:
add_action('rest_api_init',function(){
register_rest_route("productfilter/v1", "results/(?P<proddata>[\s\S]+)", [
"methods" => "GET",
"callback" => "prod_filter_func",
'permission_callback' => '__return_true'
]);
});
Callback function:
function prod_filter_func($s){
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
);
ob_start();
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
return ob_get_clean();
}
Thank you in advance!

How to show ACF in a WP_Query loop with 'post_type' => 'product'

Try to achieve this:
I made a ACF (custom field) in my WooCommerce Product and now I try to get this field shown in my template with this code:
<ul class="products">
<?php
$args = array(
'posts_per_page' => 20,
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'product_type',
'field' => 'name',
'terms' => 'grouped',
),
),
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
$linked_with_items = the_field('linked_with_items');
the_title('<strong>', '</strong>'); echo '<br />';
echo $linked_with_items;
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
</ul><!--/.products-->
But whatever I try also with get_field() the field does not show in my template.
Can someone help?
https://www.advancedcustomfields.com/
This is the final code fyi
<?php if( have_rows('catalogue') ): ?>
<?php
while( have_rows('catalogue') ): the_row(); // catalogue is the field
the_sub_field('linked_with_items'); ?>
<?php endwhile; ?>
<?php endif; ?>
You could try with this:
$linked_with_items = get_field('linked_with_items', get_the_ID());
If that doesn't work, just as a test, you could try to simply loop over posts with a foreach
foreach ( $loop->posts as $post ) {
$linked_with_items = get_field('linked_with_items', $post->ID);
}
If none of those work, please make sure that your product actually have that custom field, double check the ACF field settings (rule section), the field slug, and double check your product edit page to see if the fields shows there.

Display Random Product Link

I need PHP-code for WordPress (Woocommerce) to display random product link.
For example, I have Product1 and want to display on this page (in description of my product):
"See also other products: [Product2 - link] and [Product3 - link]"
Don't know how, I just need php code to insert it in post/pages/products and everywhere I want on my site.
I'm not a coder and I found this code, for example, to display page title with link, but it's not what I need
<?php
echo ''.get_the_title($product_id).'';
?>
But how to get random product, don't know, thanks for help.
Try this :
$args = array(
'posts_per_page' => 1,
'orderby' => 'rand',
'post_type' => 'product' );
$random_products = get_posts( $args );
foreach ( $random_products as $post ) : setup_postdata( $post ); ?>
<?php the_title(); ?>
<?php endforeach;
wp_reset_postdata();
The Perfect solution for outputting a single random product which can be achieved using the following code.
<?php
$post_array=array();
$args = array(
'post_type' => 'product',
'posts_per_page' => 12
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
array_push($post_array,get_the_ID());
endwhile;
$random_key = array_rand($post_array, 1);
echo ''.get_the_title($post_array[$random_key]).'';
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
Have tested the same for you. It worked. Lemme Know if the same worked for you too.

Woocommerce update sku of all products? filter?

I'd like to set the SKU to the ID of the product.. but cannot find the right filter to use?
Is there a filter that sends the product SKU?
add_filter( 'woocommerce_get_sku', 'update_sku', 10, 1);
function update_sku( $sku ){
//set $newsku as product id
return $newsku;
}
How do i accomplish this? I have 1000s of products without a SKU. I've tried a plugin called SKU generator but that doesn't work. Any help appreciated.
You can do it by adding following code to your functions.php file and after adding go to you site example.com for only once.
After that remove below code from the functions.php.
add_filter( 'init', 'update_sku', 10, 1);
function update_sku( $sku ){
$args = array(
'post_type' => 'product',
'posts_per_page' => 12
);
$i=0;
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
$random_sku = mt_rand(100000, 999999);
update_post_meta($loop->post->ID,'_sku',$random_sku);
$i++;
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
}
NOTE : If you don't remove after one use, products SKU will change everytime you visit any page of your site.
Add it as shortcode and make SKU same as Product id.
add_shortcode( 'update_sku', 'update_sku', 10, 1);
function update_sku( $sku ){
$args = array(
'post_type' => 'product',
'posts_per_page' => 12
);
$i=0;
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
update_post_meta($loop->post->ID,'_sku',$loop->post->ID);
$i++;
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
}
Then use this shortcode in a page and open it once. so it will not re-create sku's every time.

Wordpress custom taxonomy pagination not working

I’m using the WP PageNavi plugin for pagination. This particular problem in not getting the taxonomy-portflio-category.php page to paginate is also a problem when WP PageNavi is turned off.
I’ve had a heck of a time getting pagination to work on the homepage and on a page template page, but I did get them to work. Here’s their code:
page-home.php (used as a Page template on a static front page called “Home”)
$paged = 1;
if ( get_query_var('paged') ) $paged = get_query_var('paged');
if ( get_query_var('page') ) $paged = get_query_var('page');
$i = 0;
$loop = new WP_Query( array( 'post_type' => 'portfolio', 'paged' => $paged, 'posts_per_page' => 24 ) );
while ( $loop->have_posts() ) : $loop->the_post();
// output
$i++; endwhile;
if ( function_exists( 'wp_pagenavi' ) ) {
wp_pagenavi( array( 'query' => $loop ) );
wp_reset_postdata();
}
Pagination works!
page-portfolio.php (used as a Page template on a Page called “Work”)
$i = 0;
$loop = new WP_Query( array( 'post_type' => 'portfolio', 'paged' => get_query_var( 'paged' ), 'posts_per_page' => 24 ) );
while ( $loop->have_posts() ) : $loop->the_post();
// output
$i++; endwhile;
if ( function_exists( 'wp_pagenavi' ) ) {
wp_pagenavi( array( 'query' => $loop ) );
wp_reset_postdata();
}
Pagination works!
taxonomy-portfolio-category.php (used as a way to display portfolio sections e.g. print, photography, etc.)
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
global $wp_query;
query_posts( array_merge( $wp_query->query, array( 'posts_per_page' => 2 ) ) );
if (have_posts()) : while ( have_posts() ) : the_post();
// output
endwhile; endif;
if ( function_exists( 'wp_pagenavi' ) ) {
wp_pagenavi();
}
Page 1 (/portfolio/interactive/) looks great! It’s definitely only posting 2 items and it calculates the correct number of pagination pages. But when you click on page 2 or 3 or 4 (/portfolio/interactive/page/2/) the site defaults to index.php and shows “Page not found”. Pagination fails!
Hopefully I can resolve this soon. I’ve seen A LOT of people with this same problem of pagination on custom taxonomy pages, but no solid solutions. Please help!
You Need to set posts per page to 24 on the Settings -> Reading page in WP admin. Hope this helps someone.
I have tried using WP-Pagenavi but it never worked so i used the pagination from Wordpress it self, i used the twentyfourteen_paging_nav() function form Twentyfourteen becuse it has a taxonomy page, here is the code:
if ( ! function_exists( 'twentyfourteen_paging_nav' ) ) :
function twentyfourteen_paging_nav() {
global $wp_query, $wp_rewrite;
// Don't print empty markup if there's only one page.
if ( $wp_query->max_num_pages < 2 ) {
return;
}
$paged = get_query_var( 'paged' ) ? intval( get_query_var( 'paged' ) ) : 1;
$pagenum_link = html_entity_decode( get_pagenum_link() );
$query_args = array();
$url_parts = explode( '?', $pagenum_link );
if ( isset( $url_parts[1] ) ) {
wp_parse_str( $url_parts[1], $query_args );
}
$pagenum_link = remove_query_arg( array_keys( $query_args ), $pagenum_link );
$pagenum_link = trailingslashit( $pagenum_link ) . '%_%';
$format = $wp_rewrite->using_index_permalinks() && ! strpos( $pagenum_link, 'index.php' ) ? 'index.php/' : '';
$format .= $wp_rewrite->using_permalinks() ? user_trailingslashit( $wp_rewrite->pagination_base . '/%#%', 'paged' ) : '?paged=%#%';
// Set up paginated links.
$links = paginate_links( array(
'base' => $pagenum_link,
'format' => $format,
'total' => $wp_query->max_num_pages,
'current' => $paged,
'mid_size' => 1,
'add_args' => array_map( 'urlencode', $query_args ),
'prev_text' => __( '← Previous', 'twentyfourteen' ),
'next_text' => __( 'Next →', 'twentyfourteen' ),
) );
if ( $links ) :
?>
<nav class="pagination-contaner" role="navigation">
<h1 class="screen-reader-text"><?php _e( 'Posts navigation', 'twentyfourteen' ); ?></h1>
<ul class="pagination">
<?php echo $links; ?>
</ul><!-- .pagination -->
</nav><!-- .navigation -->
<?php
endif;
}
endif;
I ran into similar issue, it took me hours of googling! I found the solution at last.
Add the following code to functions.php in your theme folder:
$option_posts_per_page = get_option( 'posts_per_page' );
add_action( 'init', 'my_modify_posts_per_page', 0);
function my_modify_posts_per_page() {
add_filter( 'option_posts_per_page', 'my_option_posts_per_page' );
}
function my_option_posts_per_page( $value ) {
global $option_posts_per_page;
if ( is_tax( 'portfolio-category') ) {
return 2;
} else {
return $option_posts_per_page;
}
}
URL for the solution
May be you need to enable search to enable pagination
While declaring custom taxonomy you should disable search excluding.
exclude_from_search => false
This fixed my problem.
I would like to share following solution (add this code to functions.php in your theme):
function fix_taxonomy_pagination ( $query ) {
// not an admin page and it is the main query
if (!is_admin() && $query->is_main_query()){
if(is_tax()){
// where 24 is number of posts per page on custom taxonomy pages
$query->set('posts_per_page', 24);
}
}
}
add_action( 'pre_get_posts', 'fix_taxonomy_pagination' );
source

Resources