Created
December 12, 2023 00:38
-
-
Save J2112O/ad26cb25a96146cce227a2fafd7bd601 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action( 'woocommerce_display_product_attributes', 'woo_custom_variation_display', 12 ); | |
function woo_custom_variation_display( $product_attributes ) { | |
global $product; | |
// make sure this is a variable product. If not, return | |
if ( ! $product->is_type( 'variable' ) ) return; | |
if ( isset( $product_attributes[ 'attribute_pa_color' ] ) ) { | |
$variations = $product->get_available_variations( 'object' ); | |
//empty array to add the pa_color names to | |
$variations_to_display_ar = array(); | |
// make sure there are some variations | |
if( ! empty( $variations ) ) { | |
foreach ( $variations as $key => $variation ) { | |
foreach ( $variation->attributes as $at_key => $value ) { | |
// only interested in the pa_color values | |
if ( 'pa_color' == $at_key ) { | |
// adding an uppercase version (because they are stored lowercase) of each color value to our array | |
array_push( $variations_to_display_ar, ucwords( $value ) ); | |
} | |
} | |
} | |
/* need to keep only unique values as the product can have multiple variations of same color. ie blue hoodie with logo, blue hoodie without logo etc */ | |
$variations_display_string = implode( ', ', array_unique( $variations_to_display_ar ) ); | |
//set the attribute_pa_color value to the string of enabled variation colors | |
$product_attributes[ 'attribute_pa_color' ][ 'value' ] = $variations_display_string; | |
return $product_attributes; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment