2017-07-24 18 views
0

我使用的插件WooCommerce變體色板和照片讓我可以爲我的產品的屬性添加縮略圖。WooCommerce獲取屬性縮略圖 - 變體色板和照片插件

我需要列出模板上的所有屬性,並且還希望顯示縮略圖。

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

縮略圖功能在默認WooCommerce所以當我的print_r $長期沒有縮略圖網址:

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 
) 

我怎樣才能獲得屬性的縮略圖?

回答

1

感謝@ 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]; ?>"> 
1

產品類別方面'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="" />'.$tem->name.'</p>'; 
} 

所以可能會改變該分類產品的屬性,例如'pa_texture',它應該做的伎倆
(希望,但我不確定,因爲我不使用變化色板和照片插件

+0

謝謝,但沒有工作:( – Jeff

1

這是未經測試,但是下面的一些變化應工作:

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); 
} 

如果你看一下the relevant plugin file,你可以看到作者是如何獲得的圖像。上面的代碼基於此。

+0

謝謝你,我認爲這是正確的方向,但沒有工作我試圖彌補,但沒有運氣呢:( – Jeff

0

在加售的產品在顯示圖像時我也有類似的問題。這裏是一個爛攤子,但:

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]);