Utils
in package
Utility methods used for the Add to Cart + Options block.
Table of Contents
- add_quantity_stepper_classes() : string
- Add classes to the Quantity Selector needed for the stepper style.
- add_quantity_steppers() : string
- Add increment and decrement buttons to the quantity input field.
- get_product_from_context() : WC_Product|null
- Get product from block context.
- get_product_quantity_constraints() : array<string|int, mixed>
- Get the quantity constraints for a product.
- has_visible_quantity_input() : bool
- Check if the HTML content has a visible quantity input.
- is_min_max_quantity_same() : bool
- Check if min and max purchase quantity are the same for a product.
- is_not_purchasable_product() : bool
- Check if a product is not purchasable or not in stock.
- make_quantity_input_interactive() : string
- Make the quantity input interactive by wrapping it with the necessary data attribute and adding a blur event listener.
- render_block_with_context() : string
- Renders a new block with custom context
Methods
add_quantity_stepper_classes()
Add classes to the Quantity Selector needed for the stepper style.
public
static add_quantity_stepper_classes(string $quantity_html) : string
Parameters
- $quantity_html : string
-
The Quantity Selector HTML.
Return values
string — The Quantity Selector HTML with classes added.add_quantity_steppers()
Add increment and decrement buttons to the quantity input field.
public
static add_quantity_steppers(string $quantity_html, string $product_name) : string
Parameters
- $quantity_html : string
-
Quantity input HTML.
- $product_name : string
-
Product name.
Return values
string — Quantity input HTML with increment and decrement buttons.get_product_from_context()
Get product from block context.
public
static get_product_from_context(WP_Block $block, WC_Product|null $previous_product) : WC_Product|null
Parameters
- $block : WP_Block
-
The block instance.
- $previous_product : WC_Product|null
-
The previous product (usually from global scope).
Return values
WC_Product|null — The product instance or null if not found.get_product_quantity_constraints()
Get the quantity constraints for a product.
public
static get_product_quantity_constraints(WC_Product $product) : array<string|int, mixed>
Parameters
- $product : WC_Product
-
The product to get the quantity constraints for.
Return values
array<string|int, mixed> — The quantity constraints.has_visible_quantity_input()
Check if the HTML content has a visible quantity input.
public
static has_visible_quantity_input(string $html_content) : bool
Parameters
- $html_content : string
-
The HTML content.
Return values
bool — True if the HTML content has a visible input, false otherwise.is_min_max_quantity_same()
Check if min and max purchase quantity are the same for a product.
public
static is_min_max_quantity_same(WC_Product $product) : bool
Parameters
- $product : WC_Product
-
The product to check.
Return values
bool — True if min and max purchase quantity are the same, false otherwise.is_not_purchasable_product()
Check if a product is not purchasable or not in stock.
public
static is_not_purchasable_product(WC_Product $product) : bool
Parameters
- $product : WC_Product
-
The product to check.
Return values
bool — True if the product is not purchasable or not in stock.make_quantity_input_interactive()
Make the quantity input interactive by wrapping it with the necessary data attribute and adding a blur event listener.
public
static make_quantity_input_interactive(string $quantity_html[, array<string|int, mixed> $wrapper_attributes = array() ][, array<string|int, mixed> $input_attributes = array() ][, array<string|int, mixed> $context = array() ][, bool $set_product_context = false ]) : string
Parameters
- $quantity_html : string
-
The quantity HTML.
- $wrapper_attributes : array<string|int, mixed> = array()
-
Optional wrapper attributes.
- $input_attributes : array<string|int, mixed> = array()
-
Optional input attributes.
- $context : array<string|int, mixed> = array()
-
{ Optional context for quantity input. @type int $productId Product ID for context-specific behavior. @type bool $allowZero Whether to allow zero quantity. }
- $set_product_context : bool = false
-
Whether to set a local woocommerce/products context on the wrapper. Only needed when the quantity input belongs to a different product than the one provided by the inherited context (e.g. child items in grouped products). Setting this unnecessarily shadows the parent context and prevents variationId updates from propagating.
Return values
string — The quantity HTML with interactive wrapper.render_block_with_context()
Renders a new block with custom context
public
static render_block_with_context(WP_Block $block, array<string|int, mixed> $context) : string
Parameters
- $block : WP_Block
-
The block instance.
- $context : array<string|int, mixed>
-
The context for the new block.
