This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 221
Introduce Additional Fields extensibility API. #12073
Open
senadir
wants to merge
46
commits into
trunk
Choose a base branch
from
add/11585-custom-fields-api
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 37 commits
Commits
Show all changes
46 commits
Select commit
Hold shift + click to select a range
e1e8306
move address to PHP side
senadir 50c372a
support showing fields from server
senadir 33a07c1
add placeholders to code
senadir 6f90954
add data reading and saving functions
senadir 48ee2dd
Add woocommerce_blocks_register_checkout_field function
opr 88ee619
Add example code for registering a field
opr 95cf895
Create register_checkout_field function
opr 164f469
finish all code handling
senadir c09894c
fix all code errors
senadir 9e70517
Require functions from Domain/Services
opr 17a604a
fix how default address fields was used
senadir 5140eaa
Update order address with additional fields
opr 25f4394
Update default locale with fields without country limitation
opr d1fcd85
Update country locale with fields with country limitations
opr 4ab27fd
Allow country_limitation to be specified when registering fields
opr 8f0c8b4
persist user meta
senadir 2f0c1b3
Remove country limitation specific code
opr 42fbad6
add support for saving data in session
senadir a368f42
add todo to copy fields upon acount creation
senadir d323cd1
Update comment typo
opr 443a133
Move address field fetching to order controller
opr fa8e4c8
Remove hardcoded test fields
opr d48c613
Ensure fields are added to correct place when registered
opr 36f1460
Ensure fields are updated when changing custom value for first time
opr 041d724
Do not create field registration function unless build is experimental
opr 552b0f5
clean up code
senadir 60fb0b6
clean up checkoutFields functions
senadir 8ccaee8
rename get_fields
senadir 3a02c06
handle review comments
senadir 6c14f15
fix rebase error
senadir 58d422b
fix unit test
senadir c416753
remove todo
senadir 9d35d14
move fields defaults to jest
senadir 856cf89
Ensure shipping address includes additional fields
opr 6361137
Ensure billing address includes additional fields
opr 3641977
Ensure default cart state includes additional fields
opr 0fdfd6c
Revert "Ensure fields are updated when changing custom value for firs…
opr db497dc
fix default value for additional_fields key
senadir 70bf9ef
Use default fields from constants and dont get setting again
opr 5ada340
Ensure values have defaults if the config doesn't pass them
opr 0ab9b3f
Show warning if a field is registered as hidden
opr 704ce90
Fix lint error
opr 9d25427
Ensure register field runs only when woocommerce_blocks_loaded fired
opr 53694ad
only return registred fields in Store API schema
senadir f8567fb
move class calling to be inside classes
senadir b9aa65e
fix tests
senadir File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,16 +7,12 @@ import type { | |
CartResponseBillingAddress, | ||
CartResponseShippingAddress, | ||
} from '@woocommerce/types'; | ||
import { | ||
AddressFields, | ||
defaultAddressFields, | ||
ShippingAddress, | ||
BillingAddress, | ||
} from '@woocommerce/settings'; | ||
import { ShippingAddress, BillingAddress } from '@woocommerce/settings'; | ||
import { decodeEntities } from '@wordpress/html-entities'; | ||
import { | ||
SHIPPING_COUNTRIES, | ||
SHIPPING_STATES, | ||
ADDRESS_FIELDS_KEYS, | ||
} from '@woocommerce/block-settings'; | ||
|
||
/** | ||
|
@@ -26,10 +22,9 @@ export const isSameAddress = < T extends ShippingAddress | BillingAddress >( | |
address1: T, | ||
address2: T | ||
): boolean => { | ||
return Object.keys( defaultAddressFields ).every( | ||
( field: string ) => | ||
address1[ field as keyof T ] === address2[ field as keyof T ] | ||
); | ||
return Object.keys( ADDRESS_FIELDS_KEYS ).every( ( field: string ) => { | ||
return address1[ field as keyof T ] === address2[ field as keyof T ]; | ||
} ); | ||
Comment on lines
+25
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would previously check against all fields, it's changed to only check address. |
||
}; | ||
|
||
/** | ||
|
@@ -94,10 +89,11 @@ export const emptyHiddenAddressFields = < | |
>( | ||
address: T | ||
): T => { | ||
const fields = Object.keys( | ||
defaultAddressFields | ||
) as ( keyof AddressFields )[]; | ||
const addressFields = prepareAddressFields( fields, {}, address.country ); | ||
const addressFields = prepareAddressFields( | ||
ADDRESS_FIELDS_KEYS, | ||
{}, | ||
address.country | ||
); | ||
const newAddress = Object.assign( {}, address ) as T; | ||
|
||
addressFields.forEach( ( { key = '', hidden = false } ) => { | ||
|
@@ -160,10 +156,11 @@ export const isAddressComplete = ( | |
if ( ! address.country ) { | ||
return false; | ||
} | ||
const fields = Object.keys( | ||
defaultAddressFields | ||
) as ( keyof AddressFields )[]; | ||
const addressFields = prepareAddressFields( fields, {}, address.country ); | ||
const addressFields = prepareAddressFields( | ||
ADDRESS_FIELDS_KEYS, | ||
{}, | ||
address.country | ||
); | ||
|
||
return addressFields.every( | ||
( { key = '', hidden = false, required = false } ) => { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,12 @@ type CountryData = { | |
locale: Record< string, LocaleSpecificAddressField >; | ||
}; | ||
|
||
type FieldsLocations = { | ||
address: string[]; | ||
contact: string[]; | ||
additional: string[]; | ||
}; | ||
|
||
// Contains country names. | ||
const countries = getSetting< Record< string, string > >( 'countries', {} ); | ||
|
||
|
@@ -111,3 +117,35 @@ export const COUNTRY_LOCALE = Object.fromEntries( | |
return [ countryCode, countryData[ countryCode ].locale || [] ]; | ||
} ) | ||
); | ||
|
||
const defaultFieldsLocations: FieldsLocations = { | ||
address: [ | ||
'first_name', | ||
'last_name', | ||
'company', | ||
'address_1', | ||
'address_2', | ||
'city', | ||
'postcode', | ||
'country', | ||
'state', | ||
'phone', | ||
], | ||
contact: [ 'email' ], | ||
additional: [], | ||
}; | ||
Comment on lines
+121
to
+136
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This default alone isn't enough actually, because without field definition, Checkout would still not render right. |
||
|
||
export const ADDRESS_FIELDS_KEYS = getSetting< FieldsLocations >( | ||
'addressFieldsLocations', | ||
defaultFieldsLocations | ||
).address; | ||
|
||
export const CONTACT_FIELDS_KEYS = getSetting< FieldsLocations >( | ||
'addressFieldsLocations', | ||
defaultFieldsLocations | ||
).contact; | ||
|
||
export const ADDITIONAL_FIELDS_KEYS = getSetting< FieldsLocations >( | ||
'addressFieldsLocations', | ||
defaultFieldsLocations | ||
).additional; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Passing the whole fields as default no longer make sense,
fields
is now required.