Automattic\WooCommerce\Blocks\BlockTypes\AddToCartWithOptions
Utils::add_quantity_steppers
Add increment and decrement buttons to the quantity input field.
Метод класса: Utils{}
Хуков нет.
Возвращает
string. Quantity input HTML with increment and decrement buttons.
Использование
$result = Utils::add_quantity_steppers( $quantity_html, $product_name );
- $quantity_html(строка) (обязательный)
- Quantity input HTML.
- $product_name(строка) (обязательный)
- Product name.
Код Utils::add_quantity_steppers() Utils::add quantity steppers WC 11.2.1
public static function add_quantity_steppers( $quantity_html, $product_name ) {
// Regex pattern to match the <input> element with id starting with 'quantity_'.
$pattern = '/(<input[^>]*id="quantity_[^"]*"[^>]*\/>)/';
// Add the minus button BEFORE the matched <input> element so DOM order matches the
// visual order (− input +), giving a logical focus and reading sequence.
// Use preg_replace_callback to avoid backreference interpretation of $, \ sequences in product names.
$new_html = preg_replace_callback(
$pattern,
function ( $matches ) use ( $product_name ) {
/* translators: %s refers to the item name in the cart. */
$minus_aria = esc_attr( sprintf( __( 'Reduce quantity of %s', 'woocommerce' ), $product_name ) );
return '<button aria-label="' . $minus_aria . '" type="button" data-wp-on--click="actions.decreaseQuantity" data-wp-bind--disabled="!state.allowsDecrease" class="wc-block-components-quantity-selector__button wc-block-components-quantity-selector__button--minus">−</button>' . $matches[1];
},
$quantity_html ?? ''
);
// Add the plus button AFTER the matched <input> element.
$new_html = preg_replace_callback(
$pattern,
function ( $matches ) use ( $product_name ) {
/* translators: %s refers to the item name in the cart. */
$plus_aria = esc_attr( sprintf( __( 'Increase quantity of %s', 'woocommerce' ), $product_name ) );
return $matches[1] . '<button aria-label="' . $plus_aria . '" type="button" data-wp-on--click="actions.increaseQuantity" data-wp-bind--disabled="!state.allowsIncrease" class="wc-block-components-quantity-selector__button wc-block-components-quantity-selector__button--plus">+</button>';
},
$new_html ?? ''
);
return $new_html ?? '';
}