在Woocommerce商店页面上获取产品变化图片 [英] Get product variation images on Woocommerce shop page

查看:341
本文介绍了在Woocommerce商店页面上获取产品变化图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我本质上想要实现的是在商店页面上显示产品变化图像(每种变化的特定图像).我可以使用下面的代码(放入"content-product.php")成功获得变体的名称:

What I essentially want to achieve is to show product variation images (particular image for each variation) on the shop page. I was successfully able to get the name of the variations using the code below (put into "content-product.php"):

<?php
$colourvalues = get_the_terms( $product->id, 'pa_colour');
  foreach ( $colourvalues as $colourvalue ) {
   echo $colourvalue->name;
  }
?>

不幸的是,$colouvalues数组中没有任何内容是变体图片网址或与图片相关的任何内容.

Unfortunately there is nothing in the $colouvalues array that is the variations image url or anything related to the image.

有人可以帮我吗?

推荐答案

您可以获得产品版本的列表:

You can get a list of the product's variations:

// In the product loop:
$variations = $product->get_available_variations();

// Outside the product loop:
$product = new WC_Product_Variable( $product_id );
$variations = $product->get_available_variations();

环绕它以从每个变化中获取图像,如下所示:

Loop over it to get the image from each variation like so:

foreach ( $variations as $variation ) {
    echo $variation['image_src'];
}

这篇关于在Woocommerce商店页面上获取产品变化图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆