dongqing7789 2018-12-14 15:14
浏览 105
已采纳

按属性ID获取WooCommerce属性缩略图URL(WooCommerce Product Search插件)

I've already spent a couple of hours googling and already tried many things. The closer I could get to my solution was using this code:

// 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]; ?>">

I'm just out of ideas! :(

EDIT:

I was just being dumb here! Thanks to @LoicTheAztec comment I just noticed this is something from official WooCommerce Product Search plugin.

  • 写回答

1条回答 默认 最新

  • duancutan4770 2018-12-14 16:01
    关注

    Solution for this problem:

    <?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]; ?>">
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?