WooCommerce获取属性缩略图 - 变体色板和照片插件



我正在使用插件WooCommerce变体色板和照片,它让我可以为产品的属性添加缩略图。

我需要列出模板上的所有属性,我还想获取并显示缩略图。

$terms = get_terms( array(
'taxonomy' => 'pa_texture',
'hide_empty' => false,
) );
foreach ( $terms as $term ) {
print_r($term);
}

缩略图功能在WooCommerce中不是默认的,所以当我print_r $term时没有缩略图URL:

WP_Term Object
(
[term_id] => 54
[name] => Guilin
[slug] => guilin
[term_group] => 0
[term_taxonomy_id] => 54
[taxonomy] => pa_texture
[description] => Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla imperdiet facilisis convallis.
[parent] => 0
[count] => 2
[filter] => raw
[meta_value] => 0
)

如何获取属性的缩略图?

由于@Und3rTow的输入,找到了解决方案。

get_woocommerce_term_meta中的正确参数是pa_texture_swatches_id_photo

这是最终代码:

$thumbnail_id = get_woocommerce_term_meta( $term->term_id, 'pa_texture_swatches_id_photo', true );
$textureImg = wp_get_attachment_image_src( $thumbnail_id ); ?>
<img src="<?php echo $textureImg[0]; ?>">

这是未经测试的,但是以下一些变体应该可以工作:

foreach ( $terms as $key => $term ) {
$thumbnail_id = get_woocommerce_term_meta( $term->term_id, $term->taxonomy . '_photo', true );
$terms[ $key ]->thumb = wp_get_attachment_image_src( $thumbnail_id );
print_r( $term );
}

如果您查看相关的插件文件,您可以看到作者如何获取图像。上面的代码大致基于此。

产品类别术语'product_cat'分类法的经典方法是:

$terms = get_terms( array(
'taxonomy' => 'product_cat',
'hide_empty' => false,
) );
foreach ( $terms as $term ) {
$thumb_id = get_woocommerce_term_meta( $term->term_id, 'thumbnail_id', true );
$img_src = wp_get_attachment_url(  $thumb_id );
echo '<p><img src="'.$img_src.'" alt="" />'.$term->name.'</p>';
}

因此,可能会将分类更改为产品属性,例如'pa_texture',它应该可以解决问题
(我希望,但我不确定,因为我不使用变体色板和照片插件(。

我在追加销售产品中显示图像时遇到了类似的问题。一团糟,但是:

if ( $products_upsells->have_posts() ) : while ( $products_upsells->have_posts() ) : $products_upsells->the_post();
$_product = wc_get_product(get_the_ID());
$attributes = $_product->get_attributes();
$attr_id = $attributes['pa_kolor']['options'][0];
$thumb_id = get_term_meta($attr_id);
$img_src = wp_get_attachment_url($thumb_id['pa_kolor_swatches_id_photo'][0] );
echo '<img src="'.$img_src.'" alt="" />';
endwhile; endif;
wp_reset_query();

请参阅此代码:

$_product = wc_get_product(get_the_ID());
$attributes = $_product->get_attributes();
$attr_id = $attributes['pa_kolor']['options'][0];
$thumb_id = get_term_meta($attr_id);
$img_src = wp_get_attachment_url($thumb_id['pa_kolor_swatches_id_photo'][0] );

终于修复了!我没有在分类学中传递正确的值。我还使用变体色板插件。以下是工作代码。我试图获取带有图像的"品牌"属性列表。

我使用了一个短代码并调用了具有以下代码的函数。如果您想知道如何创建下面的简码检查链接 -

https://www.wpbeginner.com/wp-tutorials/how-to-add-a-shortcode-in-wordpress/

$attribute_taxonomies = wc_get_attribute_taxonomies();
$taxonomy_terms = array();
if ($attribute_taxonomies) :
foreach ($attribute_taxonomies as $tax) :
if (taxonomy_exists(wc_attribute_taxonomy_name($tax->attribute_name))) :

if($tax->attribute_name=="brands"){
$taxonomy_terms[$tax->attribute_name] = get_terms(wc_attribute_taxonomy_name($tax->attribute_name), 'number=6&orderby=name&hide_empty=1');
}
endif;
endforeach;
endif;
foreach ($taxonomy_terms as $item) :
foreach($item as $child):
$thumbnail_id = get_woocommerce_term_meta( $child->term_id, 'product_pa_brands', true );
$textureImg = wp_get_attachment_image_src( $thumbnail_id );
//we are getting image in $textureImg[0]
}
endforeach;
endforeach;

最新更新