WC_Shipping_Flat_Rate::calculate_shipping()publicWC 1.0

Calculate the shipping costs.

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

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

Возвращает

null. Ничего (null).

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

$WC_Shipping_Flat_Rate = new WC_Shipping_Flat_Rate();
$WC_Shipping_Flat_Rate->calculate_shipping( $package );
$package(массив)
Package of items from cart.
По умолчанию: array()

Код WC_Shipping_Flat_Rate::calculate_shipping() WC 8.7.0

public function calculate_shipping( $package = array() ) {
	$rate = array(
		'id'      => $this->get_rate_id(),
		'label'   => $this->title,
		'cost'    => 0,
		'package' => $package,
	);

	// Calculate the costs.
	$has_costs = false; // True when a cost is set. False if all costs are blank strings.
	$cost      = $this->get_option( 'cost' );

	if ( '' !== $cost ) {
		$has_costs    = true;
		$rate['cost'] = $this->evaluate_cost(
			$cost,
			array(
				'qty'  => $this->get_package_item_qty( $package ),
				'cost' => $package['contents_cost'],
			)
		);
	}

	// Add shipping class costs.
	$shipping_classes = WC()->shipping()->get_shipping_classes();

	if ( ! empty( $shipping_classes ) ) {
		$found_shipping_classes = $this->find_shipping_classes( $package );
		$highest_class_cost     = 0;

		foreach ( $found_shipping_classes as $shipping_class => $products ) {
			// Also handles BW compatibility when slugs were used instead of ids.
			$shipping_class_term = get_term_by( 'slug', $shipping_class, 'product_shipping_class' );
			$class_cost_string   = $shipping_class_term && $shipping_class_term->term_id ? $this->get_option( 'class_cost_' . $shipping_class_term->term_id, $this->get_option( 'class_cost_' . $shipping_class, '' ) ) : $this->get_option( 'no_class_cost', '' );

			if ( '' === $class_cost_string ) {
				continue;
			}

			$has_costs  = true;
			$class_cost = $this->evaluate_cost(
				$class_cost_string,
				array(
					'qty'  => array_sum( wp_list_pluck( $products, 'quantity' ) ),
					'cost' => array_sum( wp_list_pluck( $products, 'line_total' ) ),
				)
			);

			if ( 'class' === $this->type ) {
				$rate['cost'] += $class_cost;
			} else {
				$highest_class_cost = $class_cost > $highest_class_cost ? $class_cost : $highest_class_cost;
			}
		}

		if ( 'order' === $this->type && $highest_class_cost ) {
			$rate['cost'] += $highest_class_cost;
		}
	}

	if ( $has_costs ) {
		$this->add_rate( $rate );
	}

	/**
	 * Developers can add additional flat rates based on this one via this action since @version 2.4.
	 *
	 * Previously there were (overly complex) options to add additional rates however this was not user.
	 * friendly and goes against what Flat Rate Shipping was originally intended for.
	 */
	do_action( 'woocommerce_' . $this->id . '_shipping_add_rate', $this, $rate );
}