0

I'm trying to display a quantity box next to each of my product variations in the main shop page.

I have t-shirts listed in different colors and sizes and sell B2B. I therefore need the quantity boxes to show next to the variations for customers do do a quick order. I have researched and tried many plugins but none is able to achieve the function I am looking for.

0

Some time ago I had to do something similar. The difference was that the table was showed on single product page and not on main page... But it can be a good start for you, I guess...

<?php

function bavptc_bind_hooks() {
    remove_action( 'woocommerce_variable_add_to_cart', 'woocommerce_variable_add_to_cart', 30 );
    add_action( 'woocommerce_variable_add_to_cart', 'bavptc_woocommerce_variable_add_to_cart', 30 );

    //die('x');
}
add_action( 'plugins_loaded', 'bavptc_bind_hooks' );


function bavptc_woocommerce_variable_add_to_cart() {
    global $product;

    $variations = $product->get_available_variations();
    if ( $variations ) :
    ?>
        <form action="" method="POST">
            <table class="bavptc-variations">
                <thead>
                    <tr>
                        <th>Version</th>
                        <th>Code</th>

                        <th>Price</th>
                        <th>Quantity</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                        $_pf = new WC_Product_Factory();
                        foreach ($variations as $variation) :
                            if ( ! $variation['is_purchasable'] ) continue;
                            $_product = $_pf->get_product($variation['variation_id']);
                    ?>
                        <tr>
                            <?php foreach ( $variation['attributes'] as $k => $v ) : ?>
                            <td><?php esc_html_e($v); ?></td>
                            <?php endforeach; ?>
                            <td><?php esc_html_e($variation['sku']); ?></td>
                            <td><?php echo $_product->get_price_html(); ?></td>
                            <td><?php
                                woocommerce_quantity_input(array(
                                    'input_name' => "quantity[{$variation['variation_id']}]",
                                    'input_value' => 0
                                ), $_product, true);
                            ?></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
            <input type="submit" name="add-to-cart" value="Add to cart" />
        </form>
    <?php
    endif;
}


function bavptc__add_multiple_products_to_cart() {
    if ( ! class_exists( 'WC_Form_Handler' ) || empty( $_REQUEST['add-to-cart'] ) || ! is_array($_REQUEST['quantity']) ) {
        return;
    }

    // Remove WooCommerce's hook, as it's useless (doesn't handle multiple products).
    remove_action( 'wp_loaded', array( 'WC_Form_Handler', 'add_to_cart_action' ), 20 );

    foreach ( $_REQUEST['quantity'] as $product_id => $quantity ) {    
        $product_id        = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $product_id ) );
        $was_added_to_cart = false;
        $adding_to_cart    = wc_get_product( $product_id );

        if ( ! $adding_to_cart ) {
            continue;
        }

        $add_to_cart_handler = apply_filters( 'woocommerce_add_to_cart_handler', array($adding_to_cart, 'product_type'), $adding_to_cart );

        // For now, quantity applies to all products.. This could be changed easily enough, but I didn't need this feature.
        $passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity );

        if ( $passed_validation && false !== WC()->cart->add_to_cart( $product_id, $quantity ) ) {
            wc_add_to_cart_message( array( $product_id => $quantity ), true );
        }
    }
}
add_action( 'wp_loaded', 'bavptc__add_multiple_products_to_cart', 15 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.