2017-09-09 40 views
0

我花了幾個小時,嘗試了很多東西,但不起作用。 如在這張照片我修改Opencart的3 Add to basket如何爲每個選項逐一添加產品到購物車?

enter image description here

當前結果:

enter image description here

預期結果:
我想有產品 - 選項1 - 購物車中的每一行都有選項2。

enter image description here

我想一次添加的每個選項及其相關產品一個接一個,而不是一個產品和它的所有選項在一起。要做到這一點,我有這些代碼:

這是我複選框未渲染代碼:

<div class="checkbox"> 
    <label> 
     <input type="checkbox" 
        data-input-group="chart-group-{{ option.product_option_id }}-{{ option_value.product_option_value_id }}" 
        id="option-{{ option.product_option_id }}-{{ option_value.product_option_value_id }}" 
        name="option[{{ option.product_option_id }}][]" 
        value="{{ option_value.product_option_value_id }}" 
        class="chart-group" /> 
     {% if option_value.image %} <img src="{{ option_value.image }}" alt="{{ option_value.name }} {% if option_value.price %} {{ option_value.price_prefix }} {{ option_value.price }} {% endif %}" class="img-thumbnail" /> {% endif %}     
     {{ option_value.name }} 
    </label> 
</div> 

渲染JS和HTML:

$('#button-cart').on('click', function() { 
 
    $(".chart-group:input:checked").each(function() { 
 
     group_id = $(this).data('input-group'); 
 
     $('#input-quantity').val(
 
      $('input[name=quantity][data-input-group=' + group_id + ']').val() 
 
     ); 
 
     
 
     $.ajax({ 
 
      url: 'index.php?route=checkout/cart/add', 
 
      type: 'post', 
 
      data: $('#product input[type=\'text\'], #product input[type=\'hidden\'], #product input[type=\'radio\']:checked, #product input[type=\'checkbox\']:checked, #product select, #product textarea') 
 
     }); 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> 
 
\t <div class="form-group required "> 
 
\t \t <div id="input-option229"> 
 
\t \t \t <input type="hidden" name="product_id" value="53"> 
 
\t \t \t <table> 
 
\t \t \t \t <tr> 
 
\t \t \t \t \t <td><div class="checkbox"> 
 
\t \t \t \t \t \t \t <label> 
 
\t \t \t \t \t \t \t \t <input type="checkbox" data-input-group="chart-group-229-26" id="option-229-26" name="option[229][]" value="26" class="chart-group"> 
 
\t \t \t \t \t \t \t \t 70B </label> 
 
\t \t \t \t \t \t </div></td> 
 
\t \t \t \t \t <td><input type="text" data-input-group="chart-group-229-26" name="quantity" value="0" size="2" id="input-quantity-229-26" class="qty text-center"></td> 
 
\t \t \t \t </tr> 
 
\t \t \t \t <tr> 
 
\t \t \t \t \t <td><div class="checkbox"> 
 
\t \t \t \t \t \t \t <label> 
 
\t \t \t \t \t \t \t \t <input type="checkbox" data-input-group="chart-group-229-27" id="option-229-27" name="option[229][]" value="27" class="chart-group"> 
 
\t \t \t \t \t \t \t \t 75B </label> 
 
\t \t \t \t \t \t </div></td> 
 
\t \t \t \t \t <td><input type="text" data-input-group="chart-group-229-27" name="quantity" value="0" size="2" id="input-quantity-229-27" class="qty text-center"></td> 
 
\t \t \t \t </tr> 
 
\t \t \t \t <tr> 
 
\t \t \t \t \t <td><div class="checkbox"> 
 
\t \t \t \t \t \t \t <label> 
 
\t \t \t \t \t \t \t \t <input type="checkbox" data-input-group="chart-group-229-28" id="option-229-28" name="option[229][]" value="28" class="chart-group"> 
 
\t \t \t \t \t \t \t \t 80B </label> 
 
\t \t \t \t \t \t </div></td> 
 
\t \t \t \t \t <td><input type="text" data-input-group="chart-group-229-28" name="quantity" value="0" size="2" id="input-quantity-229-28" class="qty text-center"></td> 
 
\t \t \t \t </tr> 
 
\t \t \t </table> 
 
\t \t \t <button type="button" id="button-cart" data-loading-text="Loading..." class="button-cart btn btn-primary btn-sm btn-block"><i class="fa fa-cart-plus" aria-hidden="true"></i> Add to Cart</button> 
 
\t \t \t <input type="hidden" name="quantity" id="input-quantity" value="0"> 
 
\t \t </div> 
 
\t </div>

任何種類的幫助是非常不勝感激!

+0

嗨有點複雜,難於理解,如果你上了Ajax調用的響應,我們不知道你如何解析HTML如果是重定向或AJAX解析沒顯示任何成功的匿名函數,如果page.php index.php?route = checkout/cart/add將作業分割成兩個不同的產品,cos的大小不一樣,這要歸功於澄清這一點。問候 –

+0

@headmax我可以提供FTP信息,如果你有興趣。非常感謝提前! – Kardo

+0

好吧私人我在這裏是新的,所以如果你開車我會很好。 –

回答

0

固定!

$('#button-cart').on('click', function() { 

    $(".iqty:input:text").each(function() { 
    $('.chart-group:input:checkbox').prop("checked", false); 

    if ($(this).val() > 0) { 
     group_id = $(this).data('input-group'); 
     $('input[type=checkbox][data-input-group=' + group_id + ']').prop("checked", true); 
     $('#input-quantity').val(
      $('input[name=quantity][data-input-group=' + group_id + ']').val() 
     ); 

     $('input[name=quantity][data-input-group=' + group_id + ']').val(0) 
    } else { 
     $('#input-quantity').val(0); 
    } 

...

相關問題