WooCommerce Code Reference

CartCheckoutUtils
in package

Class containing utility methods for dealing with the Cart and Checkout blocks.

Table of Contents

find_express_checkout_attributes()  : mixed
Recursively search the checkout block to find the express checkout block and get the button style attributes
get_address_2_field_visibility()  : string
Get the default visibility for the address_2 field.
get_company_field_visibility()  : string
Get the default visibility for the address_2 field.
get_country_data()  : array<string|int, mixed>
Gets country codes, names, states, and locale information.
get_phone_field_visibility()  : string
Get the default visibility for the address_2 field.
get_shipping_zones()  : array<string|int, mixed>
Retrieves formatted shipping zones from WooCommerce.
has_block_variation()  : bool
Check if the post content contains a block with a specific attribute value.
is_cart_block_default()  : bool
Checks if the default cart page is using the Cart block.
is_cart_page()  : bool
Returns true if: - The cart page is being viewed.
is_checkout_block_default()  : bool
Checks if the default checkout page is using the Checkout block.
is_checkout_page()  : bool
Returns true if: - The checkout page is being viewed.
is_overriden_by_custom_template_content()  : bool
Checks if the template overriding the page loads the page content or not.
shipping_methods_exist()  : bool
Returns true if shipping methods exist in the store. Excludes local pickup and only counts enabled shipping methods.
update_blocks_with_new_attrs()  : mixed
Given an array of blocks, find the express payment block and update its attributes.
deep_sort_with_accents()  : array<string|int, mixed>
Removes accents from an array of values, sorts by the values, then returns the original array values sorted.
migrate_checkout_block_field_visibility_attributes()  : mixed
Migrate checkout block field visibility attributes to settings when using the checkout block.

Methods

find_express_checkout_attributes()

Recursively search the checkout block to find the express checkout block and get the button style attributes

public static find_express_checkout_attributes(array<string|int, mixed> $blocks, string $cart_or_checkout) : mixed
Parameters
$blocks : array<string|int, mixed>

Blocks to search.

$cart_or_checkout : string

The block type to check.

Return values
mixed

get_address_2_field_visibility()

Get the default visibility for the address_2 field.

public static get_address_2_field_visibility() : string
Return values
string

get_company_field_visibility()

Get the default visibility for the address_2 field.

public static get_company_field_visibility() : string
Return values
string

get_country_data()

Gets country codes, names, states, and locale information.

public static get_country_data() : array<string|int, mixed>
Return values
array<string|int, mixed>

get_phone_field_visibility()

Get the default visibility for the address_2 field.

public static get_phone_field_visibility() : string
Return values
string

get_shipping_zones()

Retrieves formatted shipping zones from WooCommerce.

public static get_shipping_zones() : array<string|int, mixed>
Return values
array<string|int, mixed>An array of formatted shipping zones.

has_block_variation()

Check if the post content contains a block with a specific attribute value.

public static has_block_variation(string $block_id, string $attribute, string $value, mixed $post_content) : bool
Parameters
$block_id : string

The block ID to check for.

$attribute : string

The attribute to check.

$value : string

The value to check for.

$post_content : mixed
Return values
bool

is_cart_block_default()

Checks if the default cart page is using the Cart block.

public static is_cart_block_default() : bool
Return values
booltrue if the WC cart page is using the Cart block.

is_cart_page()

Returns true if: - The cart page is being viewed.

public static is_cart_page() : bool
  • The page contains a cart block, cart shortcode or classic shortcode block with the cart attribute.
Return values
bool

is_checkout_block_default()

Checks if the default checkout page is using the Checkout block.

public static is_checkout_block_default() : bool
Return values
booltrue if the WC checkout page is using the Checkout block.

is_checkout_page()

Returns true if: - The checkout page is being viewed.

public static is_checkout_page() : bool
  • The page contains a checkout block, checkout shortcode or classic shortcode block with the checkout attribute.
Return values
bool

is_overriden_by_custom_template_content()

Checks if the template overriding the page loads the page content or not.

public static is_overriden_by_custom_template_content(string $block) : bool

Templates by default load the page content, but if that block is deleted the content can get out of sync with the one presented in the page editor.

Parameters
$block : string

The block to check.

Return values
booltrue if the template has out of sync content.

shipping_methods_exist()

Returns true if shipping methods exist in the store. Excludes local pickup and only counts enabled shipping methods.

public static shipping_methods_exist() : bool
Return values
booltrue if shipping methods exist.

update_blocks_with_new_attrs()

Given an array of blocks, find the express payment block and update its attributes.

public static update_blocks_with_new_attrs(array<string|int, mixed> &$blocks, string $cart_or_checkout, array<string|int, mixed> $updated_attrs) : mixed
Parameters
$blocks : array<string|int, mixed>

Blocks to search.

$cart_or_checkout : string

The block type to check.

$updated_attrs : array<string|int, mixed>

The new attributes to set.

Return values
mixed

deep_sort_with_accents()

Removes accents from an array of values, sorts by the values, then returns the original array values sorted.

protected static deep_sort_with_accents(array<string|int, mixed> $array) : array<string|int, mixed>
Parameters
$array : array<string|int, mixed>

Array of values to sort.

Return values
array<string|int, mixed>Sorted array.

migrate_checkout_block_field_visibility_attributes()

Migrate checkout block field visibility attributes to settings when using the checkout block.

protected static migrate_checkout_block_field_visibility_attributes() : mixed

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.

Return values
mixed