Woocommerce product shortcodes do not display products - wordpress

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');

Related

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

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 );
...

Woocommerce product loop - show all product variation-images

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>";
}

showing wordpress featured image in slider

I want show featured image in my slider. so what i've done in my function.php is like below:
<?php
function revconcept_get_images($post_id) {
global $post;
$thumbnail_ID = get_post_thumbnail_id();
$images = get_children( array('post_parent' => $post_id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') );
if ($images) :
foreach ($images as $attachment_id => $image) :
$img_alt = get_post_meta($attachment_id, '_wp_attachment_image_alt', true); //alt
if ($img_alt == '') : $img_alt = $image->post_title; endif;
$big_array = image_downsize( $image->ID, 'large' );
$img_url = $big_array[0];
echo '<li>';
echo '<img src="';
echo $img_url;
echo '" alt="';
echo $img_alt;
echo '" />';
echo '</li><!--end slide-->';
endforeach; endif; }
?>
<div class="flexslider"> <!-- function called in index.php -->
<ul class="slides">
<?php revconcept_get_images("$post->ID"); ?>
</ul>
</div><!--end flexslider-->
Now i have two problem. no.1 My featured image is not showing in the slider. It's showing a broken link. but it can echo the post_id. no.2 I want show only 3/4 images from the latest post of cat=5. can anyone help me to do this ? note that i am using flexslider for my slider.
Ok, this is what I've tested, you may want to refine the query a bit, but for me it worked. In functions.php put
function revconcept_get_images($post_id) {
$images = get_posts( array(
'post_type' => 'attachment',
'hide_empty' => true,
'order' => 'ASC',
'orderby' => 'menu_order ID'
));
foreach ($images as $image) {
$image_url = $image->guid;
$img_alt = get_post_meta($image->ID, '_wp_attachment_image_alt', true); //alt
echo '<li><img src="'.$image_url.'" alt="'.$img_alt.'" /></li>';
}
}
And in index.php put (where you want)
<div class="flexslider"> <!-- function called in index.php -->
<ul class="slides">
<?php revconcept_get_images($post->ID); ?>
</ul>
</div><!--end flexslider-->
If you want to show all featured images from posts you have just put 'posts_per_page' => -1, in the get_posts() array.

Give url path in woocommerce single product

I have given the a url path for image in wordpress using maya theme of woocommerce and the image is not showing the image path looks like
<img title="Adidas-H111-2" alt="Adidas-H111-2" class="attachment-shop_thumbnail" src="http://localhost/alphatest/wp-content/uploads/http://localhost/webservice/ap/upload_images/Adidas/Hard/H111/Adidas-H111-2.jpg" style="opacity: 1;">
how can I remove the part
http://localhost/alphatest/wp-content/uploads/
I am using maya theme and woocommerce plugin in wordpress. the full code is like
<?php
/**
* Single Product Thumbnails
*/
global $post, $woocommerce;
?>
<div class="thumbnails">
<?php
$thumb_id = get_post_thumbnail_id();
$small_thumbnail_size = apply_filters('single_product_small_thumbnail_size', 'shop_thumbnail');
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID,
'post__not_in' => array($thumb_id),
'post_mime_type'=> 'image',
'orderby' => 'menu_order',
'order' => 'ASC'
);
$attachments = get_posts($args);
if ($attachments) :
$loop = 0;
$columns = apply_filters('woocommerce_product_thumbnails_columns', 3);
foreach ( $attachments as $attachment ) :
if (get_post_meta($attachment->ID, '_woocommerce_exclude_image', true)==1) continue;
$loop++;
$_post = & get_post( $attachment->ID );
$url = wp_get_attachment_url($_post->ID);
$post_title = esc_attr($_post->post_title);
$image = wp_get_attachment_image($attachment->ID, $small_thumbnail_size);
echo '<a href="'.$url.'" title="'.$post_title.'" rel="thumbnails" class="zoom ';
if ($loop==1 || ($loop-1)%$columns==0) echo 'first';
if ($loop%$columns==0) echo 'last';
echo '">'.$image.'</a>';
endforeach;
endif;
?>
</div>
Simply add Double Slash (//)Instead of the Domain name. Example:
http://www.google.com/wp-content/uploads/2013/10/image.png
will be as:
//wp-content/uploads/2013/10/image.png

Resources