jQuery(document).ready(function($){

  $("button.single_add_to_cart_button").addClass('ahachat_add_user_ref');

  $("button.single_add_to_cart_button").click(function(e){

  //  e.preventDefault();

    var user_ref=$("#ahachat_random_val").val();

    var product_quantity=$('input.qty[name="quantity"]').val();

    var product_id=$('#ahachat_product_id').val();

    $("button.single_add_to_cart_button").attr("onclick",confirmOptIn(user_ref));

    $("button.single_add_to_cart_button").attr('user_ref',user_ref);

    $("button.single_add_to_cart_button").attr("data-quantity",product_quantity);

    $("button.single_add_to_cart_button").attr('data-product_id',product_id);

  });

  $(document).on("click","tr.woocommerce-cart-form__cart-item td.product-remove a.remove",function(e) {

    //e.preventDefault();

    var user_email = localStorage.user_email;

    var cookie_popup_br = localStorage.cb_cookie_popup;

    if(cookie_popup_br == undefined || cookie_popup_br == null) {

      cookie_popup_br = 0;

    }

    var cookie = localStorage.cb_cookie;

    if(cookie == undefined || cookie == null) {

      //CREATE COOKIE

      var day_current = new Date().getTime();

      localStorage.cb_cookie = day_current;

      cookie = localStorage.cb_cookie;

    }

    var product_id=$(this).data('product_id');

    var action_data = {

                        action:'ahachat_remove_product_cart',

                        product_id:product_id,

                        cookie_browser: cookie,

                        user_email:user_email,

                        cookie_popup:cookie_popup_br

                    };

      $.post(ajaxurl,action_data,function(result){

          console.log(result);

      });

  });

// Mini Cart

  $(document).on("click","li.woocommerce-mini-cart-item a.remove",function(e) {

    //e.preventDefault();

    var user_email = localStorage.user_email;

    var cookie_popup_br = localStorage.cb_cookie_popup;

    if(cookie_popup_br == undefined || cookie_popup_br == null) {

      cookie_popup_br = 0;

    }

    var product_id=$(this).data('product_id');

    var cookie = localStorage.cb_cookie;

    if(cookie == undefined || cookie == null) {

      //CREATE COOKIE

      var day_current = new Date().getTime();

      localStorage.cb_cookie = day_current;

      cookie = localStorage.cb_cookie;

    }

    var action_data = {

        action:'ahachat_remove_product_cart',

        product_id:product_id,

        cookie_browser: cookie,

        user_email:user_email,

        cookie_popup:cookie_popup_br

    };

    $.post(ajaxurl,action_data,function(result){

      console.log(result);

    });

   

  });



  // Update cart

  $(document).on("click",'[name="update_cart"]',function(e) {

    //e.preventDefault();

    var user_email = localStorage.user_email;

    var cookie_popup_br = localStorage.cb_cookie_popup;

    if(cookie_popup_br == undefined || cookie_popup_br == null) {

      cookie_popup_br = 0;

    }

    //

    var cookie = localStorage.cb_cookie;

    if(cookie == undefined || cookie == null) {

      //CREATE COOKIE

      var day_current = new Date().getTime();

      localStorage.cb_cookie = day_current;

      cookie = localStorage.cb_cookie;

    }

    var arr_your_cart = [];

    $('tr.woocommerce-cart-form__cart-item').each(function(index, value) {

          var product_id=$(this).find('td.product-remove a.remove').data('product_id');

          var product_quantity=$(this).find('td.product-quantity div.quantity input.qty').val();

          arr_your_cart.push({'product_id': product_id, 'quantity': product_quantity});

      });

     

      var action_data = {

        action:'ahachat_update_product_cart',

        arr_your_cart:arr_your_cart,

        cookie_browser:cookie,

        user_email:user_email,

        cookie_popup:cookie_popup_br

      };

      $.post(ajaxurl,action_data,function(result){

          console.log(result);

      });

    

  });

  //*******************SEND MESSENGER*************************/

});

                        