skip to Main Content

I have used the code below but this adds the quantity adjustment next to the add the cart button on every product. I only want it to appear on specific products or a single product category. I have tried everything I can think of to call multiple products or the category and have failed. I am only a novice so that doesn’t help. Any help would be appreciated.

/**
 * Override loop template and show quantities next to add to cart buttons
 */
add_filter( 'woocommerce_loop_add_to_cart_link', 'quantity_inputs_for_woocommerce_loop_add_to_cart_link', 10, 2 );
function quantity_inputs_for_woocommerce_loop_add_to_cart_link( $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;
}

2

Answers


  1. Chosen as BEST ANSWER

    This is the code I Entered into Snipets Plugin and it does not produce any results. Am I missing something????

    add_filter( 'woocommerce_loop_add_to_cart_link', 'quantity_inputs_for_woocommerce_loop_add_to_cart_link', 10, 2 ); function quantity_inputs_for_woocommerce_loop_add_to_cart_link( $html, $product ) { if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually()) { // use your category slug Choose multiples by using || is_product_category('your-slug') if (is_product_category('table-lamps')){ $html = 'add_to_cart_url() ) . '" class="cart" method="post" enctype="multipart/form-data">'; $html .= woocommerce_quantity_input( array(), $product, false ); $html .= '' . esc_html( $product->add_to_cart_text() ) . ''; $html .= ''; } } return $html; }


  2. I think this is what you were looking for based on the question as I read it. All I did was add if (is_product_category('slug')) and the get_stock_quantity() functions.

    add_filter( 'woocommerce_loop_add_to_cart_link', 'quantity_inputs_for_woocommerce_loop_add_to_cart_link', 10, 2 );
    function quantity_inputs_for_woocommerce_loop_add_to_cart_link( $html, $product ) {
        if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually()) {
            // use your category slug Choose multiples by using || is_product_category('your-slug')
            if (is_product_category('YOUR-SLUG-HERE')){ 
                $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>';
                // Wasn't sure if you wanted to show the quantity - but this is how
                $html .= '<span id="stockQuantity">'.$product->get_stock_quantity().'</span>';
                $html .= '</form>';
            }
        }
        return $html;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search