Search code examples
phpcart

Add quantity of goods in cart


I want to add possibility to change quantity of goods in cart but I don't have idea how to do this. I tried to write function but it just deleted current session. I want to do this without javascript, only php. Quantity should be saving in current session..

<?php

require_once "db.php";

$quantity = 1;
if ( isset($_GET['delete_id']) && isset($_SESSION['cart_list']) ) {
    $_SESSION['cart_list'] = array_filter($_SESSION['cart_list'], function($v) {
       return $v['id'] != $_GET['delete_id'];
    });
}

if ( isset($_GET['single_id']) && !empty($_GET['single_id']) ) {
    $current_added_good = get_single_by_id($_GET['single_id']);
    if ( !empty($current_added_good) ) {
        if ( !isset($_SESSION['cart_list'])) {
            $_SESSION['cart_list'][] = $current_added_good;
        }
        $single_check = false;
        if ( isset($_SESSION['cart_list']) ) {
            foreach ($_SESSION['cart_list'] as $value) {
                if ( $value['id'] == $current_added_good['id'] ) {
                    $single_check = true;
                }
            }
        }
        if ( !$single_check ) {
            $_SESSION['cart_list'][] = $current_added_good;
        }
    } else {
    }
}
?> 
<?php if ( isset($_SESSION['cart_list']) && count($_SESSION['cart_list']) !=0 ) : ?>
    <br>
    <?php $s = 0; ?>
    <?php foreach( $_SESSION['cart_list'] as $single ) : ?>
        <h4 class='font-weight-bold blue-text'>
            <strong><?php echo $single['price'];?>$</strong>
        </h4>
        <a class="plus">
            <i class="fas fa-plus" style='color: Blue'></i>
        </a>
        <br>
        <div class="cart_quantity"><?php echo $quantity;?></div>
        <a class="">
            <i class="fas fa-minus" style='color: Blue'></i>
        </a>
        <br>
        <hr>
        <a href="cart.php?delete_id=<?php echo $single['id'];?>">
            <i class='fas fa-times' style='color: Red'></i>
        </a>
        <hr>
        <section class='text-center mb-4 font-weight-bold display-4'>
            <label class="count" id="summ"><?php echo $s; ?>$</label>
        </section>

Solution

  • Doing it without JavaScript means resubmitting the page every time you change the quantity, so you'd need a form, so I'd guess something like this:

    <?php foreach( $_SESSION['cart_list'] as $key => $item ) : ?>
        <form action="<placeholder>" method="post">
            <input type="hidden" name="key" value="<?php $key ?>">
            <input type="submit" value="+" name="add">
            <input type="submit" value="-" name="substract">
            <input type="submit" value="x" name="remove">
        </form>
    <?php endforeach; ?>
    
    if (array_key_exists('add', $_POST)) {
       $_SESSION['cart_list'][$_POST['key']]['quantity'] += 1;
    } elseif (array_key_exists('substract', $_POST) && $_SESSION['cart_list'][$_POST['key']]['quantity'] > 0) {
       // only substract items if quantity is above 0
       $_SESSION['cart_list'][$_POST['key']]['quantity'] -= 1;
    } elseif (array_key_exists('remove', $_POST)) {
       unset($_SESSION['cart_list'][$_POST['key']]);
    }
    // redirect to cart view
    

    I don't recommend it though. This sort of dynamic functionality is what javascript is for.

    If you'd rather check against a value instead of a key, or make the buttons prettier, you could replace the submit inputs with buttons.

    <!-- $_POST['action'] will be equal to 'add' -->
    <button type="submit" name="action" value="add">
        <i class="fa fa-plus"></i>
    </button>
    <!-- $_POST['action'] will be equal to 'substract' -->
    <button type="submit" name="action" value="substract">
        <i class="fa fa-minus"></i>
    </button>
    <!-- $_POST['action'] will be equal to 'remove' -->
    <button type="submit" name="action" value="remove">
        <i class="fa fa-times"></i>
    </button>
    
    switch($_POST['action'] ?? null) {
        case 'add':
            $_SESSION['cart_list'][$_POST['key']]['quantity'] += 1;
            break;
        case 'substract':
            if ($_SESSION['cart_list'][$_POST['key']]['quantity'] > 0) {
                $_SESSION['cart_list'][$_POST['key']]['quantity'] -= 1;
            }
            break;
        case 'remove':
            unset($_SESSION['cart_list'][$_POST['key']]);
            break;
        default:
            // action is none of the above
    }
    // redirect ...