diff --git a/plugins/woocommerce/changelog/add-shipping-calculator-filters b/plugins/woocommerce/changelog/add-shipping-calculator-filters new file mode 100644 index 00000000000..ad45d75a8be --- /dev/null +++ b/plugins/woocommerce/changelog/add-shipping-calculator-filters @@ -0,0 +1,4 @@ +Significance: minor +Type: add + +Adds new filters to customize the behavior of the cart shipping calculator. diff --git a/plugins/woocommerce/includes/class-wc-cart.php b/plugins/woocommerce/includes/class-wc-cart.php index 84410f9c505..bb940addd7a 100644 --- a/plugins/woocommerce/includes/class-wc-cart.php +++ b/plugins/woocommerce/includes/class-wc-cart.php @@ -1573,10 +1573,30 @@ class WC_Cart extends WC_Legacy_Cart { return false; } $country_fields = WC()->countries->get_address_fields( $country, 'shipping_' ); - if ( isset( $country_fields['shipping_state'] ) && $country_fields['shipping_state']['required'] && ! $this->get_customer()->get_shipping_state() ) { + /** + * 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']; + if ( $state_enabled && $state_required && ! $this->get_customer()->get_shipping_state() ) { return false; } - if ( isset( $country_fields['shipping_postcode'] ) && $country_fields['shipping_postcode']['required'] && ! $this->get_customer()->get_shipping_postcode() ) { + /** + * 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']; + if ( $postcode_enabled && $postcode_required && ! $this->get_customer()->get_shipping_postcode() ) { return false; } }