I an using CodeIgniter shopping cart. I am able to add the product in the cart and remove is also working.
Now, My issue is, Is it possible to remove the product using id
instated of rowid
.
Why I am doing with id
because I want to give the customer to add the same product only once. The same product can't add more than one time. So once the product is added in the cart then I am changing the button from add to cart
to remove
using jQuery. So this scenario is working for me but the issue is when I refresh the page then my button is showing "Add to cart" again.
So I tried to solve, if the product is in the cart and the user refreshes the page then still show the remove
button.
So I tried
if (in_array($activity_name_id, array_column($this->cart->contents(), 'id'))) {?>
<button type="button" name="remove" class="btn btn-danger btn-xs remove_inventory" id="1">Remove</button>
<?php }else{?>
<button type="button" name="renew" class="btn btn-success add_cart" data-productname="<?php echo $activity_name; ?>" data-price="" data-productid="<?php echo $activity_name_id;?>"/>Add to cart</button><?php }?>
I tried above code on view page, if the user refreshes the page and product in a cart then it will display the remove
button.
So everything is working but the issue is, if I click on remove
button then the product is not removing from the cart because I am calling the id
. Also how to display again add to cart
button after removing the g product.
that's the reason I want to remove the product from the cart using id
.
Let me know I am on the right path or not.
Remove from cart
$(document).on('click', '.remove_inventory', function(){
var row_id = $(this).attr("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");
$('#totalDetails').html(obj.cart_total);
$('#totalQty').html("Total items:"+obj.totalQty);
reloadCart();
}
});
}
else
{
return false;
}
});
Remove code
public function Removecart(){
$row_id = $_POST["row_id"];
$data = array(
'rowid' => $row_id,
'qty' => 0
);
$this->cart->update($data);
}
jQuery
$(document).ready(function(){
$('.add_cart').click(function(){
var product_name = $(this).data("productname");
var product_id = $(this).data("productid");
var product_price = $(this).closest('tr').find('.calActivitylPrice').text();
var quantity =1 //$('#clubmembershipcheckbox').val();
var changeToRemoveBTNSec = $(this).closest('tr').find('.display_table');
$.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);
$(changeToRemoveBTNSec).html('<button type="button" name="remove" class="btn btn-danger btn-xs remove_inventory" id="'+obj.removebtn+'">Remove</button>');
// $('#subtotal_details').html('Total cost: INR ' +obj.subtotal);
$('#totalDetails').html(obj.cart_total);
$('#totalQty').html(obj.totalQty);
}
});
});
});
Would you help me out in this issue?