CheckoutFieldsSchema
11 months ago
Email
1 year ago
CheckoutFields.php
11 months ago
CheckoutFieldsAdmin.php
2 years ago
CheckoutFieldsFrontend.php
11 months ago
CheckoutLink.php
11 months ago
CreateAccount.php
1 year ago
DraftOrders.php
2 months ago
FeatureGating.php
1 year ago
GoogleAnalytics.php
2 years ago
Hydration.php
5 months ago
Notices.php
1 year ago
functions.php
2 years ago
functions.php
66 lines
| 1 | <?php |
| 2 | |
| 3 | use Automattic\WooCommerce\Blocks\Package; |
| 4 | use Automattic\WooCommerce\Blocks\Domain\Services\CheckoutFields; |
| 5 | |
| 6 | if ( ! function_exists( 'woocommerce_register_additional_checkout_field' ) ) { |
| 7 | /** |
| 8 | * Register a checkout field. |
| 9 | * |
| 10 | * @param array $options Field arguments. See CheckoutFields::register_checkout_field() for details. |
| 11 | * @throws \Exception If field registration fails. |
| 12 | */ |
| 13 | function woocommerce_register_additional_checkout_field( $options ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionDoubleUnderscore,PHPCompatibility.FunctionNameRestrictions.ReservedFunctionNames.FunctionDoubleUnderscore |
| 14 | |
| 15 | // Check if `woocommerce_blocks_loaded` ran. If not then the CheckoutFields class will not be available yet. |
| 16 | // In that case, re-hook `woocommerce_blocks_loaded` and try running this again. |
| 17 | $woocommerce_blocks_loaded_ran = did_action( 'woocommerce_blocks_loaded' ); |
| 18 | if ( ! $woocommerce_blocks_loaded_ran ) { |
| 19 | add_action( |
| 20 | 'woocommerce_blocks_loaded', |
| 21 | function () use ( $options ) { |
| 22 | woocommerce_register_additional_checkout_field( $options ); |
| 23 | } |
| 24 | ); |
| 25 | return; |
| 26 | } |
| 27 | $checkout_fields = Package::container()->get( CheckoutFields::class ); |
| 28 | $result = $checkout_fields->register_checkout_field( $options ); |
| 29 | if ( is_wp_error( $result ) ) { |
| 30 | throw new \Exception( esc_attr( $result->get_error_message() ) ); |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | if ( ! function_exists( '__experimental_woocommerce_blocks_register_checkout_field' ) ) { |
| 36 | |
| 37 | /** |
| 38 | * Register a checkout field. |
| 39 | * |
| 40 | * @param array $options Field arguments. See CheckoutFields::register_checkout_field() for details. |
| 41 | * @throws \Exception If field registration fails. |
| 42 | * @deprecated 5.6.0 Use woocommerce_register_additional_checkout_field() instead. |
| 43 | */ |
| 44 | function __experimental_woocommerce_blocks_register_checkout_field( $options ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionDoubleUnderscore,PHPCompatibility.FunctionNameRestrictions.ReservedFunctionNames.FunctionDoubleUnderscore |
| 45 | wc_deprecated_function( __FUNCTION__, '8.9.0', 'woocommerce_register_additional_checkout_field' ); |
| 46 | woocommerce_register_additional_checkout_field( $options ); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | if ( ! function_exists( '__internal_woocommerce_blocks_deregister_checkout_field' ) ) { |
| 51 | /** |
| 52 | * Deregister a checkout field. |
| 53 | * |
| 54 | * @param string $field_id Field ID. |
| 55 | * @throws \Exception If field deregistration fails. |
| 56 | * @internal |
| 57 | */ |
| 58 | function __internal_woocommerce_blocks_deregister_checkout_field( $field_id ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionDoubleUnderscore,PHPCompatibility.FunctionNameRestrictions.ReservedFunctionNames.FunctionDoubleUnderscore |
| 59 | $checkout_fields = Package::container()->get( CheckoutFields::class ); |
| 60 | $result = $checkout_fields->deregister_checkout_field( $field_id ); |
| 61 | if ( is_wp_error( $result ) ) { |
| 62 | throw new \Exception( esc_attr( $result->get_error_message() ) ); |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 |