WC_Cart::calculate_shipping │ public │ WC 1.0

Uses the shipping class to calculate shipping then gets the totals when its finished.

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

Хуков нет.

Возвращает

array.

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

$WC_Cart = new WC_Cart();
$WC_Cart->calculate_shipping();

Код WC_Cart::calculate_shipping() WC 11.3.0

public function calculate_shipping() {
	$this->clear_shipping_totals();

	$ready = $this->needs_shipping() && $this->show_shipping();

	// A filter run by show_shipping() can calculate the totals again and leave that nested run's shipping behind.
	$this->clear_shipping_totals();

	if ( ! $ready ) {
		return $this->shipping_methods;
	}

	$this->has_calculated_shipping = true;
	$this->shipping_methods        = $this->get_chosen_shipping_methods( WC()->shipping()->calculate_shipping( $this->get_shipping_packages() ) );

	$shipping_costs = wp_list_pluck( $this->shipping_methods, 'cost' );
	$shipping_taxes = wp_list_pluck( $this->shipping_methods, 'taxes' );
	$merged_taxes   = array();
	foreach ( $shipping_taxes as $taxes ) {
		foreach ( $taxes as $tax_id => $tax_amount ) {
			$merged_taxes[ $tax_id ] = ( $merged_taxes[ $tax_id ] ?? 0 ) + $tax_amount;
		}
	}

	$this->set_shipping_total( array_sum( array_filter( $shipping_costs ) ) );
	$this->set_shipping_tax( array_sum( $merged_taxes ) );
	$this->set_shipping_taxes( $merged_taxes );

	return $this->shipping_methods;
}