vendredi 31 août 2018

If product count reached, trigger alert. If product count already reached, don't trigger alert Woocommerce

I'm not sure if this is possible, or how to achieve this. I thought maybe a nested IF statement?

I am checking the product count in Woocommerce to check for a maximum of 4 items from a given category. I would like the IF statement to have logic as follows:

  • If the count becomes equal to 4, Trigger the javascript alert. (i.e. from 3 to 4)
  • If the count is already 4, do not trigger the javascript sweet alert if the user attempts to add another item to the cart. (This is where the js alert fires incorrectly)
  • If the count becomes 4 or more, then drops below 4 and then becomes equal to 4 again, Trigger the javascript alert.

Currently whats happening is that the alert is triggering successfully for when 3 items becomes 4 items in the cart. But then it triggers the javascript alert again when the user attempts to add another and added_to_cart event fires.

                    success: function (response) {
                    $.each( JSON.parse(response), function( category, count ) {
                        if( count == 4 ){ //IF STATEMENT HERE                         
swal({
  type: 'success',
  title: "You've Added The 4 Minimum "+category+" Items!",
  allowOutsideClick: false,
  showCancelButton: false,
  showConfirmButton: false,
timer: 3000,
})        
                        }

Aucun commentaire:

Enregistrer un commentaire