Automattic\WooCommerce\Internal\Shipping\FlatRate

WeightPlaceholder::get_for_items │ public │ WC 11.3.0

Sum the weight of the given items, multiplied by their quantity.

Items that don't need shipping, and items without a weight, contribute nothing. To adjust the weight a cost formula sees, hook woocommerce_evaluate_shipping_cost_args change its weight argument.

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

Хуков нет.

Возвращает

float.

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

$WeightPlaceholder = new WeightPlaceholder();
$WeightPlaceholder->get_for_items( $items ): float;
$items(массив) (обязательный)
Cart items, in the shape of a package's contents.

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

С версии 11.3.0 Введена.

Код WeightPlaceholder::get_for_items() WC 11.3.0

public function get_for_items( array $items ): float {
	$total_weight = 0.0;

	foreach ( $items as $values ) {
		if ( ! is_array( $values ) ) {
			continue;
		}

		$product  = $values['data'] ?? null;
		$quantity = $values['quantity'] ?? 0;

		if ( $product instanceof WC_Product && is_numeric( $quantity ) && $quantity > 0 && $product->needs_shipping() && $product->has_weight() ) {
			$total_weight += $this->normalize( $product->get_weight() ) * $quantity;
		}
	}

	return $total_weight;
}