'', 'source' => '', ); $mode = Meta::mode( $variation ); if ( Meta::MODE_OFF === $mode ) { return $none; } if ( Meta::MODE_CUSTOM === $mode ) { $custom = sanitize_hex_color( (string) $variation->get_meta( Meta::CUSTOM_COLOR ) ); if ( $custom ) { return array( 'color' => $custom, 'source' => self::SOURCE_CUSTOM, ); } } $term_color = self::term_color( $variation ); if ( '' !== $term_color ) { return array( 'color' => $term_color, 'source' => self::SOURCE_TERM, ); } $auto = sanitize_hex_color( (string) $variation->get_meta( Meta::AUTO_COLOR ) ); if ( $auto ) { return array( 'color' => $auto, 'source' => self::SOURCE_AUTO, ); } return $none; } /** * The swatch color of the first global attribute term on the variation * that has one. * * @param WC_Product_Variation $variation Variation. * @return string Hex color, or an empty string. */ public static function term_color( $variation ) { // Only a variation has a picked term; a simple product's attributes are all its options. if ( ! $variation instanceof WC_Product_Variation ) { return ''; } foreach ( $variation->get_attributes() as $taxonomy => $slug ) { if ( '' === $slug || ! taxonomy_is_product_attribute( $taxonomy ) ) { continue; } $term = get_term_by( 'slug', $slug, $taxonomy ); if ( ! $term instanceof \WP_Term ) { continue; } $color = sanitize_hex_color( (string) get_term_meta( $term->term_id, self::TERM_COLOR_META, true ) ); if ( $color ) { return $color; } } return ''; } }