Automattic\WooCommerce\Blocks\BlockTypes\AddToCartWithOptions
Utils::make_quantity_input_interactive
Make the quantity input interactive by wrapping it with the necessary data attribute and adding a blur event listener.
Метод класса: Utils{}
Хуков нет.
Возвращает
string. The quantity HTML with interactive wrapper.
Использование
$result = Utils::make_quantity_input_interactive( $quantity_html, $wrapper_attributes, $input_attributes, $context, $set_product_context );
- $quantity_html(строка) (обязательный)
- The quantity HTML.
- $wrapper_attributes(массив)
- Optional wrapper attributes.
По умолчанию:array() - $input_attributes(массив)
- Optional input attributes.
По умолчанию:array() - $context(массив)
Optional context for quantity input.
По умолчанию:
array()-
productId(int)
Product ID for context-specific behavior. - allowZero(true|false)
Whether to allow zero quantity.
-
- $set_product_context(true|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.
По умолчанию:false
Код Utils::make_quantity_input_interactive() Utils::make quantity input interactive WC 11.3.0
public static function make_quantity_input_interactive( $quantity_html, $wrapper_attributes = array(), $input_attributes = array(), $context = array(), $set_product_context = false ) {
$processor = new \WP_HTML_Tag_Processor( $quantity_html );
global $product;
if ( $set_product_context && $product instanceof \WC_Product ) {
$product_context = array(
'productId' => $product->get_id(),
'variationId' => null,
);
$products_context = 'woocommerce/products::' . wp_json_encode( $product_context, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP );
// This moves the `woocommerce/products` context to a nested `div`,
// as multiple context directives are not supported in the same
// element in WordPress 6.8. Once WooCommerce drops support for
// WordPress 6.8, this code can be refactored.
if (
$processor->next_tag(
array(
'tag_name' => 'div',
'class_name' => 'quantity',
)
)
) {
$processor->set_attribute( 'data-wp-context', $products_context );
} else {
// If filtered markup omits the `div.quantity`, reinitialize the
// processor so the input bindings below still execute.
$processor = new \WP_HTML_Tag_Processor( $quantity_html );
}
}
if (
$processor->next_tag( 'input' ) &&
$processor->get_attribute( 'type' ) === 'number' &&
strpos( $processor->get_attribute( 'name' ), 'quantity' ) !== false
) {
$default_quantity = $product instanceof \WC_Product ? $product->get_min_purchase_quantity() : 1;
$input_quantity = isset( $context['allowZero'] ) && true === $context['allowZero'] ? 0 : $default_quantity;
wp_interactivity_state(
'woocommerce/add-to-cart-with-options-quantity-selector',
array(
'inputQuantity' => $input_quantity,
)
);
$processor->set_attribute( 'data-wp-on--blur', 'actions.handleQuantityBlur' );
$processor->set_attribute( 'data-wp-bind--value', 'state.inputQuantity' );
foreach ( $input_attributes as $attribute => $value ) {
$processor->set_attribute( $attribute, $value );
}
}
$quantity_html = $processor->get_updated_html();
$wrapper_attributes = array_merge(
array(
'data-wp-interactive' => 'woocommerce/add-to-cart-with-options-quantity-selector',
'data-wp-init' => 'callbacks.storeInputElementRef',
),
$wrapper_attributes
);
$context_attribute = wp_interactivity_data_wp_context( $context );
return sprintf(
'<div %1$s %2$s>%3$s</div>',
get_block_wrapper_attributes( $wrapper_attributes ),
$context_attribute,
$quantity_html
);
}