我正在使用CodeIgniter购物车,下面的代码在浏览器中显示了我的所有详细信息。
(只需共享逻辑部分)
<?php
foreach ($post as $row) {?>
<tr>
<td><?php echo $i++;?></td>
<td><?php echo $row->name;?></td>
<td><?php echo $row->price;?></td>
<td>
<?php if($row->is_approved==1){?><button type="button" name="renew" class="btn btn-success add_cart">Add to cart</button>
<?php }else{?>
<div class="activity_status">Not Approved</div>
<?php };?>
</td>
</tr>
<div id="primarycart_details"></div>
<?php}?>
我正在浏览器上得到输出
Id | Name | price | Action
1 | poimjh | 10 | Add to cart
2 | asdasd | 100 | Add to cart
3 | dgfdfw | 50 | Add to cart
4 | qwewqe | 100 | Not Approved
// and so on
现在,我的问题是,当我单击“添加到购物车”时,它会在购物车中添加产品详细信息,而且我还将按钮状态从Add to cart
更改为Remove
。现在,当我刷新页面时,我的按钮再次从Remove
变为Add to cart
。我不想多次添加同一产品。
Ajax
$(document).ready(function(){
$(document).on('click', '.add_cart', function() {
var product_name = $(this).closest('tr').find('.productname').text();
var product_id = $(this).closest('tr').find('.productid').val();
var product_price = $(this).closest('tr').find('.calActivitylPrice').text();
var quantity =1;
$.ajax({
url:"<?php echo base_url(); ?>Member_controller/addToCart",
method:"POST",
data:{product_id:product_id, product_name:product_name, product_price:product_price,quantity:quantity},
success:function(data)
{
var obj = JSON.parse(data);
//alert(data);
$('#changeToRemove').html('<button type="button" name="remove" class="btn btn-danger btn-xs remove_inventory" id="'+obj.removebtn+'">Remove</button>')
$('#primarycart_details').html(obj.subtotal);
}
});
});
});
删除代码
$(document).on('click', '.remove_inventory', function(){
var row_id = $(this).attr("id");
//alert(row_id);
if(confirm("Are you sure you want to remove this?"))
{
$.ajax({
url:"<?php echo base_url(); ?>Member_controller/Removecart",
method:"POST",
data:{row_id:row_id},
success:function(data)
{
alert("Product removed from Cart");
}
});
}
else
{
return false;
}
});
从控制器中的购物车中删除
public function Removecart(){
$row_id = $_POST["row_id"];
//echo $row_id;
$data = array(
'rowid' => $row_id,
'qty' => 0
);
//print_r($data);
$this->cart->update($data);
echo $this->viewCart();
}
添加到控制器中的购物车代码
public function addToCart(){
$data = array(
"id" => $_POST["product_id"],
"name" => $_POST["product_name"],
"qty" => $_POST["quantity"],
"price" => $_POST["product_price"]
);
//print_r($data);
$this->cart->insert($data); //return rowid
//echo $this->viewCart();
}
有人可以帮我解决这个问题吗?
答案 0 :(得分:1)
您可以在产品循环中设置条件,如下所示:
<?php
foreach ($post as $row) {?>
<tr>
<td><?php echo $i++;?></td>
<td><?php echo $row->name;?></td>
<td><?php echo $row->price;?></td>
<td>
<?php if($row->is_approved==1){
if (in_array($row->id, array_column($this->cart->contents(), 'id'))) {
$rowid = 0;
foreach ($this->cart->contents() as $product) {if ($product['id'] === $row->id) { $rowid = $product['rowid']; break;}}
?>
<button type="button" name="remove" class="btn btn-danger btn-xs remove_inventory" id="<?php echo $rowid; ?>">Remove</button>
<?php } else { ?>
<button type="button" name="renew" class="btn btn-success add_cart" data-productname="<?php echo $row->name; ?>" data-price="<?php echo $row->price;?>" data-productid="<?php echo $row->name_id; ?>" />Add to cart</button>
<?php } ?>
<?php }else{?>
<div class="activity_status">Not Approved</div>
<?php };?>
</td>
</tr>
<div id="primarycart_details"></div>
<?php
}
首先使用array_column($this->cart->contents(), 'id')
获取产品ID的数组列表,然后在其上应用in_array()
以检查当前购物车项目是否存在于产品列表中。
此行:foreach ($this->cart->contents() as $product) {if ($product['id'] === $row->id) { $rowid = $product['rowid']; break;}}
用于在每种产品上查询rowid
的值。