Woocommerce – adding x number of products thru add-to-cart

has anyone been able to add multiple products of the same kind, with a click of add-to-cart button. I have enclosed a picture to show what I would like to build.

Objective: To be able to add multiples of a product to cart at once on my CATALOG page, NOT viewing product page.

Read More

This is what I want to build. Check ink.talentosoft.com, imagine, under the products, the "Details" link is to replace the number input, and "Add to Cart" to replace for the add cart button that can handle the number input

This is what I want to build. Check ink.talentosoft.com, imagine, under the products, the “Details” link is to replace the number input, and “Add to Cart” to replace for the add cart button that can handle the number input

I have looked at the code for single product/add-to-cart/simple product, the problem is: the number input and add to cart button are enclosed in a form element. Due to wp code being spread out, I am unable to find the handler for this form. If the handler for the form is found, may I theoretically copy past the simple.php add multiples section, right into my catalog page?

I have looked at and tried to use hooks do_action( "woocommerce_simple_add_to_cart" ); to bring up the single.php file.

I want to ask about the woocommerce_add_to_cart_action() method in plugin/woocommerce/woocommerce-function.php, Can I directly call this method ( add a hook to this method ), How do I keep track of the number input so I know how many products the user wants in the cart?

If I make this change, will this neglect woo’s AJAX functionality to add to cart? Of course depending on what stage of the add to cart transaction I am able to squeeze in to this event handler.

Related posts

Leave a Reply

1 comment

  1. This solution helped me a lot.

    Create this file inside your theme: woocommerce/loop/add-to-cart.php. And add the following code to it:

    <?php
    /**
     * Custom Loop Add to Cart.
     * 
     * Template with quantity.
     *
     * @author      WooThemes
     * @package     WooCommerce/Templates
     * @version     1.6.4
     */
    
    if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly.
    
    global $product;
    ?>
    
    <?php if ( ! $product->is_in_stock() ) : ?>
    
        <a href="<?php echo apply_filters( 'out_of_stock_add_to_cart_url', get_permalink( $product->id ) ); ?>" class="button"><?php echo apply_filters( 'out_of_stock_add_to_cart_text', __( 'Read More', 'woocommerce' ) ); ?></a>
    
    <?php else : ?>
    
        <?php
            $link = array(
                'url'   => '',
                'label' => '',
                'class' => ''
            );
    
            switch ( $product->product_type ) {
                case "variable" :
                    $link['url']    = apply_filters( 'variable_add_to_cart_url', get_permalink( $product->id ) );
                    $link['label']  = apply_filters( 'variable_add_to_cart_text', __( 'Select options', 'woocommerce' ) );
                break;
                case "grouped" :
                    $link['url']    = apply_filters( 'grouped_add_to_cart_url', get_permalink( $product->id ) );
                    $link['label']  = apply_filters( 'grouped_add_to_cart_text', __( 'View options', 'woocommerce' ) );
                break;
                case "external" :
                    $link['url']    = apply_filters( 'external_add_to_cart_url', get_permalink( $product->id ) );
                    $link['label']  = apply_filters( 'external_add_to_cart_text', __( 'Read More', 'woocommerce' ) );
                break;
                default :
                    if ( $product->is_purchasable() ) {
                        $link['url']    = apply_filters( 'add_to_cart_url', esc_url( $product->add_to_cart_url() ) );
                        $link['label']  = apply_filters( 'add_to_cart_text', __( 'Add to cart', 'woocommerce' ) );
                        $link['class']  = apply_filters( 'add_to_cart_class', 'add_to_cart_button' );
                    } else {
                        $link['url']    = apply_filters( 'not_purchasable_url', get_permalink( $product->id ) );
                        $link['label']  = apply_filters( 'not_purchasable_text', __( 'Read More', 'woocommerce' ) );
                    }
                break;
            }
    
            // If there is a simple product.
            if ( $product->product_type == 'simple' ) {
                ?>
                <form action="<?php echo esc_url( $product->add_to_cart_url() ); ?>" class="cart" method="post" enctype="multipart/form-data">
                    <?php
                        // Displays the quantity box.
                        woocommerce_quantity_input();
                    ?>
                    <button type="submit" class="button alt"><?php echo $link['label']; ?></button>
                </form>
                <?php
            } else {
              echo apply_filters( 'woocommerce_loop_add_to_cart_link', sprintf('<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" class="%s button product_type_%s">%s</a>', esc_url( $link['url'] ), esc_attr( $product->id ), esc_attr( $product->get_sku() ), esc_attr( $link['class'] ), esc_attr( $product->product_type ), esc_html( $link['label'] ) ), $product, $link );
            }
    
        ?>
    
    <?php endif; ?>