0
votes

I am customizing woocommerce wp right now and I have a problem.

How do I check woocommerce cart using jQuery? The reason why I need to do this is I want to make some animations for the site using jQuery when user clicks on the product add to cart button.

The scenario is like: A user adds a product and this site I'm developing only gives 1 products, meaning you cant buy more than (1) product at a time. So when a user is done clicking or buying a product and back to home/product page and clicks the add to cart button on the other product I want something to popup using jQuery. My problem is I don't know how to do it. Thank you in advance for your answers.

1

1 Answers

0
votes

modify your product or home page and add your on own button with onclick function with product id as a parameter. make an ajax call in that function .

function somefunction(productid) {

  $.ajax({
    type : "post",
  url: 'wwww.siteurl.com/wp-admin/admin-ajax.php',
  data: {
  action: 'myajax',pid:productid

  },
  success: function(data){
alert(data); // this is error message user will see.use javascript to make popup



  },
  error: function(MLHttpRequest, textStatus, errorThrown){
  alert(errorThrown);
  }
  });

}

in your function.php

function myajax() {
$product_id = $_POST['pid'];

        if(WC()->cart->cart_contents_count <1) {
    WC()->cart->add_to_cart( $product_id );
echo "success";
die();
}
else {
echo "Not more then one product can be added"; // this is the error will return to jquery function //

die();

}

}
add_action('wp_ajax_myajax', 'myajax');
add_action('wp_ajax_nopriv_myajax', 'myajax');

hope this help ...