• Resolved rubenmgar

    (@rubenmgar)


    Hi @waltmesser and @danieliser, thank you for your help and sorry for the delay. My question was this:

    I’ve got a popup where users write a CP to see if they can buy in our store. In the confirmation I’ve a query to database, and in case they can buy a set a cookie by javascript, with CP value, and in case they can’t buy, I set this cookie value as NO. Then I refresh the page an show the user a message. The popup is controlled by a Session Cookie, pum-20.

    What I need is that if user enters a CP invalid, and then, click again the Buy button (which triggers the popup), the popup shows again. But as in first time, it created the pum-20 cookie, the popup doesn’t open again.

    So, how can I show the popup always, if you enter an invalid CP? Should I delete the cookie? How?

    I’ve try jQuery(‘#pum-20’).trigger(‘pumSetCookie’); with no luck. My code is this:

    add_filter('gform_confirmation_4', 'ch_courage_confirmation', 10, 4);
    function ch_courage_confirmation($confirmation, $form, $lead, $ajax) {
    	global $wpdb;
    	$user_count = $wpdb->get_var( "SELECT COUNT(*) FROM pz_z_cp WHERE cp_cp = '".$_POST['input_1']."'" ); //busca a la BBDD
    	
    	if (!session_id()) {
        	session_start();
    	}
    	
    	if($user_count==0) {
    		$confirmation = "You cannot buy";
    		
    		$confirmation .= "<script>
    		    jQuery('#popmake-20').trigger('popmakeSetCookie');
    		    document.cookie = 'PIZZA-CP=NO; path=/';
    		    setTimeout(function () {
    		        jQuery('#popmake-20').popmake('close');
    		    }, 500);
    		    window.location.href = window.location.href;
    		</script>";
    		$_SESSION['comprar'] = "No";
    	}else{
    		$botiga = $wpdb->get_row( "SELECT * FROM pz_z_cp, pz_z_botigues WHERE pz_z_cp.botiga_cp = pz_z_botigues.id_bg AND pz_z_cp.cp_cp = ".$_POST['input_1'] );
    		$confirmation = "Gracias! Te atenderá tu tienda más cercana. Por favor haz tu pedido.";	
    		$confirmation .= "<script>
    		    jQuery('#popmake-20').trigger('popmakeSetCookie');
    			document.cookie = 'PIZZA-CP=".$_POST['input_1']."; path=/';
    		    document.cookie = 'PIZZA-BOTIGA=".$botiga->name_bg."; path=/';
    		    document.cookie = 'PIZZA-EMAIL=".$botiga->email_bg."; path=/';
    			jQuery(‘#pum-20’).trigger(‘pumSetCookie’);
    		    setTimeout(function () {
    		        jQuery('#popmake-20').popmake('close');
    		    }, 500); 
    		    window.location.href = window.location.href;
    
    		</script>";
    
    		$_SESSION['comprar'] = "Si";
    	}
    	
    	// return the confirmation     
    	return $confirmation;
    }
    

    If I enter a wrong CP, the popup doesn’t show again. Thank you for your help!

Viewing 12 replies - 1 through 12 (of 12 total)
  • @rubenmgar

    Sorry you’re having difficulties, I’m sure we can this resolved.

    I’ll have our Lead Dev @danieliser take a look as soon as possible.

    Thank you for being patient.

    Thread Starter rubenmgar

    (@rubenmgar)

    @waltmesser, thank you for your help. I’ll be fantastic to have a solution in a few days, as I’ve to finish the project as soon as possible. I hope @danieliser can take a look.
    By the way, I’ve deleted “jQuery(‘#pum-20’).trigger(‘pumSetCookie’);” because, the popup doesn’t auto close, and if the user use the close button the cookie is not created. I’ll be great to be able to close the popup with the button, but have the cookie created by the code (in case a good CP is entered).

    As this is a new thread, I’ll explain again what I need, for new visitors. In my store you can only buy if you enter an accepted Postal Code, or if you are a registered user. When you want to go to a product, you see a button that says “Enter CP” (Postal Code). When you click a popup is opened. In this popup there are to forms: one for login, one for enter CP. The code I posted, is for the one for entering CP (Postal Code). If you enter an accepted CP, some code is executed, and some cookies are created. Then the same page is reloaded but now the button is “Add to cart”. If you click, you add to cart. Everything works if CP is accepted. But in case is not accepted, you can’t buy, and the button stills shows “Enter CP”, but the popup is not opened. What I need, or think that I need, is to be able to create or not Cookie that triggers popup open. In case you enter a bad CP, this cookie has not to be created. And, If you login, this cookies has to be created by the login form, wich is another thing that I don’t know how to do.

    Thank you @waltmesser anf @danieliser!

    Plugin Author Daniel Iser

    (@danieliser)

    @rubenmgar – Ok so first off jQuery(‘#pum-20’).trigger(‘pumSetCookie’); is the newer version of jQuery(‘#popmake-20’).trigger(‘popmakeSetCookie’);. Does the same thing but targets the overlay that contains the popup instead and uses our newer shorter prefixes.

    Ok moving on.

    I think you are nearly there, but confusing one factor. Once a cookie is set, no matter what the value (“No” included), a trigger will not fire.

    So if you want it to open again delete the pum-20 cookie entirely, if you don’t want it to show simply set that cookie.

    One thing I am not 100% of is that you can use cookies with click triggers, I see the option there just now, but having coded this 100% myself can’t recall that. If I left it in there it should work, not one to leave unfinished features laying around.

    Also you don’t need to set the cookie using our JavaScript functions, you can just as easily set it using PHP. Since the page is refreshing your PHP cookies will be detectable. As long as the name is pum-20 and it has a value other than null / false then our JS should detect it at runtime. The only note is that you need to make sure its not a private/secure cookie as JS cannot access those.

    Hope that helps.

    Thread Starter rubenmgar

    (@rubenmgar)

    Hi @danieliser! I’ve done some changes, and now situation is similar. First, I’ve update to 1.4.20. Then I’ve changed jQuery(‘#popmake-20’).trigger(‘popmakeSetCookie’); for jQuery(‘#pum-20’).trigger(‘pumSetCookie’);. When you write a enabled Postal Code, my code is more simple, and it seems to work. But how can I delete the cookie in case the Postal Code is wrong? By PHP with setcookie(“pum-20”, “”, time()-3600);? Or by JS? Because it seems that my code in $confirmation, is not working… Thank you for your help and for your work!

    add_filter('gform_confirmation_4', 'ch_courage_confirmation', 10, 4);
    function ch_courage_confirmation($confirmation, $form, $lead, $ajax) {
    	global $wpdb;
    	$user_count = $wpdb->get_var( "SELECT COUNT(*) FROM pz_z_cp WHERE cp_cp = '".$_POST['input_1']."'" ); //busca a la BBDD
    	
    	if (!session_id()) {
        	session_start();
    	}
    	if($user_count==0) {
    		$confirmation = "You cannot buy";
    		$confirmation .= "<script>
    			jQuery(‘#pum-20’).trigger(‘pumSetCookie’);
    		    document.cookie = 'PIZZA-CP=NO; path=/';
    		    setTimeout(function () {
    		        jQuery('#popmake-20').popmake('close');
    		    }, 500);
    		    window.location.href = window.location.href;
    		</script>";
    		
    		$_SESSION['comprar'] = "No";
    	}else{
    		$botiga = $wpdb->get_row( "SELECT * FROM pz_z_cp, pz_z_botigues WHERE pz_z_cp.botiga_cp = pz_z_botigues.id_bg AND pz_z_cp.cp_cp = ".$_POST['input_1'] );
    		setcookie("PIZZA-CP", $_POST['input_1'], 0, "/");
    		setcookie("PIZZA-BOTIGA", $botiga->name_bg, 0, "/");
    		setcookie("PIZZA-EMAIL", $botiga->email_bg, 0, "/");
    		$confirmation = "Gracias! Te atenderá tu tienda más cercana. Por favor haz tu pedido.";	
    		$_SESSION['comprar'] = "Si";
    	}
    	
    	// return the confirmation     
    	return $confirmation;
    }
    
    Plugin Author Daniel Iser

    (@danieliser)

    @rubenmgar – Via PHP I am not certain, I believe just setting an already expired time.

    Via JS we have the following helper function pm_remove_cookie(‘cookie-name’);

    Or if you need it scoped you can use jQuery.pm_remove_cookie(‘cookie-name’);

    Hope that helps.

    Thread Starter rubenmgar

    (@rubenmgar)

    Hi @danieliser, I’ve use your code with no success. In fact it seems that my javascript is not executed. My code

    //Resposta al submit del CP
    add_filter('gform_confirmation_1', 'ch_courage_confirmation', 10, 4);
    function ch_courage_confirmation($confirmation, $form, $lead, $ajax) {
    	global $wpdb;
    	$user_count = $wpdb->get_var( "SELECT COUNT(*) FROM pz_z_cp WHERE cp_cp = '".$_POST['input_1']."'" ); //busca a la BBDD
    	
    	if (!session_id()) {
        	session_start();
    	}
    	if($user_count==0) {
    		$confirmation = "You cannot buy";
    		$confirmation .= "<script>
    			jQuery.pm_remove_cookie(‘pum-20’);
    		    document.cookie = 'PIZZA-CP=NO; path=/';
    		    setTimeout(function () {
    		        jQuery('#popmake-1161').popmake('close');
    		    }, 500);
    		    window.location.href = window.location.href;
    		</script>";
    		$_SESSION['comprar'] = "No";
    	}else{
                   //you can buy
    		$botiga = $wpdb->get_row( "SELECT * FROM pz_z_cp, pz_z_botigues WHERE pz_z_cp.botiga_cp = pz_z_botigues.id_bg AND pz_z_cp.cp_cp = ".$_POST['input_1'] );
    		setcookie("PIZZA-CP", $_POST['input_1'], 0, "/");
    		setcookie("PIZZA-BOTIGA", $botiga->name_bg, 0, "/");
    		setcookie("PIZZA-EMAIL", $botiga->email_bg, 0, "/");
    		$confirmation = "Gracias! Te atenderá tu tienda más cercana. Por favor haz tu pedido.";	
    		$_SESSION['comprar'] = "Si";
    	}
    	
    	// return the confirmation     
    	return $confirmation;
    }

    Popup is not closed and cookie is not removed. @waltmesser, maybe now the problem is that code is not executed? Thanks!

    @rubenmgar

    I’ll have @danieliser take a look as soon as possible. Thank you for being patient.

    Thread Starter rubenmgar

    (@rubenmgar)

    Hi @waltmesser, any news?

    Thanks!

    @rubenmgar

    I’ve PM’ed Daniel, I’ll try again.

    Thank you for being patient.

    Plugin Author Daniel Iser

    (@danieliser)

    @rubenmgar – Sorry about the delay.

    Couple things to try:

    Change <script> to <script type="text/javascript">

    Second, why do it via JS. In your case if you can set cookies in PHP you should just set the pum-# cookie the same way, just pass a value of true and give it a valid expiration and path of / and you should be set. Likley a much simpler approach given your using a PHP filter rather than just inserting a script tag into the confirmation message via form settings.

    Hope that helps, let me know where I can test it in action if there is still an issue.

    Thread Starter rubenmgar

    (@rubenmgar)

    Thank you both @danieliser and @waltmesser! I’ll try this and tell you something.

    @rubenmgar

    I’m closing this ticket because it’s like 10 pages deep in the forum 😐

    When something develops, let us know with a new ticket, and link this thread in reference.

    Thanks so much!

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Force popup to open again’ is closed to new replies.