Automattic\WooCommerce\Internal\Shipping\FlatRate

WeightPlaceholder::clamp │ private │ WC 1.0

Clamp a weight to the optional min and max attributes of a placeholder, and format it for the expression evaluator.

Метод класса: WeightPlaceholder{}

Хуков нет.

Возвращает

string.

Использование

// private - только в коде основоного (родительского) класса
$result = $this->clamp( $weight, $atts ): string;
$weight(float) (обязательный)
Weight, in the store's weight unit.
$atts(массив|строка) (обязательный)
Placeholder attributes, as returned by shortcode_parse_atts().

Код WeightPlaceholder::clamp() WC 11.3.0

private function clamp( float $weight, $atts ): string {
	// shortcode_parse_atts() returns the raw string when there are no attributes to parse.
	$atts = shortcode_atts(
		array(
			'min' => '',
			'max' => '',
		),
		is_array( $atts ) ? $atts : array()
	);

	// is_numeric() rather than a truthiness test, so that min="0" and max="0" are honoured.
	if ( is_numeric( $atts['min'] ) && $weight < (float) $atts['min'] ) {
		$weight = (float) $atts['min'];
	}

	if ( is_numeric( $atts['max'] ) && $weight > (float) $atts['max'] ) {
		$weight = (float) $atts['max'];
	}

	// WC_Eval_Math reads `e` as a constant and rejects scientific notation, so the weight has to be a plain
	// dot-decimal string. A zero weight yields "0" rather than "", so `10 * [weight]` isn't trimmed to `10`.
	return wc_format_decimal( $weight, false, true );
}