WC_Shipping_Free_Shipping::is_available()publicWC 1.0

See if free shipping is available based on the package and cart.

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

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

Возвращает

true|false.

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

$WC_Shipping_Free_Shipping = new WC_Shipping_Free_Shipping();
$WC_Shipping_Free_Shipping->is_available( $package );
$package(массив) (обязательный)
Shipping package.

Код WC_Shipping_Free_Shipping::is_available() WC 8.7.0

public function is_available( $package ) {
	$has_coupon         = false;
	$has_met_min_amount = false;

	if ( in_array( $this->requires, array( 'coupon', 'either', 'both' ), true ) ) {
		$coupons = WC()->cart->get_coupons();

		if ( $coupons ) {
			foreach ( $coupons as $code => $coupon ) {
				if ( $coupon->is_valid() && $coupon->get_free_shipping() ) {
					$has_coupon = true;
					break;
				}
			}
		}
	}

	if ( in_array( $this->requires, array( 'min_amount', 'either', 'both' ), true ) ) {
		$total = WC()->cart->get_displayed_subtotal();

		if ( 'no' === $this->ignore_discounts ) {
			$total = $total - WC()->cart->get_discount_total();
			if ( WC()->cart->display_prices_including_tax() ) {
				$total = $total - WC()->cart->get_discount_tax();
			}
		}

		$total = NumberUtil::round( $total, wc_get_price_decimals() );

		if ( $total >= $this->min_amount ) {
			$has_met_min_amount = true;
		}
	}

	switch ( $this->requires ) {
		case 'min_amount':
			$is_available = $has_met_min_amount;
			break;
		case 'coupon':
			$is_available = $has_coupon;
			break;
		case 'both':
			$is_available = $has_met_min_amount && $has_coupon;
			break;
		case 'either':
			$is_available = $has_met_min_amount || $has_coupon;
			break;
		default:
			$is_available = true;
			break;
	}

	return apply_filters( 'woocommerce_shipping_' . $this->id . '_is_available', $is_available, $package, $this );
}