简体   繁体   English

Codeigniter:购物车删除不起作用

[英]Codeigniter : cart remove not working

i am trying to remove the item from the cart this is my code : 我正在尝试从购物车中删除商品,这是我的代码:

 function remove($rowid) {
                 if ($rowid==="all"){
                    $this->cart->destroy();
                }
                else{
                    $data = array(
                    'rowid' => $rowid,
                    'qty' => 0
                    );
                    $this->cart->update($data); 
                 }
            redirect(base_url()."cart/viewcart");
         }

in my cart view, this is my code 在我的购物车视图中,这是我的代码

echo form_open('cart/update_cart');
$grand_total = 0;
$i = 1;

foreach ($cart as $item):
echo form_hidden('cart[' . $item['id'] . '][id]', $item['id']);
echo form_hidden('cart[' . $item['id'] . '][rowid]', $item['rowid']);
echo form_hidden('cart[' . $item['id'] . '][name]', $item['name']);
echo form_hidden('cart[' . $item['id'] . '][price]', $item['price']);
echo form_hidden('cart[' . $item['id'] . '][qty]', $item['qty']);
?>
<tr>
<td>
<?php echo $i++; ?>
</td>
<td>
<?php echo $item['name']; ?>
</td>
<td>
 <?php echo number_format($item['price'], 2); ?>
</td>
<td>
<?php

 $numberfield = array( 'type' => 'number', 'class' => 'form-control qty ' );
 echo form_input('cart[' . $item['id'] . '][qty]', $item['qty'], 'maxlength="3" size="1" style="text-align: right"',$numberfield); ?>
</td>
<?php $grand_total = $grand_total + $item['subtotal']; ?>
<td>
AED <?php echo number_format($item['subtotal'], 2) ?>
</td>
<td>

<?php
// cancle image.
$url = base_url().'assets/img/cross.png';
$path ="<img src='$url' width='20' height='20'>";
echo anchor(base_url().'cart/remove/' . $item['rowid'], $path); ?>
</td>
<?php endforeach; ?>

problem is that cart item does not get removed from the cart 问题是购物车中的物品没有从购物车中移除

i tried to destroy the cart using below code but it did not work 我试图使用下面的代码销毁购物车,但没有成功

   $this->cart->destroy();
in view section
  <?php 
                            if(!empty($this->cart->contents())){
                           foreach($this->cart->contents() as $items)
  {?>

   <tr id="tr'.$items["id"].'"> 
    <td><?=$items["name"];?></td>
    <td><?=$items["subject"];?></td>
    <td><?=$items["price"];?></td>
    <td><?=$items["qty"];?></td>
    <td><button type="button" name="remove" class="btn btn-danger btn-xs remove_inventory" id="'.$items["rowid"].'">Remove</button></td>
   </tr>

  <?php }}else{?>
      <tr> 
    <td colspan="5" style="
    text-align: center;
">No Data</td>
   </tr>

 <?php  }
  ?>
 <script>
 $(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?"))
      {
          $('#sspopupcart').modal('hide');
       $.ajax({
        url:"<?php echo base_url(); ?>Assignment_page/remove",
        method:"POST",
        data:{row_id:row_id},
        success:function(data)
        {
            //alert(data);
         //alert("Product removed from Cart");
         //$('#tbl1').html(data);
         $('#result_id').html(data);
            $('#sspopupcart').modal('show'); 
        }
       });
      }
      else
      {
       return false;
      }

 });
</script>
    public function remove()
 {
  //$this->load->library("cart");
  $row_id = $_POST["row_id"];
  $data = array(
   'rowid'  => $row_id,
   'qty'  => 0
  );
  //var_dump($data);
  $this->cart->update($data);
  echo $this->view();
  //echo $row_id;
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM