*/ public static function for_variation( int $variation_id ): array { $variation = wc_get_product( $variation_id ); if ( ! $variation instanceof \WC_Product_Variation ) { return array(); } $parent = wc_get_product( $variation->get_parent_id() ); $attributes = array(); foreach ( $variation->get_attributes() as $taxonomy => $value ) { $value = (string) $value; if ( '' === $value ) { continue; } if ( taxonomy_exists( (string) $taxonomy ) ) { $term = get_term_by( 'slug', $value, (string) $taxonomy ); $value = $term instanceof \WP_Term ? $term->name : $value; } $label = $parent instanceof \WC_Product ? wc_attribute_label( (string) $taxonomy, $parent ) : wc_attribute_label( (string) $taxonomy ); $attributes[ $label ] = $value; } /** * Filters the variant attributes named in the try-on prompt (size is always left out). * * @since 1.0.0 * * @param array $attributes Attribute labels and values. * @param int $variation_id Variation ID. */ $filtered = apply_filters( 'fitzaro/variant_attributes', $attributes, $variation_id ); return self::only_strings( $filtered ); } /** * Keeps string keys and scalar values. A filter can return anything. * * @param mixed $attributes Filter result. * @return array */ private static function only_strings( $attributes ): array { $clean = array(); foreach ( is_array( $attributes ) ? $attributes : array() as $name => $value ) { if ( is_string( $name ) && is_scalar( $value ) ) { $clean[ $name ] = (string) $value; } } return $clean; } }