Woocommerce product loop - show all product variation-images - wordpress

In the wordpress front-page.php theme file i loop through all the woocoommerce products and show the featured-image.
<ul class="products">
<?php
// Setup your custom query
$args = array( 'post_type' => 'product' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li>
<a href="<?php echo get_permalink( $loop->post->ID ) ?>">
<?php the_post_thumbnail( ); ?>
</a>
</li>
<?php endwhile; wp_reset_query(); // Remember to reset ?>
</ul>
Now i have some products which are variable products (different colors). Every color variation has its own image. How can i show all the different variation-images inside this loop? My plan is to create an image-slider with these images.

<?php
$args = array( 'post_type' => 'product' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$product_s = wc_get_product( $loop->post->ID );
if ($product_s->product_type == 'variable') {
$args = array(
'post_parent' => $plan->ID,
'post_type' => 'product_variation',
'numberposts' => -1,
);
$variations = $product_s->get_available_variations();
echo '<pre>';
print_r($variations);
// You may get all images from $variations variable using loop
echo '</pre>';
}
endwhile; wp_reset_query(); // Remember to reset ?>
I do not have tested yet. But hopefully it will work.

Use below code to get image urls
For Wc3+
foreach ( $variations as $variation ) {
echo $variation['image']['url'];
}
And for old Wc versions
foreach ( $variations as $variation ) {
echo $variation['image_src'];
}

$args = array(
'post_type' => 'product',
'posts_per_page' => 10,
);
$product_arrray = get_posts($args);
foreach($product_arrray as $prod)
{
$product_id = $prod->ID;
$product = wc_get_product($product_id);
$pvariation = $product->get_available_variations();
echo "<pre>";print_r($pvariation);echo "</pre>";
}

Related

How do I show custom recent posts in WordPress?

Now it's showing like this: now it's showing like this
I want it to look like this: I want it to look like this
There are two ways to do:
In your WordPress dashboard, go to Appearance » Widgets and add the ‘Recent Posts’ widget to your sidebar.
https://prnt.sc/1rhjdc4
Using wp query
$args = array(
'post_type' => 'product',
'posts_per_page' => 10,
);
$loop = new WP_Query($args);
while ( $loop->have_posts() ) {
$loop->the_post();
?>
<div class="entry-content">
<?php the_title(); ?>
<?php the_content(); ?>
</div>
<?php
}
wp_get_recent_posts( array $args = array(), string $output = ARRAY_A )
Please read this article
https://developer.wordpress.org/reference/functions/wp_get_recent_posts/
<?php
$recent_posts = wp_get_recent_posts();
foreach( $recent_posts as $recent ) {
printf( '<li>%2$s</li>',
esc_url( get_permalink( $recent['ID'] ) ),
apply_filters( 'the_title', $recent['post_title'], $recent['ID'] )
);
}
?>

Custom WordPress PHP code to display other posts in a post breaks my comments

I'm running the following code in my WordPress site and while it works, it breaks my comments. I suspect I'm not resetting the query properly.
<?php
// name: posts you may also like
// get the current post's "post id."
$postId = get_the_ID();
$query = new WP_Query([
'post_type' => 'post',
'posts_per_page' => 5,
'post__not_in' => array($postId),
'orderby' => 'rand',
]);
$html = '<div style="font-weight:bold;">OTHER INTERESTING POSTS:</div><ul>';
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$title = get_the_title();
$permalink = get_the_permalink();
$html .= <<<EOD
<li>$title</li>
EOD;
}
}
$html .= '</ul>';
echo($html);
?>
Try the below code.
<?php
// name: posts you may also like
// get the current post's "post id."
$postId = get_the_ID();
$query = new WP_Query( [
'post_type' => 'post',
'posts_per_page' => 5,
'post__not_in' => array($postId),
'orderby' => 'rand',
] );
?>
<div style="font-weight:bold;">OTHER INTERESTING POSTS:</div>
<ul>
<?php if ( $query->have_posts() ) { while ( $query->have_posts() ) { $query->the_post();
$title = get_the_title();
$permalink = get_the_permalink(); ?>
<li><?php echo $title; ?></li>
<?php } wp_reset_postdata(); } ?>
</ul>
</div>
<?php

Woocommerce product shortcodes do not display products

I face a problem with displaying products on a specific portfolio post. I use product shortcodes in order to display specific products, but they do not appear.
I use wordpress 5.2.5, avada theme 5.9.1 and woocommerce 3.9.2.
Anyone got an idea what is going on?
If not, can anyone tell me an alternative way to display them?
use shortcode [get_specific_product_shortcode id='here enter product id']
function get_specific_product_shortcode_function($atts){
extract( shortcode_atts(
array(
'id' => '',
), $atts )
);
ob_start();
$args = array(
'posts_per_page' => 1,
'post__in' =>array($id),
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'product',
'post_status' => 'publish',
);
$query = new WP_Query( $args );
$count = $query->found_posts;
if($query->have_posts()){
while( $query->have_posts() ) { $query->the_post();
$post_thumbnail_id = get_post_thumbnail_id();
$product = wc_get_product(get_the_ID());
if (!empty($post_thumbnail_id)) {
$image_size = 'medium'; // (thumbnail, medium, large, full or custom size)
$img_url_arr = wp_get_attachment_image_src( $post_thumbnail_id, $image_size );
$img_url = $img_url_arr[0];
}else{
//placeholder image
$img_url = home_url().'/wp-content/uploads/woocommerce-placeholder.png';
}
?>
<div>
<a href="<?php echo get_permalink();?>">
<img src="<?php echo $img_url?>" style="width: 150px;">
</a>
<div>Price: <?php echo $product->get_price_html(); ?></div>
<div><?php echo get_the_title();?></div>
</div>
<?php
}
wp_reset_postdata();
}else{
echo 'no post'; echo "<br>";
}
return ob_get_clean();
}
add_shortcode('get_specific_product_shortcode', 'get_specific_product_shortcode_function');

How can you loop through wordpress posts and using posts_per_page with no duplicates on the last page?

In wordpress i am creating a custom loop/query throughwhich i pass certain parameters. As i click through pages however the last page duplicates some posts/products inorder to satisfy the posts_per_page variable however i would like to specify that i dont want any repetitions. Is there a standard way to do this? It would seem like a pretty obvious point.
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 5, 'product_cat' => 'products', 'orderby' => 'rand' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<li class="product">
<a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
<?php woocommerce_show_product_sale_flash( $post, $product ); ?>
<?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="300px" height="300px" />'; ?>
<h3><?php the_title(); ?></h3>
<span class="price"><?php echo $product->get_price_html(); ?></span>
</a>
<?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
</li>
<?php endwhile; ?>
<?php previous_posts_link('« Previous', $loop ->max_num_pages);
next_posts_link(' Next »', $loop ->max_num_pages); ?>
<?php wp_reset_query();?>
It would seem like a pretty obvious point.
Not if you're using 'orderby' => 'rand' on your query, which is also very expensive by the way on large tables.
If you want to make sure that the items which already have been displayed will be excluded in the upcoming queries you'll need to save the post_ids which already has been displayed and pass them to the post__not_in parameter, see the codex page an search for post__not_in .
You could do something like this which should help you get the idea:
...
// don't forget to initialize the session somewhere
$already_displayed_post_ids = [];
if ( ! isset( $_SESSION['already_displayed_post_ids'] ) {
$_SESSION['already_displayed_post_ids'] = $already_displayed_post_ids;
} else {
$already_displayed_post_ids = array_merge( $already_displayed_post_ids, $_SESSION['already_displayed_post_ids'] );
}
$args = [
'post_type' => 'product',
'posts_per_page' => 5,
'product_cat' => 'products',
'orderby' => 'rand',
'post__not_in' => $already_displayed_post_ids
];
$loop = new WP_Query( $args );
...

Woocomerce shop page to show category name & display products within that category

I'm developing a website with Woocommerce plugin installed. I want my shop page to show the category name first & then display the products within that category, then next category name with the products it has.
already searched in google & here, found a solution, but it is not what I want. it added the category name after the price area of every product. I'm not a pro, so having a difficult time to solve this problem.
function wc_category_title_archive_products(){
$product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' );
if ( $product_cats && ! is_wp_error ( $product_cats ) ){
$single_cat = array_shift( $product_cats ); ?>
<small class="product_category_title"><?php echo $single_cat->name; ?></small>
<?php }
}
add_action( 'woocommerce_after_shop_loop_item', 'wc_category_title_archive_products', 5 );
<?php
$cat_args = array(
'parent' => '0',
'taxonomy' => 'product_cat'
);
$categories = get_categories( $cat_args );
foreach ($categories as $category) {
echo $category->cat_name;
echo do_shortcode('[product_category category="'.$category->cat_name.'" per_page="12" columns="4" orderby="date" order="DESC"]');
}
?>
SHOW PRODUCT CATEGORY NAME ABOVE TITLE IN SHOP PAGE ==> Check on Woocomerce Version 3.5.3 You need to just copy it and paste it to your theme functions.php
function wpa89819_wc_single_product(){
$product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' );
if ( $product_cats && ! is_wp_error ( $product_cats ) ){
$single_cat = array_shift( $product_cats ); ?>
<h2 itemprop="name" class="product_category_title"><span><?php echo $single_cat->name; ?></span></h2>
<?php }
}
add_action( 'woocommerce_after_shop_loop_item_title', 'wpa89819_wc_single_product', 5 );
You're going to have to do a new loop/query for each category. We can automate some of the process by using WordPress's get_categories function. Here's an example::
<?php
$cat_args = array(
'parent' => '0',
'taxonomy' => 'product_cat'
);
$categories = get_categories( $cat_args );
foreach ($categories as $category) { ?>
<ul class="product-category">
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 1, 'cat' => $category->cat_ID, 'orderby' => 'rand' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<!-- Your output -->
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</ul>
}
?>

Resources