Variable product image Will not display on single product view - php

My theme uses custom code to display the Woocommerce single product image but the variable product images will not display. The feature image shows and the gallery images show. When using the woocommerce standard template from product-image.php the variable image works but I do not want to use standard template because it does not work well with my theme (gallery image slider not working properly). here is the custom code below. Any idea what's missing or what can be done to get the variable images to show up?
Thanks in advance.
$image_size = array(500,600);
$url_image_default = ST_MaxShop_Assets::url('images/img-default.png');
$attachment_ids = $product->get_gallery_image_ids();
$post_thumbnail_id = $product->get_image_id();
$image_url = wp_get_attachment_image_src( get_post_thumbnail_id( $product->get_id(), apply_filters( 'single_product_large_thumbnail_size', 'shop_single' ) ) );
?>
<div class="<?php echo esc_attr( implode( ' ', array_map( 'sanitize_html_class', $wrapper_classes ) ) ); ?>" data-columns="<?php echo esc_attr( $columns ); ?>" style="opacity: 1; transition: opacity .25s ease-in-out;width:100%">
<div class="clearfix">
<figure class="woocommerce-product-gallery__wrapper">
<?php
if ( has_post_thumbnail() ) {
?>
<a class="jqzoom" href="<?php echo get_the_post_thumbnail_url($product->get_id() , array(1500,1800)); ?>" id="jqzoom" data-rel="gal-1">
<?php
$props = wc_get_product_attachment_props( get_post_thumbnail_id(), $post );
?>
<img src="<?php echo $image_url[0]; ?>" title="<?php echo esc_attr($props['title']); ?>" alt ="<?php echo esc_attr($props['alt'])?>">
<?php
?>
</a>
<?php
} else { ?>
<a class="jqzoom" href="<?php echo esc_url($url_image_default); ?>" id="jqzoom" data-rel="gal-1">
<?php
$dimensions = wc_get_image_size( $image_size ); ?>
<img src="<?php echo esc_url($url_image_default); ?>" width="<?php echo esc_attr( $dimensions['width'] ) ?> " height="<?php echo esc_attr( $dimensions['height'] ) ?> " alt="<?php esc_html_e('Image Default','maxshop')?>" title="<?php esc_html_e('product','maxshop')?>" />
<?php
?></a><?php
}
?>
</figure>
</div>
</div>
<?php
if($attachment_ids){
?>
<ul class="jqzoom-list">
<?php
if ( has_post_thumbnail() ) { ?>
<li>
<a class="zoomThumbActive" href="javascript:void(0)" data-rel="{gallery:'gal-1', smallimage: '<?php echo get_the_post_thumbnail_url($post -> ID,$image_size); ?>', largeimage: '<?php echo get_the_post_thumbnail_url($post->ID , array(1500,1800)); ?>'}">
<?php echo get_the_post_thumbnail( $post->ID, array(100,100)); ?>
</a>
</li>
<?php } ?>
<?php
foreach ($attachment_ids as $attachment_id){ ?>
<li>
<a href="javascript:void(0)" data-rel="{gallery:'gal-1', smallimage: '<?php echo esc_url(wp_get_attachment_image_url($attachment_id,$image_size)); ?>', largeimage: '<?php echo esc_url(wp_get_attachment_image_url($attachment_id, array(1500,1800))); ?>'}">
<?php
echo wp_get_attachment_image($attachment_id,array(100,100)); ?>
</a>
</li>
<?php
}
?>
</ul>
<?php
}
}

as i can see you are getting only the main product image at this line
$attachment_ids = $product->get_gallery_image_ids();
and you are looping through all of them withing your code and in order to get the variation images you need to add the following code as follow:
$image_size = array(500,600);
$variation_image_id = array();
$attachment_ids = $product->get_gallery_image_ids();
$variations = $product->get_available_variations();
foreach ( $variations as $variation ) {
$variation_image_id[]= $variation["image_id"];
}
$attachment_ids = array_merge( $variation_image_id , $attachment_ids);
$post_thumbnail_id = $product->get_image_id();

Related

ACF Gallery Custom Shortcode

Hello I want to create custom shortcode for ACF gallery. I tried to add the below code in the functions.php in my child theme. But I am getting WordPress critical error.
https://gist.github.com/mwangepatrick/01cbf679c3c9e6256c8483c337c274d3
<?php
add_shortcode('display_acf_gallery', 'acf_gallery');
function acf_gallery ( $atts ) {
if ( empty ( $atts['name'] ) ) return "";
$images = get_field( $atts['name'] );
if( $images ): ?>
<?php foreach( $images as $image ): ?>
<div class="thumbnail" style="float:left;" ><img src="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" /></div>
<?php endforeach; ?>
<?php endif;
}

Display a second WooCommerce image from product gallery on the loop

i'm using this code to display woocommerce thumb:
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $loop->post->ID ), 'single-post-thumbnail' );?>
<img src="<?php echo $image[0]; ?>" data-id="<?php echo $loop->post->ID; ?>">
But looking solution how to display second image from product gallery. How to modify this code?
Updated: The following will display a second image from product gallery (first image):
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $loop->post->ID ), 'single-post-thumbnail' ); ?>
<img src="<?php echo $image[0]; ?>" data-id="<?php echo $loop->post->ID; ?>">
<?php
// Get gallery images IDs
if( $gallery_image_ids = get_post_meta( $loop->post->ID, '_product_image_gallery', true ) ) :
// Convert a coma separated string of Ids to an array of Ids
$gallery_image_ids = explode(',', $gallery_image_ids);
// Display the first image (optional)
?><img src="<?php echo wp_get_attachment_image_url( reset($gallery_image_ids), 'single-post-thumbnail'); ?>" data-id="<?php echo $loop->post->ID; ?>">
<?php
// Display the 2nd image (if it exists)
if( isset( $gallery_image_ids[1] ) ) :
?><img src="<?php echo wp_get_attachment_image_url( $gallery_image_ids[1], 'single-post-thumbnail'); ?>" data-id="<?php echo $loop->post->ID; ?>">
<?php endif; ?>
<?php endif; ?>
Related : How to show all images in WooCommerce product description

Get the permalink from a loop displaying custom taxonomy titles and images

I have a custom post type called projects and a custom taxonomy attached to that called sectors. I have also added the ability to add an image to each taxonomy using Advanced Custom Fields.
I'm displaying a list of the sectors on the homepage of my site which consists of the title of the taxonomy along with its image here:
So far, so good...
I'm struggling with surrounding either the image or title with the permalink for the taxonomy. I'd like to be able to click on it and it takes me to a page showing all of the projects inside that sector.
My loop is as follows:
<div class="container">
<div class="row no-gutters">
<?php
// loop through terms of the Sectors Taxonomy
$getArgs = array(
'parent' => 0,
'order' => 'DESC',
'orderby' => 'count',
'hide_empty' => false,
);
// get the taxonomy we are going to loop through.
$taxonomy = get_terms('sectors', $getArgs);
$terms = get_terms($taxonomy);
// Start the loop through the terms
foreach ($taxonomy as $term) {
// Get acf field Name
$image = get_field('sector_image', $term );
$url = $image['url'];
$title = $image['title'];
$alt = $image['alt'];
// which size?
$size = 'large';
$thumb = $image['sizes'][ $size ];
?>
<div class="col-md-6">
<div class="sector-item-container" style="position: relative;">
<div class="box-overlay"><?php echo $term->name; ?></div><!-- box overlay -->
<a href="<?php the_permalink(); ?>">
<img src="<?php echo $thumb; ?>" alt="<?php echo $alt; ?>" title="<?php echo $title; ?>" />
</a>
</div>
</div>
<?php } // end foreach ?>
</div>
</div>
I've tried adding tags like this around the elements I want to be clickable:
<a href="<?php the_permalink(); ?>">
</a>
or
<a href="<?php echo get_term_link($term->slug, $taxonomy); ?>">
</a>
But they don't seem to pull the links through...
Can anyone see what I'm doing wrong? I appreciate any insight at all :)
***** EDIT ***************
This seems to show a list of the taxonomies and also apply the link to them... so it's somehow a mix of both I need I think, this following code works but without the images!...
<?php
$taxonomy = 'sectors';
$terms = get_terms($taxonomy); // Get all terms of a taxonomy
if ( $terms && !is_wp_error( $terms ) ) :
?>
<div class="container-flex">
<div class="row no-gutters">
<?php foreach ( $terms as $term ) { ?>
<div class="col-md-6">
<div class="sector-item-container" style="position: relative;">
<a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>">
<div>
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail('page-thumb-mine');
}
?>
<?php
$image = get_field('sector_image');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
</div>
</a>
<div class="sector-item-title">
<a href="<?php echo get_term_link($term->slug, $taxonomy); ?>">
<h4><?php echo $term->name; ?></h4>
</a>
</div>
</div>
</div>
<?php } ?>
</div>
</div>
<?php endif;?>
<?php wp_reset_postdata(); ?>
This is from the codex on https://developer.wordpress.org/reference/functions/get_term_link/.
$terms = get_terms( 'species' );
echo '<ul>';
foreach ( $terms as $term ) {
// The $term is an object, so we don't need to specify the $taxonomy.
$term_link = get_term_link( $term );
// If there was an error, continue to the next term.
if ( is_wp_error( $term_link ) ) {
continue;
}
// We successfully got a link. Print it out.
echo '<li>' . $term->name . '</li>';
}
echo '</ul>';
For your specific case, try swapping the above with this: $terms = get_terms( 'sectors' );
Edit:
As for your images, retrieve them with get_field('sector_image', $id_of_term_or_post_or_whatever); Be sure to echo it.
$image = get_field('sector_image', id_of_term_or_post_or_whatever);
echo $image; //this might be $image['url'] or whatever, depending on how you set up ACF
Ok, I think I actually managed to combine the two bits of code like this and it seems to work!
<?php
$taxonomy = 'sectors';
$terms = get_terms($taxonomy); // Get all terms of a taxonomy
if ( $terms && !is_wp_error( $terms ) ) :
?>
<div class="container-flex">
<div class="row no-gutters">
<?php foreach ( $terms as $term ) {
$image = get_field('sector_image', $term );
$url = $image['url'];
$title = $image['title'];
$alt = $image['alt'];
$size = 'large';
$thumb = $image['sizes'][ $size ];
?>
<div class="col-md-6">
<div class="sector-item-container" style="position: relative;">
<a href="<?php echo get_term_link($term->slug, $taxonomy); ?>">
<div>
<img src="<?php echo $thumb; ?>" alt="<?php echo $alt; ?>" title="<?php echo $title; ?>" />
</div>
</a>
<div class="sector-item-title">
<a href="<?php echo get_term_link($term->slug, $taxonomy); ?>">
<h4><?php echo $term->name; ?></h4>
</a>
</div>
</div>
</div>
<?php } ?>
</div>
</div>
<?php endif;?>
<?php wp_reset_postdata(); ?>

How Do I Apply Custom Field Link to Slide Button?

I created a relational custom field (slide_link) to link my slides to pages, but i am having difficulty applying the link to my slider button in the home.php file. Here are my codes:
<div class="flexslider">
<ul class="slides">
<?php
$query = new WP_Query( array('post_type' => 'slide') );
while ( $query->have_posts() ) : $query->the_post();
?>
<?php
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnail_size' );
$url = $thumb['0'];
?>
<?php
global $post;
$meta = get_post_meta( $post->ID );
$captn = isset( $meta['caption'][0] ) ? filter_var( $meta['caption'][0], FILTER_SANITIZE_STRING ) : '';
$slideurl = isset( $meta['slide_link'][0] ) ? filter_var( $meta['slide_link'][0], FILTER_SANITIZE_STRING ) : '';
?>
<li data-thumb="<?php echo $url; ?>">
<img src="<?php echo $url; ?>" />
<p class="flex-caption"> <?php echo($captn); ?> </p>
See More
</li>
<?php endwhile; ?>
</ul>
</div><!-- /home banner -->
Function.php where I registered the custom field
//function to register vision field
add_filter('the_permalink', 'getCustomFeature6');
function getCustomFeature6($slideurl) {
global $post;
$meta = get_post_meta($post->ID, 'slider_link', true);
return $slideurl;
}
Any help would be appreciated!
I do not understand why are you using add_filter('the_permalink', 'getCustomFeature6'); . I would use this:
<?php $slideurl = get_post_meta($post->ID, "slider_link", true); ?>
<li data-thumb="<?php echo $url; ?>">
<img src="<?php echo $url; ?>" />
<p class="flex-caption"> <?php echo($captn); ?> </p>
See More
</li>

Get Products within Current Product Category

I'm currently playing around with WOOCOMMERCE V2.0.13 and I'm trying to display each product from the current product category (e.g. Construction Products when on the Construction Page), I've managed to display the single products from within the current category but if the product is also in another category (e.g Construction and Enviroment) then the current category breaks and shows zero products either from Construction or Enviroment.
If I could get some advice/help on displaying products from the current category and allow it to work with products that are in multiple categories I'd trully apreciate the help and time.
I'm more than happy to recode this entire section to make it work, here is my code below please let me know if I've missed anything.
Thank you.
<ul class="products">
<?php
global $post, $product;
$categ = $product - > get_categories();
$categ2 = preg_replace('/<a href=\"(.*?)\">(.*?)<\/a>/', "\\2", $categ);
?>
<?php
global $product;
$args = array('post_type' = > 'product', 'posts_per_page' = > '999', 'product_cat' = > $categ2, );
$loop = new WP_Query($args);
while ($loop - > have_posts()): $loop - > the_post();
global $product;
?>
<li>
<a href = "<?php echo get_permalink(); ?>">
<?php
if (has_post_thumbnail()) {
$image = get_the_post_thumbnail($post - > ID, apply_filters('single_product_large_thumbnail_size', 'shop_single'));
$image_title = esc_attr(get_the_title(get_post_thumbnail_id()));
$image_link = get_permalink($product_id);
$attachment_count = count($product - > get_gallery_attachment_ids());
echo apply_filters('woocommerce_single_product_image_html', sprintf('%s', $image_link, $image_title, $image), $post - > ID);
} else {
echo apply_filters('woocommerce_single_product_image_html', sprintf('<img src="%s" alt="Placeholder" />', woocommerce_placeholder_img_src()), $post - > ID);
} ?>
</a>
<div>
<h3>
<?php the_title();?>
<span>
<?php
if ($price_html = $product - > get_price_html()) {
?>
<span class = "price">
<?php echo $price_html; ?>
</span>
<?php } ?>
</span>
</h3>
</div>
<div>
<p>
<?php
$excerpt = get_the_excerpt();
echo string_limit_words($excerpt, 15);
?>
</p>
</div>
</li>
<?php endwhile; ?>
</ul>
<?php if ( have_posts() ) : ?>
<?php woocommerce_product_loop_start(); ?>
<?php woocommerce_product_subcategories(); ?>
<div class="courses-main">
<ul class="products">
<?php while ( have_posts() ) : the_post(); ?>
<li>
<a href="<?php echo get_permalink(); ?>">
<?php
if ( has_post_thumbnail() ) {
$image = get_the_post_thumbnail( $post->ID, apply_filters( 'single_product_large_thumbnail_size', 'shop_single' ) );
$image_title = esc_attr( get_the_title( get_post_thumbnail_id() ) );
$image_link = get_permalink( $product_id );
$attachment_count = count( $product->get_gallery_attachment_ids() );
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '%s', $image_link, $image_title, $image ), $post->ID );
} else {
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '<img src="%s" alt="Placeholder" />', woocommerce_placeholder_img_src() ), $post->ID );
}
?>
</a>
<div>
<h3>
<?php the_title();?>
<span>
<?php if ( $price_html = $product->get_price_html()) { ?>
<span class="price"><?php echo $price_html; ?></span>
<?php } ?>
</span>
</h3>
</div>
<div>
<p>
<?php
$excerpt = get_the_excerpt();
echo string_limit_words($excerpt,15);
?>
</p>
</div>
</li>
<?php endwhile;?>
</ul>
</div>
<?php woocommerce_product_loop_end(); ?>
<?php endif; ?>

Categories