简体   繁体   English

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

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

I am currently developing an ecommerce system and I am already in the part of ordering module. 我目前正在开发一个电子商务系统,我已经在订购模块的一部分。 I am using the shopping cart of Codeigniter and it is my first to do it. 我正在使用Codeigniter的购物车,这是我第一次这样做。

Done in a part of add to cart but having problem in remove single item in cart session. 完成添加到购物车的一部分,但在购物车会话中删除单个项目有问题。 When I clicked the Remove, everything in my cart will be remove. 当我点击删除时,我的购物车中的所有内容都将被删除。

Question: How can I remove a single item in my cart? 问题:如何删除购物车中的单个商品?

View 视图

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

Controller 调节器

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

You should not use Shopping cart class from Codeigniter. 您不应该使用Codeigniter中的购物车类。 It has been deprecated. 它已被弃用。

You should implement your own. 你应该实现自己的。 We are using Codeigniter for our custom ecommerce app, and i can tell you that using makes much more sense. 我们使用Codeigniter作为我们的自定义电子商务应用程序,我可以告诉您使用更有意义。 I have never seen any point in using Codeigniter shoping cart class. 我从未见过使用Codeigniter购物车类的任何意义。

It is nothing more than wrapper around Session. 它只不过是Session的包装。

This code removes the cart items 此代码删除购物车项目

<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 part 这部分

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

Here it gets removed 在这里它被删除

remove the code and you can try like this. 删除代码,你可以尝试这样。

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

An easy way to remove a row by clicking a button is using JQuery 通过单击按钮删除行的简单方法是使用JQuery
This is my solution : 这是我的解决方案:

HTML , PHP : 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: JQuery的:

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

that is the link to the example : http://tpcg.io/J0Uv2H 这是示例的链接: http//tpcg.io/J0Uv2H

Try like this one: 试试这个:

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 has a value. check $ rowid有一个值。

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

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