WC_Shipping_Legacy_Flat_Rate::get_extra_cost()publicWC 1.0

Устарела с версии 2.4.0. Больше не поддерживается и может быть удалена. Рекомендуется заменить эту функцию на аналог.

Get extra cost.

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

Хуков нет.

Возвращает

float.

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

$WC_Shipping_Legacy_Flat_Rate = new WC_Shipping_Legacy_Flat_Rate();
$WC_Shipping_Legacy_Flat_Rate->get_extra_cost( $cost_string, $type, $package );
$cost_string(строка) (обязательный)
Cost string.
$type(строка) (обязательный)
Type.
$package(массив) (обязательный)
Package information.

Список изменений

Устарела с 2.4.0

Код WC_Shipping_Legacy_Flat_Rate::get_extra_cost() WC 9.3.3

public function get_extra_cost( $cost_string, $type, $package ) {
	$cost         = $cost_string;
	$cost_percent = false;
	// @codingStandardsIgnoreStart
	$pattern      =
		'/' .           // Start regex.
		'(\d+\.?\d*)' . // Capture digits, optionally capture a `.` and more digits.
		'\s*' .         // Match whitespace.
		'(\+|-)' .      // Capture the operand.
		'\s*' .         // Match whitespace.
		'(\d+\.?\d*)' . // Capture digits, optionally capture a `.` and more digits.
		'\%/';          // Match the percent sign & end regex.
	// @codingStandardsIgnoreEnd
	if ( preg_match( $pattern, $cost_string, $this_cost_matches ) ) {
		$cost_operator = $this_cost_matches[2];
		$cost_percent  = $this_cost_matches[3] / 100;
		$cost          = $this_cost_matches[1];
	}
	switch ( $type ) {
		case 'class':
			$cost = $cost * count( $this->find_shipping_classes( $package ) );
			break;
		case 'item':
			$cost = $cost * $this->get_package_item_qty( $package );
			break;
	}
	if ( $cost_percent ) {
		switch ( $type ) {
			case 'class':
				$shipping_classes = $this->find_shipping_classes( $package );
				foreach ( $shipping_classes as $shipping_class => $items ) {
					foreach ( $items as $item_id => $values ) {
						$cost = $this->calc_percentage_adjustment( $cost, $cost_percent, $cost_operator, $values['line_total'] );
					}
				}
				break;
			case 'item':
				foreach ( $package['contents'] as $item_id => $values ) {
					if ( $values['data']->needs_shipping() ) {
						$cost = $this->calc_percentage_adjustment( $cost, $cost_percent, $cost_operator, $values['line_total'] );
					}
				}
				break;
			case 'order':
				$cost = $this->calc_percentage_adjustment( $cost, $cost_percent, $cost_operator, $package['contents_cost'] );
				break;
		}
	}
	return $cost;
}