WC_Shipping_Legacy_Free_Shipping::is_available( array $package )
Check if package is available.
Description Description
Parameters Parameters
- $package
-
(Required) Package information.
Return Return
(bool)
Source Source
File: includes/shipping/legacy-free-shipping/class-wc-shipping-legacy-free-shipping.php
public function is_available( $package ) {
if ( 'no' === $this->enabled ) {
return false;
}
if ( 'specific' === $this->availability ) {
$ship_to_countries = $this->countries;
} else {
$ship_to_countries = array_keys( WC()->countries->get_shipping_countries() );
}
if ( is_array( $ship_to_countries ) && ! in_array( $package['destination']['country'], $ship_to_countries, true ) ) {
return false;
}
// Enabled logic.
$is_available = false;
$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;
}
}
}
}
if ( in_array( $this->requires, array( 'min_amount', 'either', 'both' ), true ) ) {
$total = WC()->cart->get_displayed_subtotal();
if ( WC()->cart->display_prices_including_tax() ) {
$total = round( $total - ( WC()->cart->get_discount_total() + WC()->cart->get_discount_tax() ), wc_get_price_decimals() );
} else {
$total = round( $total - WC()->cart->get_discount_total(), wc_get_price_decimals() );
}
if ( $total >= $this->min_amount ) {
$has_met_min_amount = true;
}
}
switch ( $this->requires ) {
case 'min_amount':
if ( $has_met_min_amount ) {
$is_available = true;
}
break;
case 'coupon':
if ( $has_coupon ) {
$is_available = true;
}
break;
case 'both':
if ( $has_met_min_amount && $has_coupon ) {
$is_available = true;
}
break;
case 'either':
if ( $has_met_min_amount || $has_coupon ) {
$is_available = true;
}
break;
default:
$is_available = true;
break;
}
return apply_filters( 'woocommerce_shipping_' . $this->id . '_is_available', $is_available, $package, $this );
}