使用购物车中的复选框添加多个产品 - Opencart

时间:2017-06-08 06:53:03

标签: jquery arrays ajax opencart shopping-cart

我想使用复选框在购物车中添加多个产品。其中我设计了以下代码。通过此代码,即使我选择多个复选框

,也只会添加一个产品
    <script type="text/javascript">

  //for ADone product

  function AddToCartAddon(){

    var currentproductId = $('#product [name^=product_id]').val(); 
    var delivery_city = $("#delivery_city").val();
    var delivery_date = $('#delivery_date').val();
    var addonProductIds = [];
    $(':checkbox:checked[name^=addonId]').val(function() {

       addonProductIds.push(this.value);

    });
      if(currentproductId != '' && addonProductIds.length > 0) {    
        $.ajax({

            url: 'index.php?route=checkout/cart/add',
            type: 'post',
            data: 'product_id=' +addonProductIds+ '&quantity=' + 1, //need to change this
            dataType: 'json',
            success: function(json) {
            $('.success, .warning, .attention, .information, .error').remove();

            if (json['redirect']) {
                location = json['redirect'];
            }

            if (json['success']) {
                $('#notification').html('<div class="success" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');

                $('.success').fadeIn('slow');

                $('#cart-total').html(json['total']);

                $('html, body').animate({ scrollTop: 0 }, 'slow'); 

            }   

    }
    });

        window.location.href = 'index.php?route=checkout/cart';

     } else{

        window.location.href = 'index.php?route=checkout/cart';

     }





  }



  function gotocart(url){

    location = url;

  }

我的HTML代码:`

            <div class="tab-pane tab-content active" id="top-selling" style="overflow:hidden;">

              <div class="row main-products product-grid">

                                    <div class="product-grid-item xs-50 sm-50 md-20 lg-20 xl-20 display-icon block-button">

                  <div class="product-thumb  product-wrapper">

                    <label for="check_51" style="margin:0px; padding:0px;">

                    <div class="product-details popbrtdr">

                      <div class="image"> <img alt="Price Surprise" class="lazy first-image"  width="100%" src="image/catalog/products/ET001.jpg" style="display: block;" title="Greeting Card As Per Occasion" > </div>

                      <div class="caption addcon">

                        <p class="small red">Greeting Car..</p>

                        <p class="small">Rs. 149 <span class="price-tax"></span></p>

                      </div>

                      <div>

                        <p>

                          <input id="check_51" type="checkbox" name="addonId" value="51" class="check_add"/>

                          Add + </p>

                      </div>

                    </div>

                    </label>

                  </div>

                </div>

                                    <div class="product-grid-item xs-50 sm-50 md-20 lg-20 xl-20 display-icon block-button">

                  <div class="product-thumb  product-wrapper">

                    <label for="check_52" style="margin:0px; padding:0px;">

                    <div class="product-details popbrtdr">

                      <div class="image"> <img alt="Price Surprise" class="lazy first-image"  width="100%" src="image/catalog/products/ET002.jpg" style="display: block;" title="6 Red Roses" > </div>

                      <div class="caption addcon">

                        <p class="small red">6 Red Roses..</p>

                        <p class="small">Rs. 199<span class="price-tax"></span></p>

                      </div>

                      <div>

                        <p>

                          <input id="check_52" type="checkbox" name="addonId" value="52" class="check_add"/>

                          Add + </p>

                      </div>

                    </div>

                    </label>

                  </div>

                </div>

            </div>

          </div>

        </div>

      </div>

      <div class="modal-footer">

        <button type="button" onclick="gotocart('index.php?route=checkout/cart')" class="btn btn-default" data-dismiss="modal" style="background-color:rgb(241, 196, 15);">No Thanks</button>


        <button type="button" class="btn btn-primary" onclick="AddToCartAddon()">Continue</button>

      </div>

    </div>

</div>

  </div>

  </div>

`

0 个答案:

没有答案