Woocommerce Quantity Dropdown Selector Problem
-
Hi everyone,
I am developing an online store with WooCommerce. I have managed to display a quantity selector using this code in the functions file:
–start–
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>’;
}
–end—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: https://www.ticketexpress.com.au/product/hong-kong-sevens-friday-tickets/
Cart page: https://www.ticketexpress.com.au/cart/Thanks a lot!
- The topic ‘Woocommerce Quantity Dropdown Selector Problem’ is closed to new replies.