V
V
Vlad Bazarov2015-06-16 16:37:04
PHP
Vlad Bazarov, 2015-06-16 16:37:04

How to solve the cart problem in PHP?

Good day to all!
Without prelude: there is a site loveyou.ua (not advertising). The site is on the OpenCart 2.0 engine, but the cart has been modified. Everything seemed to work flawlessly, but today I noticed a serious bug - when you add two or more products to the cart and change the quantity of the second , third, etc. product, the quantity of the first product changes. So it's confusing.
Here is the code itself:

<input type="text" name="quantity[<?php echo $product['key']; ?>]" value="<?php echo $product['quantity']; ?>"  size="1" class="form-cart" style="display:none;" id="quan-txt" />

            <select value="<?php echo $product['quantity']; ?>" onchange="vvv = $(this).val(); $('#quan-txt').val(vvv); $('#cart-form2').submit(); " >
      
                  <?php
                    for($i=1; $i<=20; $i++) {
                      if ($i == $product['quantity']) {
                        echo "<option value='$i' selected>$i шт</option>";
                      } else {
                        echo "<option value='$i'>$i шт</option>";
                      }
                    }
                  ?>
      
            </select>

As you can see, the onchange property has been used.
Any guesses?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander N++, 2015-06-16
@vlad1337

Option 1

<input type="text" name="quantity[<?php echo $product['key']; ?>]" value="<?php echo $product['quantity']; ?>"  size="1" class="form-cart" style="display:none;">
<select class="countOrders" value="<?php echo $product['quantity']; ?>" >
  <?php for($i=1; $i<=20; $i++):?>
      <option value="<?=$i?>" <?=($i == $product['quantity'] ? ' selected="selected"' : '' )?>><?=$i?> шт</option>
     <?php endfor;?>
</select>
<span class="input-group-btn">
  <button type="button" data-toggle="tooltip" class="btn btn-danger"  onclick="cart.remove('<?php echo $product['key']; ?>');"><i class="fa fa-times-circle"></i></button>
</span>

Гдето внизу подключить
<script>
$( document ).ready(function() {
  $('.countOrders').on('change', function(e){
    $(this).prev().val( $(this).val() );
    $('#cart-form2').submit();
  })
});
</script>

Option 2
<input type="text" name="quantity[<?php echo $product['key']; ?>]" value="<?php echo $product['quantity']; ?>"  size="1" class="form-cart" style="display:none;"/>
<select class="countOrders" value="<?php echo $product['quantity']; ?>"  onchange="$(this).prev().val( $(this).val()); $('#cart-form2').submit(); " >

  <?php for($i=1; $i<=20; $i++):?>
      <option value="<?=$i?>" <?=($i == $product['quantity'] ? ' selected="selected"' : '' )?>><?=$i?> шт</option>
     <?php endfor;?>
</select>
<span class="input-group-btn">
  <button type="button" data-toggle="tooltip" class="btn btn-danger"  onclick="cart.remove('<?php echo $product['key']; ?>');"><i class="fa fa-times-circle"></i></button>
</span>
<code>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question