Closed. This question is off-topic. It is not currently accepting answers.admin管理员组文章数量:1327936
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 questionRight 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 questionRight 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
1 Answer
Reset to default 0This 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
}
}
}
}
本文标签: Remove duplicates from echo output in PHP
版权声明:本文标题:Remove duplicates from echo output in PHP 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742254592a2441396.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论