How can i add multiple products to cart with quantity at same time in magento2?

44 Views Asked by At

My currunt url: http://viva-wireless.local/apple/iphone.html#

I want to add all product to cart with quantity who has quantity value is more then 0 while i click on "Add All To Cart" button.

my product display page

=> I want something like this: https://bsscommerce.com/magento-2-add-multiple-products-to-cart-extension.html

Comment if you have any idea.

I tried to modify "app/code/Magepow/Layerednav/view/frontend/templates/category/products.phtml" file by following code but not works for me.

<?php if (!$block->isContentMode() || $block->isMixedMode()): ?>
    <input type="checkbox" id="selectall" name="selectall" value="selectall">Select All</input>
    <button type="submit" title="Add to Cart 11111" class="action tocart tocart-text primary mt-3" id="addAllToCartBtn">
        <i class="icon icon-add-to-cart"></i> add all to cart
    </button>

    <div id="layerednav-list-products">
        <?php echo $block->getProductListHtml() ?>
    </div>
    <script type="text/x-magento-init">
        {
            ".quickview-product [data-role=quickview-button]": {
                "productQuickview": {}
            }
        }
    </script>

    <script type="text/javascript">
        require(['jquery'], function ($) {
            $(document).ready(function () {
                $('#addAllToCartBtn').click(function (e) {
                    e.preventDefault();

                    // Trigger click on all "Add to Cart" buttons
                    $('.action.tocart').click();

                    // Perform AJAX request
                    $.ajax({
                        url: '<?php echo $block->getUrl('yourmodule/controller/action'); ?>',
                        type: 'post',
                        dataType: 'json',
                        success: function (response) {
                            // Handle the response here (e.g., display a success message)
                            console.log(response);
                        },
                        error: function (xhr, status, errorThrown) {
                            console.log('Error happens. Try again.');
                            console.log(errorThrown);
                        }
                    });
                });
            });
        });
    </script>

<?php endif; ?>
0

There are 0 best solutions below