Automattic\WooCommerce\Blocks\Utils
CartCheckoutUtils::migrate_checkout_block_field_visibility_attributes()
Migrate checkout block field visibility attributes to settings when using the checkout block.
This migration routine is called if the options (woocommerce_checkout_phone_field, woocommerce_checkout_company_field, woocommerce_checkout_address_2_field) are not set. They are not set by default; they were orignally set by the customizer interface of the legacy shortcode based checkout.
Once migration is initiated, the settings will be updated and will not trigger this routine again.
Note: The block only stores non-default attributes. Not all attributes will be present.
e.g. {"showCompanyField":true,"requireCompanyField":true,"showApartmentField":false,"className":"wc-block-checkout"}
If the attributes are missing, we assume default values are needed.
Method of the class: CartCheckoutUtils{}
No Hooks.
Return
null
. Nothing (null).
Usage
$result = CartCheckoutUtils::migrate_checkout_block_field_visibility_attributes();
CartCheckoutUtils::migrate_checkout_block_field_visibility_attributes() CartCheckoutUtils::migrate checkout block field visibility attributes code WC 9.6.0
protected static function migrate_checkout_block_field_visibility_attributes() { // Before migrating attributes, migrate the "default" options checkout block uses into the settings. update_option( 'woocommerce_checkout_phone_field', 'optional' ); update_option( 'woocommerce_checkout_company_field', 'hidden' ); update_option( 'woocommerce_checkout_address_2_field', 'optional' ); // Parse the block from the checkout page. $checkout_blocks = \WC_Blocks_Utils::get_blocks_from_page( 'woocommerce/checkout', 'checkout' ); if ( empty( $checkout_blocks ) || ! isset( $checkout_blocks[0]['attrs'] ) ) { return; } // Combine actual attributes with default values. $block_attributes = wp_parse_args( $checkout_blocks[0]['attrs'], array( 'showPhoneField' => true, 'requirePhoneField' => false, 'showCompanyField' => false, 'requireCompanyField' => false, 'showApartmentField' => true, 'requireApartmentField' => false, ) ); if ( $block_attributes['showPhoneField'] ) { update_option( 'woocommerce_checkout_phone_field', $block_attributes['requirePhoneField'] ? 'required' : 'optional' ); } else { update_option( 'woocommerce_checkout_phone_field', 'hidden' ); } if ( $block_attributes['showCompanyField'] ) { update_option( 'woocommerce_checkout_company_field', $block_attributes['requireCompanyField'] ? 'required' : 'optional' ); } else { update_option( 'woocommerce_checkout_company_field', 'hidden' ); } if ( $block_attributes['showApartmentField'] ) { update_option( 'woocommerce_checkout_address_2_field', $block_attributes['requireApartmentField'] ? 'required' : 'optional' ); } else { update_option( 'woocommerce_checkout_address_2_field', 'hidden' ); } }