Woocommerce Quantity Dropdown Selector Fatal Error

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:

Read More

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/

Related posts

1 comment

  1. 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 use woocommerce_quantity_input_args filter…

    something like this… copy this to your functions.php

    add_filter('woocommerce_quantity_input_args','my_quantity_input_args',10,2);
    function my_quantity_input_args( $args, $product ){
    
        // add style to the $default;
        $args['style'] = apply_filters( 'woocommerce_quantity_style', 'float:left;', $product );
    
        // check for min
        if ( empty( $args['min_value'] ) )
            $args['min_value'] = 1;
    
        // check for max
        if ( empty( $args['max_value'] ) )
            $args['max_value'] = 8; // I'm not sure where to get your max_qty.
    
        return $args;
    }
    

    then locate quantity-input.php file in your plugins folder, woocommerce/templates/global/quantity-input.php. Copy it to yourtheme/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.

      for ( $count = $min_value; $count <= $max_value; $count = $count+$step ) {
        $selected = $count === $input_value ? ' selected' : '';
        $options .= '<option value="' . $count . '"'.$selected.'>' . $count . '</option>';
      }
    
      echo '<div class="quantity_select" style="' . $style . '" ><select name="' . esc_attr( $input_name ) . '" title="' . _x( 'Qty', 'Product quantity input tooltip', 'woocommerce' ) . '" class="qty">' . $options . '</select></div>';
    

Comments are closed.