简体   繁体   English

如何将产品再次添加到购物车或“添加到购物车”按钮不起作用?

[英]How to add the product again to the cart or add to cart button not working?

<?php foreach ($memberData as $c) {?>
    <tr>
      <td><?php echo $c->name;?></td>
      <td> 1 year</td>
      <td><div class="calActivitylPrice"> <?php echo $c->Fees;?></div></td>
      <td>
          <div class="display_table"><?php if($c->member_approve==1){?>
          <button type="button" name="renew" class="btn btn-success add_cart" data-productname="membership" data-price="" data-productid="1" />Add to cart</button><?php }else{?><div class="redDot statusDot"></div><div class="activity_status">Not Approved</div><?php };?>
         </div>
      </td>

    </tr>
<?php }?>

The output of the above code is 上面代码的输出是

Alex | 1 Year | 500 | Add to cart
blex | 1 Year | 300 | Add to cart
clex | 1 Year | 200 | Add to cart

Now when the user clicks on Add to cart button then product will add in the cart and button will change from add to cart to remove using jQuery. 现在,当用户单击“ Add to cart按钮时,产品将添加到购物车中,并且按钮将从“ add to cart更改为使用jQuery remove below is the code of add to cart and change the button from add to cart to remove . 以下是添加到购物车并将按钮从add to cart更改为remove

Add to cart 添加到购物车

$(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 ;

  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 remove_inventory" id="'+obj.removebtn+'">Remove</button>');

     $('#totalDetails').html(obj.cart_total);          
     $('#totalQty').html(obj.totalQty);
    }
   });

 });

});

After adding the product in a cart the I am getting the remove button so my list is 将产品添加到购物车后,我会看到“删除”按钮,因此我的列表是

Alex | 1 Year | 500 | Add to cart
blex | 1 Year | 300 | remove
clex | 1 Year | 200 | remove

Now If I click on the remove button then the product is also removed from the cart and button change from remove to add to cart and the script is below 现在,如果我单击“删除”按钮,那么该产品也将从购物车中删除,并且按钮从“ remove更改为“ add to cart ,脚本如下

Remove from cart 从购物车中删除

 $(document).on('click', '.remove_inventory', function(){
  var row_id = $(this).attr("id");
  var changeToAddtocartBTN = $(this).closest('tr').find('.display_table');

  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)
    {
        var obj = JSON.parse(data);
        alert("Product removed from Cart");


      $(changeToAddtocartBTN).html('<button type="button" name="renew" class="btn btn-success add_cart" data-productname=cmembership" data-price="" data-productid="-3">Add to cart</button>');


    }
   });
  }
  else
  {
   return false;
  }
 });

Above scenario is working for me. 上面的情况对我有用。 now I am getting the issue on I am not able to add the product again because add to cart button is not working. 现在我遇到问题,因为无法add to cart按钮,所以无法再次add to cart产品。 because this time it's not getting the product name and all information. 因为这一次并没有获得产品名称和所有信息。

Would you help me out in this issue? 您能帮我解决这个问题吗?

Change your add to cart function from .click to .on 将添加到购物车功能从.click更改为.on

$(document).on('click', '.add_cart', 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;

    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 remove_inventory" id="' + obj.removebtn + '">Remove</button>');

            $('#totalDetails').html(obj.cart_total);
            $('#totalQty').html(obj.totalQty);
        }
    });

});

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

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