get_param( 'dp' ) ) { $decimals = absint( $request->get_param( 'dp' ) ); } $product['price'] = wc_format_decimal( $computed['minimum_price'], $decimals ); } return self::inject_meta_entry( $product, self::META_KEY, $ranges ); } /** * Render the canonical range onto the sync wire: drop every empty sub-range, and * report null when no visible variation carries any price at all. * * @param array $computed The Variable_Price_Range result. */ private static function wire_ranges( array $computed ): ?array { if ( ! $computed['has_prices'] ) { return null; } $ranges = array(); foreach ( $computed['ranges'] as $field => $range ) { if ( '' === $range['min'] && '' === $range['max'] ) { continue; } $ranges[ $field ] = $range; } return empty( $ranges ) ? null : $ranges; } /** * Replace (or add) a single meta_data entry by key, preserving the others (order-stable). * A null value removes the matching entry without adding a replacement. */ private static function inject_meta_entry( array $payload, string $key, $value ): array { $others = array(); $meta = ( isset( $payload['meta_data'] ) && \is_array( $payload['meta_data'] ) ) ? $payload['meta_data'] : array(); foreach ( $meta as $entry ) { $entry_key = Meta_Entry::key( $entry ); if ( $key !== $entry_key ) { $others[] = $entry; } } if ( null !== $value ) { $others[] = array( 'key' => $key, 'value' => $value, ); } $payload['meta_data'] = array_values( $others ); return $payload; } }