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.
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>₱<?= $this->cart->format_number($items['price'])?></span></td>
<td style="text-align:center"><span>₱<?= $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. 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. I have never seen any point in using Codeigniter shoping cart class.
It is nothing more than wrapper around 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
This is my solution :
HTML , PHP :
<table>
<tr>
<td><!--your PHP code--></td>
<td><!--your PHP code--></td>
<td><span>₱<!--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>₱<!--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();
})
});
that is the link to the example : 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.