Display shipping calculator link in the Cart block when fallback shipping options available (#43803)

* Hide the shipping rates in editor to match front-end

- If there are no shipping rates, hide the shipping rates in editor to match front-end.

* Add get_shipping_zones method to CartCheckoutUtils

* Display change address like for fallback shipping zone

- If cart has multiple and default shipping zones without shopper address, display the change address link

* Update shipping address label

* Add changefile(s) from automation for the following project(s): woocommerce-blocks, woocommerce

* Fix linting errors

* Update plugins/woocommerce-blocks/assets/js/base/components/cart-checkout/totals/shipping/shipping-address.tsx

Co-authored-by: Niels Lange <info@nielslange.de>

* Display shipping calculator for locations outside all other zones

* Add changefile(s) from automation for the following project(s): woocommerce-blocks, woocommerce

* Update typescript for activeShippingZones

---------

Co-authored-by: github-actions <github-actions@github.com>
Co-authored-by: Niels Lange <info@nielslange.de>
This commit is contained in:
Tarun Vijwani 2024-01-24 20:40:53 +04:00 committed by GitHub
parent bbed678d13
commit 0c79d1b6a7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 73 additions and 26 deletions

View File

@ -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 && (
<CalculatorButton
label={ __( 'Change address', 'woocommerce' ) }
label={ label }
isShippingCalculatorOpen={ isShippingCalculatorOpen }
setIsShippingCalculatorOpen={ setIsShippingCalculatorOpen }
/>

View File

@ -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 (

View File

@ -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.

View File

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

View File

@ -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' ) ) {

View File

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