Automattic\WooCommerce\Blocks\BlockTypes\AddToCartWithOptions
GroupedProductItemSelector::get_quantity_selector_markup
Gets the quantity selector markup for a product.
Method of the class: GroupedProductItemSelector{}
Hooks from the method
Returns
String. The HTML markup for the quantity selector.
Usage
// private - for code of main (parent) class only $result = $this->get_quantity_selector_markup( $product );
- $product(WC_Product) (required)
- The product object.
GroupedProductItemSelector::get_quantity_selector_markup() GroupedProductItemSelector::get quantity selector markup code WC 10.8.1
private function get_quantity_selector_markup( $product ) {
ob_start();
$min_value = $product->get_min_purchase_quantity();
$max_value = $product->get_max_purchase_quantity();
if ( $min_value === $max_value && $min_value > 0 ) {
add_filter( 'woocommerce_quantity_input_type', array( $this, 'set_quantity_input_type' ) );
}
woocommerce_quantity_input(
array(
'input_name' => 'quantity[' . $product->get_id() . ']',
'input_id' => 'quantity_' . $product->get_id(),
'input_value' => isset( $_POST['quantity'][ $product->get_id() ] ) ? wc_stock_amount( wc_clean( wp_unslash( $_POST['quantity'][ $product->get_id() ] ) ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing
'min_value' => 0,
'max_value' => $max_value,
/**
* Filter the placeholder value allowed for the product.
*
* @since 3.10.0
* @param int $max_value Maximum quantity value.
* @param WC_Product $product Product object.
*/
'placeholder' => apply_filters( 'woocommerce_quantity_input_placeholder', 0, $product ),
)
);
if ( $min_value === $max_value && $min_value > 0 ) {
remove_filter( 'woocommerce_quantity_input_type', array( $this, 'set_quantity_input_type' ) );
}
$quantity_html = ob_get_clean();
// Remove the label because we are rendering one as a separate block via GroupedProductItemLabel.
$quantity_html = $this->remove_quantity_label( $quantity_html );
// Modify the quantity input to add stepper buttons.
$product_name = $product->get_name();
$quantity_html = AddToCartWithOptionsUtils::add_quantity_steppers( $quantity_html, $product_name );
$quantity_html = AddToCartWithOptionsUtils::add_quantity_stepper_classes( $quantity_html );
$context = array(
'allowZero' => true, // The item is optional in grouped products.
);
// Add interactive data attribute for the stepper functionality.
// Pass $set_product_context = true because each grouped product child needs its own
// products context scope (the inherited context points to the grouped parent).
$quantity_html = AddToCartWithOptionsUtils::make_quantity_input_interactive( $quantity_html, array(), array(), $context, true );
return $quantity_html;
}