WC_Shipping_Flat_Rate::evaluate_cost()protectedWC 1.0

Evaluate a cost from a sum/string.

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

Хуки из метода

Возвращает

Строку.

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

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->evaluate_cost( $sum, $args );
$sum(строка) (обязательный)
Sum of shipping.
$args(массив)
Args, must contain cost and qty keys. Having array() as default is for back compat reasons.
По умолчанию: array()

Код WC_Shipping_Flat_Rate::evaluate_cost() WC 9.4.2

protected function evaluate_cost( $sum, $args = array() ) {
	// Add warning for subclasses.
	if ( ! is_array( $args ) || ! array_key_exists( 'qty', $args ) || ! array_key_exists( 'cost', $args ) ) {
		wc_doing_it_wrong( __FUNCTION__, '$args must contain `cost` and `qty` keys.', '4.0.1' );
	}

	include_once WC()->plugin_path() . '/includes/libraries/class-wc-eval-math.php';

	// Allow 3rd parties to process shipping cost arguments.
	$args           = apply_filters( 'woocommerce_evaluate_shipping_cost_args', $args, $sum, $this );
	$locale         = localeconv();
	$decimals       = array( wc_get_price_decimal_separator(), $locale['decimal_point'], $locale['mon_decimal_point'], ',' );
	$this->fee_cost = $args['cost'];

	// Expand shortcodes.
	add_shortcode( 'fee', array( $this, 'fee' ) );

	$sum = do_shortcode(
		str_replace(
			array(
				'[qty]',
				'[cost]',
			),
			array(
				$args['qty'],
				$args['cost'],
			),
			$sum
		)
	);

	remove_shortcode( 'fee', array( $this, 'fee' ) );

	// Remove whitespace from string.
	$sum = preg_replace( '/\s+/', '', $sum );

	// Remove locale from string.
	$sum = str_replace( $decimals, '.', $sum );

	// Trim invalid start/end characters.
	$sum = rtrim( ltrim( $sum, "\t\n\r\0\x0B+*/" ), "\t\n\r\0\x0B+-*/" );

	// Do the math.
	return $sum ? WC_Eval_Math::evaluate( $sum ) : 0;
}