I am developing an online store with WooCommerce. I have managed to display a quantity selector using this code in the functions file:
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;
foreach ($product->get_available_variations() as $key) {
if ( ! empty( $defaults['max_value'] ) )
$max = $key['max_qty'];
else $max = $key['max_qty'];
}
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>';
}
It works perfect under the single product pages, but the problem comes on the cart page, giving this error:
Fatal error: Call to a member function get_available_variations() on a non-object in functions.php on line 240 -> foreach ($product->get_available_variations() as $key) {
Does anyone know how could I fix this?
Single product: http://www.ticketexpress.com.au/product/hong-kong-sevens-friday-tickets/
Cart page: http://www.ticketexpress.com.au/cart/
I can see you’re overriding a woocommerce function. I won’t suggest doing what your doing especially on a plugin. It’s dangerous. Other plugins might be using that function. There’s a filter you can use to changed the args. And there’s a woocommerce template for quantity input you can copy to your theme and do your edits. This should be the safest and the right way of doing it.
if you need to change the
$default
or the given$args
usewoocommerce_quantity_input_args
filter…something like this… copy this to your
functions.php
then locate
quantity-input.php
file in your plugins folder,woocommerce/templates/global/quantity-input.php
. Copy it toyourtheme/woocommerce/global/quantity-input.php
Do your edits. Remove the codes there and replace it with the code below.. These next lines of codes will give you exactly what you are trying to do now.