添加购物车中的商品数量

时间:2021-02-15 23:27:11

标签: php cart

我想添加更改购物车中商品数量的可能性,但我不知道该怎么做。我尝试编写函数,但它只是删除了当前会话。 我想在没有 javascript 的情况下做到这一点,只有 php。数量应该在当前会话中保存..

<?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>

1 个答案:

答案 0 :(得分:0)

在没有 JavaScript 的情况下进行意味着每次更改数量时都重新提交页面,因此您需要一个表单,所以我猜是这样的:

<?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

我不推荐它。这种动态功能正是 javascript 的用途。

如果您更喜欢检查一个值而不是一个键,或者让按钮更漂亮,您可以用按钮替换提交输入。

<!-- $_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 ...
相关问题