Hide the shipping address form from the Checkout when the "Force shipping to the customer billing address" is enabled (https://github.com/woocommerce/woocommerce-blocks/pull/7268)

* Hide shipping address form from the Checkout when forced billing address is enabled.

* Update shipping address when billing address is changed

* Display shipping options

* Fixed needs shipping condition

Co-authored-by: Saad Tarhi <saad.trh@gmail.com>
This commit is contained in:
Tarun Vijwani 2022-10-07 11:50:26 +04:00 committed by GitHub
parent 27ad42cd20
commit 75d6e84552
4 changed files with 18 additions and 6 deletions

View File

@ -6,6 +6,7 @@ import {
AddressFields,
ShippingAddress,
BillingAddress,
getSetting,
} from '@woocommerce/settings';
import { useCallback } from '@wordpress/element';
import { useDispatch, useSelect } from '@wordpress/data';
@ -30,6 +31,7 @@ interface CheckoutAddress {
defaultAddressFields: AddressFields;
showShippingFields: boolean;
showBillingFields: boolean;
forcedBillingAddress: boolean;
}
/**
@ -72,7 +74,10 @@ export const useCheckoutAddress = (): CheckoutAddress => {
} ),
[ setShippingAddress ]
);
const forcedBillingAddress: boolean = getSetting(
'forcedBillingAddress',
false
);
return {
shippingAddress,
billingAddress,
@ -84,7 +89,9 @@ export const useCheckoutAddress = (): CheckoutAddress => {
defaultAddressFields,
useShippingAsBilling,
setUseShippingAsBilling: __internalSetUseShippingAsBilling,
showShippingFields: needsShipping,
showBillingFields: ! needsShipping || ! useShippingAsBilling,
showShippingFields: ! forcedBillingAddress && needsShipping,
showBillingFields:
forcedBillingAddress || ! needsShipping || ! useShippingAsBilling,
forcedBillingAddress,
};
};

View File

@ -37,11 +37,12 @@ const Block = ( {
defaultAddressFields,
billingAddress,
setBillingAddress,
setShippingAddress,
setBillingPhone,
} = useCheckoutAddress();
const { dispatchCheckoutEvent } = useStoreEvents();
const { isEditor } = useEditorContext();
const { forcedBillingAddress } = useCheckoutAddress();
// Clears data if fields are hidden.
useEffect( () => {
if ( ! showPhoneField ) {
@ -74,6 +75,9 @@ const Block = ( {
type="billing"
onChange={ ( values: Partial< BillingAddress > ) => {
setBillingAddress( values );
if ( forcedBillingAddress ) {
setShippingAddress( values );
}
dispatchCheckoutEvent( 'set-billing-address' );
} }
values={ billingAddress }

View File

@ -35,9 +35,9 @@ const FrontendBlock = ( {
const checkoutIsProcessing = useSelect( ( select ) =>
select( CHECKOUT_STORE_KEY ).isProcessing()
);
const { showShippingFields } = useCheckoutAddress();
const { showShippingFields, forcedBillingAddress } = useCheckoutAddress();
if ( ! showShippingFields ) {
if ( ! showShippingFields && ! forcedBillingAddress ) {
return null;
}

View File

@ -236,6 +236,7 @@ class Checkout extends AbstractBlock {
$this->asset_data_registry->add( 'checkoutShowLoginReminder', filter_var( get_option( 'woocommerce_enable_checkout_login_reminder' ), FILTER_VALIDATE_BOOLEAN ), true );
$this->asset_data_registry->add( 'displayCartPricesIncludingTax', 'incl' === get_option( 'woocommerce_tax_display_cart' ), true );
$this->asset_data_registry->add( 'displayItemizedTaxes', 'itemized' === get_option( 'woocommerce_tax_total_display' ), true );
$this->asset_data_registry->add( 'forcedBillingAddress', 'billing_only' === get_option( 'woocommerce_ship_to_destination' ), true );
$this->asset_data_registry->add( 'taxesEnabled', wc_tax_enabled(), true );
$this->asset_data_registry->add( 'couponsEnabled', wc_coupons_enabled(), true );
$this->asset_data_registry->add( 'shippingEnabled', wc_shipping_enabled(), true );