WooCommerce Code Reference

CartCheckoutUtils
in package

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

Table of Contents

$is_cart_page  : bool
Caches if we're on the cart page.
$is_checkout_page  : bool
Caches if we're on the checkout page.
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.
has_cart_page()  : bool
Check if the cart page is defined.
is_cart_block_default()  : bool
Checks if the default cart page is using the Cart block.
is_cart_page()  : bool
Returns true on the cart page.
is_checkout_block_default()  : bool
Checks if the default checkout page is using the Checkout block.
is_checkout_page()  : bool
Returns true on the checkout page.
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.
is_page_type()  : bool|null
Returns true if the current page is a specific page type (cart or checkout).

Properties

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, string $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 : string

The post content to check.

Return values
bool

has_cart_page()

Check if the cart page is defined.

public static has_cart_page() : bool
Return values
boolTrue if the cart page is defined, false otherwise.

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_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_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> $sort_array) : array<string|int, mixed>
Parameters
$sort_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

is_page_type()

Returns true if the current page is a specific page type (cart or checkout).

private static is_page_type(string $page_type) : bool|null

This is determined by looking at the global $post object and comparing it to the post ID defined in settings, or checking the page contents for a block or shortcode.

This function cannot be used accurately before the pre_get_posts action has been run.

Parameters
$page_type : string

The page type to check for.

Return values
bool|null