通过属性ID获取WooCommerce属性缩略图URL(WooCommerce产品搜索插件)

时间:2018-12-14 15:14:31

标签: php wordpress woocommerce

我已经花了几个小时进行谷歌搜索,并且已经尝试了很多东西。我更接近解决方案的地方是使用以下代码:

// With this I successfully get an Array with attribute ID
$carimg_attr = wc_get_product_terms( get_the_id(), 'pa_carimg', 'thumbnail' );

// This echo actually returns the correct ID
echo $carimg_attr[0]->term_id;

// In this echo, I get it empty but this was supposed to show me metas from the attribute (at least as far as other online answers was saying)
echo get_woocommerce_term_meta( $carimg_attr[0]->term_id, 'pa_carimg', true );

// Printing this one, I get actually all fields EXCEPT the one I need: thumbnail
print_r(wc_get_product_terms( get_the_id(), 'pa_carimg', array( 'fields' => 'all' ) ));

// This was just a last desperate try and doesn't work either
$carimg_Img = wp_get_attachment_image_src( $carimg_attr[0]->term_id ); ?>
<img src="<?php echo $carimg_Img[0]; ?>">

我没主意! :(

编辑:

我只是在这里呆呆!感谢@LoicTheAztec的评论,我刚刚注意到这是来自WooCommerce产品搜索官方插件的东西。

1 个答案:

答案 0 :(得分:0)

此问题的解决方案:

<?php
$carimg_attr = wc_get_product_terms( get_the_id(), 'pa_carimg', 'thumbnail' );
$imgAtt_id = get_woocommerce_term_meta( $carimg_attr[0]->term_id, 'product_search_image_id', true );
$imgAtt_url = wp_get_attachment_image_src( $imgAtt_id, 'medium', false );
?>
<img src="<?php echo $imgAtt_url[0]; ?>">