最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Remove duplicates from echo output in PHP

programmeradmin5浏览0评论
Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 4 years ago.

Improve this question

Right now the below function outputs all variations images of a product. E.g. 3x blue (size S, M, L), 3x red (size S, M, L)

I would like the below function to only output unique color images. E.g. 1x blue, 1x red

Tried to use array_unique in the echo string but could not get it working.

Thank you for helping.

function loop_display_variation_attribute_and_thumbnail() {
    global $product;
    if( $product->is_type('variable') ){
        foreach ( $product->get_visible_children() as $variation_id ){
            // Get an instance of the Product_Variation object
            $variation = wc_get_product( $variation_id );

            // Get "color" product attribute term name value
            $color = $variation->get_attribute('pa_color');

            if( ! empty($color) ){
                // Display "color" product attribute term name value
                echo $color;

                // Display the product thumbnail with a defined size (here 30 x 30 pixels)
                echo $variation->get_image( array(30, 30) );
            }
        }
    }
}
Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 4 years ago.

Improve this question

Right now the below function outputs all variations images of a product. E.g. 3x blue (size S, M, L), 3x red (size S, M, L)

I would like the below function to only output unique color images. E.g. 1x blue, 1x red

Tried to use array_unique in the echo string but could not get it working.

Thank you for helping.

function loop_display_variation_attribute_and_thumbnail() {
    global $product;
    if( $product->is_type('variable') ){
        foreach ( $product->get_visible_children() as $variation_id ){
            // Get an instance of the Product_Variation object
            $variation = wc_get_product( $variation_id );

            // Get "color" product attribute term name value
            $color = $variation->get_attribute('pa_color');

            if( ! empty($color) ){
                // Display "color" product attribute term name value
                echo $color;

                // Display the product thumbnail with a defined size (here 30 x 30 pixels)
                echo $variation->get_image( array(30, 30) );
            }
        }
    }
}
Share Improve this question asked Jul 17, 2020 at 12:14 epostemaepostema 31 bronze badge 1
  • so in this code two different variants might have the same color, is that the problem? – mozboz Commented Jul 17, 2020 at 12:22
Add a comment  | 

1 Answer 1

Reset to default 0

This is more of a PHP programming question than specific to Wordpress, but you could do something like this.

This keeps a track of which colors have been shown in $shownColors array and doesn't show the same one twice.

function loop_display_variation_attribute_and_thumbnail() {
    global $product;
    if( $product->is_type('variable') ){

        $shownColors = Array();

        foreach ( $product->get_visible_children() as $variation_id ){
            // Get an instance of the Product_Variation object
            $variation = wc_get_product( $variation_id );

            // Get "color" product attribute term name value
            $color = $variation->get_attribute('pa_color');

            if( ! empty($color) && !in_array($color, $shownColors) ){
            // Display "color" product attribute term name value
                echo $color;

                // Display the product thumbnail with a defined size (here 30 x 30 pixels)
                echo $variation->get_image( array(30, 30) );

                $shownColors[] = $color;  // add this to colors we've shown
            }
        }
    }
}
发布评论

评论列表(0)

  1. 暂无评论