duanluo9369 2017-11-24 12:13 采纳率: 100%
浏览 58
已采纳

从WP_Query for WooCommerce产品中的foreach循环中排除重复项

I have a WP Query to list matching products, like this:

function sku_ean_sizes () {
global $product;

$current_stijlcode = $product->get_attribute( 'pa_stijlcode' );
$current_ean = $product->get_attribute( 'pa_ean' );

$postid = get_the_ID();

    $args = array(
      'post_type' => 'product',
      'orderby' => 'meta_value_num',
        'meta_key' => '_price',
        'order' => 'asc',
      'posts_per_page' => 100,
      'tax_query' => array(
    'relation' => 'OR',
    array(
      'taxonomy' => 'pa_stijlcode',
      'field'    => 'slug',
      'terms'    => $current_stijlcode,
    ),
    array(
      'taxonomy' => 'pa_ean',
      'field'    => 'slug',
      'terms'    => $current_ean,
    )
  )
      );
    $loop = new WP_Query( $args );
    if ( $loop->have_posts() ) {
      while ( $loop->have_posts() ) : $loop->the_post();



global $product;

foreach( wc_get_product_terms( $product->id, 'pa_maat' ) as $attribute_value ){
  echo '<span>' . $attribute_value . '</span>';
}

      endwhile;
    } else {
      // no sku matches found
    }
    wp_reset_postdata();
}

I would like to use the foreach loop to list the found product attributes attached to the products found with the WP Query:

global $product;

    foreach( wc_get_product_terms( $product->id, 'pa_maat' ) as $attribute_value ){
      echo '<span>' . $attribute_value . '</span>';
    }

This code works. However, the $attribute_value variable outputs duplicate records. This makes sense, as multiple products could have the same output.

What adjustment can i make so it excludes duplicate values?

In context; This is to display all available sizes for a specific product.

  • 写回答

1条回答 默认 最新

  • duanpang2751 2017-11-24 12:34
    关注

    Updated

    You should use a foreach loop to set all the values in an array avoiding duplicates

    Then, using implode() PHP function, you will display all attributes term names without duplicates.

    This partial code to replace yours, will display the non duplicated attribute term names for all products at once:

    $loop = new WP_Query( $args );
    if ( $loop->have_posts() ):
        $maat_term_names = array();
        while ( $loop->have_posts() ): 
            $loop->the_post();
    
            // Set the attribute term names in an array avoiding duplicates
            foreach( wc_get_product_terms( $loop->post->ID, 'pa_maat' ) as $attribute_value ):
                $maat_term_names[$attribute_value] = $attribute_value;
            endforeach;
        endwhile;
    
        // Sorting (ordering)
        sort($maat_term_names);
    
        // Here you display attribute term names without duplicates (coma separated)
        echo '<span>' . implode( '</span>, <span>', $maat_term_names ) . '</span> ';
    
    else:
       echo '<span>No SKUs matches found</span>';
    endif;
    
    wp_reset_postdata();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置