繁体   English   中英

如何使用购物车会话Codeigniter删除项目

[英]How to remove item using shopping cart session Codeigniter

我目前正在开发一个电子商务系统,我已经在订购模块的一部分。 我正在使用Codeigniter的购物车,这是我第一次这样做。

完成添加到购物车的一部分,但在购物车会话中删除单个项目有问题。 当我点击删除时,我的购物车中的所有内容都将被删除。

问题:如何删除购物车中的单个商品?

视图

<?php foreach ($this->cart->contents() as $items): ?>
        <tr>
              <td><?= $items['name']?></td>
              <td><?= $items['qty']?></td>
              <td style="text-align:center"><span>&#8369;<?= $this->cart->format_number($items['price'])?></span></td>
              <td style="text-align:center"><span>&#8369;<?= $items['subtotal']?></span></td>
              <td><a href="<?= base_url().'user/remove_cart'?>"><button class="btn btn-primary btn-sm"><i class="fa fa-times" aria-hidden="true"><?= $this->cart->remove($items['rowid'])?>REMOVE</i></button></a></td> 
        </tr>
<?php endforeach; ?>

调节器

public function remove_cart($rowid)
    {
        $removed_cart = array(
            'rowid'         => $rowid,
            'qty'           => 0
        );
         $this->cart->update($removed_cart);
    }
}

您不应该使用Codeigniter中的购物车类。 它已被弃用。

你应该实现自己的。 我们使用Codeigniter作为我们的自定义电子商务应用程序,我可以告诉您使用更有意义。 我从未见过使用Codeigniter购物车类的任何意义。

它只不过是Session的包装。

此代码删除购物车项目

<td><a href="<?= base_url().'user/remove_cart'?>"><button class="btn btn-primary btn-sm"><i class="fa fa-times" aria-hidden="true"><?= $this->cart->remove($items['rowid'])?>REMOVE</i></button></a></td>

这部分

<?= $this->cart->remove($items['rowid'])?>

在这里它被删除

删除代码,你可以尝试这样。

<td>
      <a href="<?= base_url().'user/remove_cart/'.$items['rowid']; ?>">
          <button class="btn btn-primary btn-sm"><i class="fa fa-times" aria-hidden="true">REMOVE</i></button>
      </a>

通过单击按钮删除行的简单方法是使用JQuery
这是我的解决方案:

HTML,PHP:

<table>
      <tr>
        <td><!--your PHP code--></td>
        <td><!--your PHP code--></td>
        <td><span>&#8369;<!--your PHP code--></span></td>   
        <td><a href="<!--your PHP code-->"><button class="remove">REMOVE</button></a></td> 
      </tr>
      <tr>
        <td><!--your PHP code--></td>
        <td><!--your PHP code--></td>
        <td><span>&#8369;<!--your PHP code--></span></td>   
        <td><a href="<!--your PHP code-->"><button class="remove">REMOVE</button></a></td> 
     </tr>
</table>

JQuery的:

$(document).ready(function(){
    $('.remove').click(function(){
        $(this).closest('tr').remove();
    })
});

这是示例的链接: http//tpcg.io/J0Uv2H

试试这个:

function remove($id) {

        if ($id === "all"){
        $this->cart->destroy();
        }
        else{
        // Destroy selected rowid in session.
        $data = array(
        'rowid' => $this->input->post('id'),
        'qty' => 0
        );
        $this->cart->update($data);
        }
        $this->load->view('users/cart_function');
    }

check $ rowid有一个值。

暂无
暂无

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

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