I am using woocommerce_quantity_input
pluggable function to modify the quantity input from a text box to a dropdown on my site.
On the cart page, when outputting the quantity input, I need to get the product ID so I can grab an ACF field from the single product page.
My code:
function woocommerce_quantity_input($data = null, $args = array(), $echo = true) {
global $product;
$set_quantity_limit = get_field('set_quantity_limit');
if ( !$data || is_product() ) {
$defaults = array(
'input_id' => '',
'input_name' => 'quantity',
'input_value' => '1',
'max_value' => apply_filters( 'woocommerce_quantity_input_max', '', $product ),
'min_value' => apply_filters( 'woocommerce_quantity_input_min', '', $product ),
'step' => apply_filters( 'woocommerce_quantity_input_step', '1', $product ),
);
} else {
$defaults = array(
'input_id' => $data['input_id'],
'input_name' => $data['input_name'],
'input_value' => $data['input_value'],
'step' => apply_filters( 'cw_woocommerce_quantity_input_step', '1', $product ),
'max_value' => apply_filters( 'cw_woocommerce_quantity_input_max', '', $product ),
'min_value' => apply_filters( 'cw_woocommerce_quantity_input_min', '', $product ),
);
}
if($set_quantity_limit){
if ( ! $product->is_sold_individually() ) {
$min = $defaults['min_value'] = 1;
$max = $defaults['max_value'] = $set_quantity_limit;
$step = $defaults['step'] = 1;
}
} else {
if ( ! empty( $defaults['min_value'] ) )
$min = $defaults['min_value'];
else $min = 1;
if ( ! empty( $defaults['max_value'] ) )
$max = $defaults['max_value'];
else $max = 6;
if ( ! empty( $defaults['step'] ) )
$step = $defaults['step'];
else $step = 1;
}
$options = '';
for ( $count = $min; $count <= $max; $count = $count+$step ) {
$selected = (($count == $defaults['input_value']) ? ' selected' : '');
$suffix_text_with_count = $count . ( ( $count == 6 ) ? ' - 1 Mastercase' : ' box - 12 ct.' );
$options .= '<option value="' . $count . '"'.$selected.'>' . ( ( $set_quantity_limit ) ? $count : $suffix_text_with_count ) . '</option>';
}
$string = '<div class="quantity quantity_select" style="' . $defaults['style'] . '">';
$string .= '<label class="screen-reader-text" for="' . esc_attr( $defaults['input_id'] ) . '">' . _x( 'Quantity', 'woocommerce' ) . '</label>';
$string .= '<select ';
$string .= 'name="' . esc_attr( $defaults['input_name'] ) . '" ';
$string .= 'title="' . _x( 'Qty', 'Product Description', 'woocommerce' ) . '" ';
$string .= 'class="qty">';
$string .= $options;
$string .= '</select>';
$string .= '</div>';
if ( $echo ) {
echo $string;
} else {
return $string;
}
}
This function applies the changes to all quantity inputs, not just those on the shop page, the single product page and the cart page.