-
-
Save Antonio-Laguna/8eab29f48a06277a6856 to your computer and use it in GitHub Desktop.
<?php | |
// Place the following code in your theme's functions.php file | |
// override the quantity input with a dropdown | |
// Note that you still have to invoke this function like this: | |
/* | |
$product_quantity = woocommerce_quantity_input( array( | |
'input_name' => "cart[{$cart_item_key}][qty]", | |
'input_value' => $cart_item['quantity'], | |
'max_value' => $_product->backorders_allowed() ? '' : $_product->get_stock_quantity(), | |
'min_value' => '0' | |
), $_product, false ); | |
*/ | |
function woocommerce_quantity_input($data) { | |
global $product; | |
$defaults = array( | |
'input_name' => $data['input_name'], | |
'input_value' => $data['input_value'], | |
'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 ), | |
'style' => apply_filters( 'woocommerce_quantity_style', 'float:left; margin-right:10px;', $product ) | |
); | |
if ( ! empty( $defaults['min_value'] ) ) | |
$min = $defaults['min_value']; | |
else $min = 1; | |
if ( ! empty( $defaults['max_value'] ) ) | |
$max = $defaults['max_value']; | |
else $max = 20; | |
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' : ''; | |
$options .= '<option value="' . $count . '"'.$selected.'>' . $count . '</option>'; | |
} | |
echo '<div class="quantity_select" style="' . $defaults['style'] . '"><select name="' . esc_attr( $defaults['input_name'] ) . '" title="' . _x( 'Qty', 'Product quantity input tooltip', 'woocommerce' ) . '" class="qty">' . $options . '</select></div>'; | |
} | |
?> |
This is awesome! Thx for sharing! One question though, it works great except on the product page if you select more then one qty it doesn't reflect this in the cart. You have to manual update the qty in the cart a second time and update cart?
Same issue as @pxabrown, any ideas?
I am not really in to PHP, but this worked for me - https://gist.github.com/ignaty/4e3610331b676ea2a9d5
Implemented this, and it works on desktop, but strangely it does not work on mobile. Any idea why? Clarification: It works on the product page, but not viewing cart on mobile. It shows a quantity of 1 on mobile. But, if you change it again on the cart it seems to save correctly.
I was using your code in combination with the following code (found it here: https://gist.github.com/mikaelz/f41e29c6a99a595602e4) to auto update the cart after quantity changed and it worked well until woocommerce version 3.0.
add_action( 'wp_footer', 'cart_update_qty_script' );
function cart_update_qty_script() {
if (is_cart()) :
?>
<script>
jQuery('div.woocommerce').on('change', '.qty', function(){
jQuery("[name='update_cart']").removeAttr('disabled');
jQuery("[name='update_cart']").trigger("click");
});
</script>With woocommerce 3.0.X, it works only for every second update. So if I change the quantity, it does not update. If I change it again, it does update. Any idea how to fix this? Many thanks!
To get it working on both product pages and cart, you need to ensure the name
attribute is set correctly.
if (!empty($defaults['input_name'])) {
$name = esc_attr($defaults['input_name']);
} else {
$name = 'quantity';
}
And then use this in your select element, mine is like so
<select name="' . $name . '" title="' . _x( 'Qty', 'Product quantity input tooltip', 'woocommerce' ) . '" class="qty">' . $options . '</select></div>';
/**---------------------------------------------------------------------------
* Change quantity in to dropdown
----------------------------------------------------------------------------*/
function woocommerce_quantity_input($data = null) {
global $product;
if (!$data) {
$defaults = array(
'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 ),
'style' => apply_filters( 'woocommerce_quantity_style', 'float:left;', $product )
);
} else {
$defaults = array(
'input_name' => $data['input_name'],
'input_value' => $data['input_value'],
'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 ),
'style' => apply_filters( 'woocommerce_quantity_style', 'float:left;', $product )
);
}
if ( ! empty( $defaults['min_value'] ) )
$min = $defaults['min_value'];
else $min = 1;
if ( ! empty( $defaults['max_value'] ) )
$max = $defaults['max_value'];
else $max = 100;
if ( ! empty( $defaults['step'] ) )
$step = $defaults['step'];
else $step = 1;
$options = '';
//fix add to cart data to send on cart page
if (!empty($defaults['input_name'])) {
$name = esc_attr($defaults['input_name']);
} else {
$name = 'quantity';
}
for ( $count = $min; $count <= $max; $count = $count+$step ) {
$selected = $count === $defaults['input_value'] ? ' selected' : '';
$options .= '<option value="' . $count . '"'.$selected.'>' . $count . '</option>';
}
echo '<div class="quantity_select" style="' . $defaults['style'] . '"><select name="' . $name . '" title="' . _x( 'Qty', 'Product quantity input tooltip', 'woocommerce' ) . '" class="qty">' . $options . '</select></div>';
}
This is great!
Pardon the question I'm new to php/woocommerce and slowly learning.
Would it be possible / take a lot of work to alter it to pull the drop down amounts from a shared attribute’s values for every product id?
aka
Pogosticks attribute = pa_sell-quantities [ 10|20|40|80|160 ]
Beachballs attribute= pa_sell-quantities [ 20|40|80|100|1000]