WC_Cart::show_shipping
Sees if the customer has entered enough data to calculate shipping.
Method of the class: WC_Cart{}
Hooks from the method
Returns
true|false.
Usage
$WC_Cart = new WC_Cart(); $WC_Cart->show_shipping();
WC_Cart::show_shipping() WC Cart::show shipping code WC 10.8.1
public function show_shipping() {
// If there are no shipping methods or no cart contents, no need to calculate shipping.
if ( ! wc_shipping_enabled() || 0 === wc_get_shipping_method_count( true ) || ! $this->get_cart_contents() ) {
return false;
}
if ( 'yes' === get_option( 'woocommerce_shipping_cost_requires_address' ) ) {
// If local pickup is enabled, shipping should be shown so that pickup locations are visible before address entry.
if ( LocalPickupUtils::is_local_pickup_enabled() ) {
/**
* This filter is documented below.
*
* @since 2.7.0
*/
return apply_filters( 'woocommerce_cart_ready_to_calc_shipping', true );
}
if ( 'shortcode' === $this->cart_context ) {
$country = $this->get_customer()->get_shipping_country();
if ( ! $country ) {
return false;
}
$country_fields = WC()->countries->get_address_fields( $country, 'shipping_' );
$checkout_fields = WC()->checkout()->get_checkout_fields();
/**
* Filter to not require shipping state for shipping calculation, even if it is required at checkout.
* This can be used to allow shipping calculations to be done without a state.
*
* @since 8.4.0
*
* @param bool $show_state Whether to use the state field. Default true.
*/
$state_enabled = apply_filters( 'woocommerce_shipping_calculator_enable_state', true );
$state_required = isset( $country_fields['shipping_state'] ) && $country_fields['shipping_state']['required'];
// Takes care of late unsetting of checkout fields via hooks (woocommerce_checkout_fields, woocommerce_shipping_fields).
$checkout_state_field_exists = isset( $checkout_fields['shipping']['shipping_state'] );
if ( $state_enabled && $state_required && ! $this->get_customer()->get_shipping_state() && $checkout_state_field_exists ) {
return false;
}
/**
* Filter to not require shipping postcode for shipping calculation, even if it is required at checkout.
* This can be used to allow shipping calculations to be done without a postcode.
*
* @since 8.4.0
*
* @param bool $show_postcode Whether to use the postcode field. Default true.
*/
$postcode_enabled = apply_filters( 'woocommerce_shipping_calculator_enable_postcode', true );
$postcode_required = isset( $country_fields['shipping_postcode'] ) && $country_fields['shipping_postcode']['required'];
// Takes care of late unsetting of checkout fields via hooks (woocommerce_checkout_fields, woocommerce_shipping_fields).
$checkout_postcode_field_exists = isset( $checkout_fields['shipping']['shipping_postcode'] );
if ( $postcode_enabled && $postcode_required && '' === $this->get_customer()->get_shipping_postcode() && $checkout_postcode_field_exists ) {
return false;
}
} else {
$customer = $this->get_customer();
if ( ! $customer instanceof \WC_Customer || ! $customer->has_full_shipping_address() ) {
return false;
}
}
}
/**
* Filter to allow plugins to prevent shipping calculations.
*
* @since 2.7.0
*
* @param bool $ready Whether the cart is ready to calculate shipping.
*/
return apply_filters( 'woocommerce_cart_ready_to_calc_shipping', true );
}