A simple, powerful and independent e-commerce platform.
Sell anything with ease.

Add quantity selector to the category page

The default Classic Commerce setup displays the add-to-cart button on the category page. The quantity selector is only available on the individual product page, or in the cart. This code adds a quantity selector onto the category page as well.

/**
 * Add quantity selector to the category page
 */

add_filter('woocommerce_loop_add_to_cart_link', 'cc_add_quantity_inputs_on_category_page', 10, 2);

function cc_add_quantity_inputs_on_category_page( $html, $product ) {

  if ($product && $product->is_type('simple') && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually()) {
    $html = '<form action="' . esc_url($product->add_to_cart_url()) . '" class="cart" method="post" enctype="multipart/form-data">';
    $html .= woocommerce_quantity_input(array(), $product, false);
    $html .= '<button type="submit" class="button alt">' . esc_html( $product->add_to_cart_text() ) . '</button>';
    $html .= '</form>';
  }

  return $html;

}
View Raw Code ID: 927