diff --git a/plugins/woocommerce-blocks/assets/js/base/components/cart-checkout/totals/shipping/shipping-address.tsx b/plugins/woocommerce-blocks/assets/js/base/components/cart-checkout/totals/shipping/shipping-address.tsx index 58f93a480c4..dd9eb92adf7 100644 --- a/plugins/woocommerce-blocks/assets/js/base/components/cart-checkout/totals/shipping/shipping-address.tsx +++ b/plugins/woocommerce-blocks/assets/js/base/components/cart-checkout/totals/shipping/shipping-address.tsx @@ -4,7 +4,10 @@ import { __ } from '@wordpress/i18n'; import { formatShippingAddress } from '@woocommerce/base-utils'; import { useEditorContext } from '@woocommerce/base-context'; -import { ShippingAddress as ShippingAddressType } from '@woocommerce/settings'; +import { + ShippingAddress as ShippingAddressType, + getSetting, +} from '@woocommerce/settings'; import PickupLocation from '@woocommerce/base-components/cart-checkout/pickup-location'; import { CHECKOUT_STORE_KEY } from '@woocommerce/block-data'; import { useSelect } from '@wordpress/data'; @@ -22,6 +25,10 @@ export interface ShippingAddressProps { shippingAddress: ShippingAddressType; } +export type ActiveShippingZones = { + description: string; +}[]; + export const ShippingAddress = ( { showCalculator, isShippingCalculatorOpen, @@ -32,15 +39,30 @@ export const ShippingAddress = ( { const prefersCollection = useSelect( ( select ) => select( CHECKOUT_STORE_KEY ).prefersCollection() ); + const activeShippingZones: ActiveShippingZones = getSetting( + 'activeShippingZones' + ); + + const hasMultipleAndDefaultZone = + activeShippingZones.length > 1 && + activeShippingZones.some( + ( zone: { description: string } ) => + zone.description === 'Everywhere' || + zone.description === 'Locations outside all other zones' + ); + const hasFormattedAddress = !! formatShippingAddress( shippingAddress ); - // If there is no default customer location set in the store, the customer hasn't provided their address, - // but a default shipping method is available for all locations, + // If there is no default customer location set in the store, + // and the customer hasn't provided their address, + // and only one default shipping method is available for all locations, // then the shipping calculator will be hidden to avoid confusion. - if ( ! hasFormattedAddress && ! isEditor ) { + if ( ! hasFormattedAddress && ! isEditor && ! hasMultipleAndDefaultZone ) { return null; } - + const label = hasFormattedAddress + ? __( 'Change address', 'woocommerce' ) + : __( 'Calculate shipping for your location', 'woocommerce' ); const formattedLocation = formatShippingAddress( shippingAddress ); return ( <> @@ -51,7 +73,7 @@ export const ShippingAddress = ( { ) } { showCalculator && ( diff --git a/plugins/woocommerce-blocks/assets/js/blocks/cart/inner-blocks/cart-order-summary-shipping/block.tsx b/plugins/woocommerce-blocks/assets/js/blocks/cart/inner-blocks/cart-order-summary-shipping/block.tsx index 87430ace202..02d5951711c 100644 --- a/plugins/woocommerce-blocks/assets/js/blocks/cart/inner-blocks/cart-order-summary-shipping/block.tsx +++ b/plugins/woocommerce-blocks/assets/js/blocks/cart/inner-blocks/cart-order-summary-shipping/block.tsx @@ -3,17 +3,28 @@ */ import { TotalsShipping } from '@woocommerce/base-components/cart-checkout'; import { getCurrencyFromPriceResponse } from '@woocommerce/price-format'; -import { useStoreCart } from '@woocommerce/base-context/hooks'; +import { useStoreCart, useEditorContext } from '@woocommerce/base-context/'; import { TotalsWrapper } from '@woocommerce/blocks-components'; import { getSetting } from '@woocommerce/settings'; +import { getShippingRatesPackageCount } from '@woocommerce/base-utils'; +import { select } from '@wordpress/data'; const Block = ( { className }: { className: string } ): JSX.Element | null => { const { cartTotals, cartNeedsShipping } = useStoreCart(); + const { isEditor } = useEditorContext(); if ( ! cartNeedsShipping ) { return null; } + const shippingRates = select( 'wc/store/cart' ).getShippingRates(); + const shippingRatesPackageCount = + getShippingRatesPackageCount( shippingRates ); + + if ( ! shippingRatesPackageCount && isEditor ) { + return null; + } + const totalsCurrency = getCurrencyFromPriceResponse( cartTotals ); return ( diff --git a/plugins/woocommerce/changelog/43803-fix-42280-change-address-link-in-cart-block b/plugins/woocommerce/changelog/43803-fix-42280-change-address-link-in-cart-block new file mode 100644 index 00000000000..e96a0b33dbe --- /dev/null +++ b/plugins/woocommerce/changelog/43803-fix-42280-change-address-link-in-cart-block @@ -0,0 +1,4 @@ +Significance: minor +Type: fix + +Hide the shipping calculator link on the Cart editor with no default address and zone. Display the shipping calculator link on the front end for multiple zones with a fallback. \ No newline at end of file diff --git a/plugins/woocommerce/src/Blocks/BlockTypes/Cart.php b/plugins/woocommerce/src/Blocks/BlockTypes/Cart.php index cae153ab83e..8af17041cfb 100644 --- a/plugins/woocommerce/src/Blocks/BlockTypes/Cart.php +++ b/plugins/woocommerce/src/Blocks/BlockTypes/Cart.php @@ -244,6 +244,7 @@ class Cart extends AbstractBlock { $this->asset_data_registry->add( 'hasDarkEditorStyleSupport', current_theme_supports( 'dark-editor-style' ), true ); $this->asset_data_registry->register_page_id( isset( $attributes['checkoutPageId'] ) ? $attributes['checkoutPageId'] : 0 ); $this->asset_data_registry->add( 'isBlockTheme', wc_current_theme_is_fse_theme(), true ); + $this->asset_data_registry->add( 'activeShippingZones', CartCheckoutUtils::get_shipping_zones(), true ); $pickup_location_settings = get_option( 'woocommerce_pickup_location_settings', [] ); $this->asset_data_registry->add( 'localPickupEnabled', wc_string_to_bool( $pickup_location_settings['enabled'] ?? 'no' ), true ); diff --git a/plugins/woocommerce/src/Blocks/BlockTypes/Checkout.php b/plugins/woocommerce/src/Blocks/BlockTypes/Checkout.php index 2f394b6c5f2..86bac1c8643 100644 --- a/plugins/woocommerce/src/Blocks/BlockTypes/Checkout.php +++ b/plugins/woocommerce/src/Blocks/BlockTypes/Checkout.php @@ -315,25 +315,7 @@ class Checkout extends AbstractBlock { } if ( $is_block_editor && ! $this->asset_data_registry->exists( 'activeShippingZones' ) && class_exists( '\WC_Shipping_Zones' ) ) { - $shipping_zones = \WC_Shipping_Zones::get_zones(); - $formatted_shipping_zones = array_reduce( - $shipping_zones, - function( $acc, $zone ) { - $acc[] = [ - 'id' => $zone['id'], - 'title' => $zone['zone_name'], - 'description' => $zone['formatted_zone_location'], - ]; - return $acc; - }, - [] - ); - $formatted_shipping_zones[] = [ - 'id' => 0, - 'title' => __( 'International', 'woocommerce' ), - 'description' => __( 'Locations outside all other zones', 'woocommerce' ), - ]; - $this->asset_data_registry->add( 'activeShippingZones', $formatted_shipping_zones ); + $this->asset_data_registry->add( 'activeShippingZones', CartCheckoutUtils::get_shipping_zones() ); } if ( $is_block_editor && ! $this->asset_data_registry->exists( 'globalPaymentMethods' ) ) { diff --git a/plugins/woocommerce/src/Blocks/Utils/CartCheckoutUtils.php b/plugins/woocommerce/src/Blocks/Utils/CartCheckoutUtils.php index e1d74d2c65e..24ca32dad2a 100644 --- a/plugins/woocommerce/src/Blocks/Utils/CartCheckoutUtils.php +++ b/plugins/woocommerce/src/Blocks/Utils/CartCheckoutUtils.php @@ -105,4 +105,31 @@ class CartCheckoutUtils { asort( $array_without_accents ); return array_replace( $array_without_accents, $array ); } + + /** + * Retrieves formatted shipping zones from WooCommerce. + * + * @return array An array of formatted shipping zones. + */ + public static function get_shipping_zones() { + $shipping_zones = \WC_Shipping_Zones::get_zones(); + $formatted_shipping_zones = array_reduce( + $shipping_zones, + function( $acc, $zone ) { + $acc[] = [ + 'id' => $zone['id'], + 'title' => $zone['zone_name'], + 'description' => $zone['formatted_zone_location'], + ]; + return $acc; + }, + [] + ); + $formatted_shipping_zones[] = [ + 'id' => 0, + 'title' => __( 'International', 'woocommerce' ), + 'description' => __( 'Locations outside all other zones', 'woocommerce' ), + ]; + return $formatted_shipping_zones; + } }