【问题标题】:WooCommerce get attribute thumbnail - Variation Swatches and Photos pluginWooCommerce 获取属性缩略图 - 变体色板和照片插件
【发布时间】:2017-12-30 04:28:41
【问题描述】:

我正在使用插件 WooCommerce Variation Swatches and Photos 让我可以为我的产品属性添加缩略图。

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

$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
)

如何获取属性的缩略图?

【问题讨论】:

    标签: wordpress plugins woocommerce attributes thumbnails


    【解决方案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' 这样的产品属性,它应该可以解决问题
    (我希望,但我不确定 因为我不使用 Variation Swatches and Photos 插件

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

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

        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,您可以看到作者如何获取图像。上面的代码大致基于此。

        【讨论】:

        • 谢谢!我认为这是正确的方向,但没有奏效。我正在尝试弥补它,但还没有运气:(
        【解决方案4】:

        在加售产品中显示图像时,我遇到了类似的问题。有点乱,但是:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-09-15
          • 1970-01-01
          • 2015-12-12
          • 1970-01-01
          • 2015-12-03
          • 1970-01-01
          • 2012-05-24
          • 2012-11-16
          相关资源
          最近更新 更多