Add hooks to customize the behavior of the cart shipping calculator (#41146)

This commit is contained in:
nigeljamesstevenson 2023-11-08 23:14:44 +00:00 committed by GitHub
commit 3befeb4b2d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 2 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: add
Adds new filters to customize the behavior of the cart shipping calculator.

View File

@ -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;
}
}