Automattic\WooCommerce\Blocks\BlockTypes

AddToCartWithOptionsQuantitySelector::add_steppers()privateWC 1.0

Add increment and decrement buttons to the quantity input field.

Method of the class: AddToCartWithOptionsQuantitySelector{}

No Hooks.

Return

Stringa. Quantity input HTML with increment and decrement buttons.

Usage

// private - for code of main (parent) class only
$result = $this->add_steppers( $product_html, $product_name );
$product_html(string) (required)
Quantity input HTML.
$product_name(string) (required)
Product name.

AddToCartWithOptionsQuantitySelector::add_steppers() code WC 9.6.1

private function add_steppers( $product_html, $product_name ) {
	// Regex pattern to match the <input> element with id starting with 'quantity_'.
	$pattern = '/(<input[^>]*id="quantity_[^"]*"[^>]*\/>)/';
	// Replacement string to add button BEFORE the matched <input> element.
	/* translators: %s refers to the item name in the cart. */
	$minus_button = '<button aria-label="' . esc_html( sprintf( __( 'Reduce quantity of %s', 'woocommerce' ), $product_name ) ) . '"type="button" data-wc-on--click="actions.removeQuantity" class="wc-block-components-quantity-selector__button wc-block-components-quantity-selector__button--minus">-</button>$1';
	// Replacement string to add button AFTER the matched <input> element.
	/* translators: %s refers to the item name in the cart. */
	$plus_button = '$1<button aria-label="' . esc_html( sprintf( __( 'Increase quantity of %s', 'woocommerce' ), $product_name ) ) . '" type="button" data-wc-on--click="actions.addQuantity" class="wc-block-components-quantity-selector__button wc-block-components-quantity-selector__button--plus">+</button>';
	$new_html    = preg_replace( $pattern, $minus_button, $product_html );
	$new_html    = preg_replace( $pattern, $plus_button, $new_html );
	return $new_html;
}