不允许同一产品超过一次性 CodeIgniter 购物车
Does not allow same product more than a one-time CodeIgniter shopping cart
我正在使用 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();
}
谁能帮我解决这个问题?
您可以像这样在产品循环中设置条件:
<?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
价值。
我正在使用 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();
}
谁能帮我解决这个问题?
您可以像这样在产品循环中设置条件:
<?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
价值。