Jump to content

How to set/get cookie with PHP and Smarty


Gerard
 Share

Recommended Posts

Hi all, 

I'll do a promotion on the site, then I created a landing page with his own controller, I modified the ProductController to check if the ID of the product it's new that you only can access through the landing, then, when you access the product, I created on controller a cookie that only permit buy the product if you put a correct VAT number on the checkout form. The problem comes when for example deletes from the cart the product. Then I created a conditional in JS that checks the ID of the product and if it's the promotional product I execute an AJAX that calls a .php for destroy the cookie that doesn't let you buy if you don't put the VAT number. 

To make it more schematic:

  1. A user of the promotion (Company) lands on the landing page
  2. The user clicks on the product, then is redirected to the product page with a parameter in the URL to confirm that it comes from the promo.
  3. The user adds the product to the cart and he goes to the checkout.
  4. We create the cookie of the promo.
  5. If the cookie of the promo exists, then, the user needs to fill the VAT Number field mandatory.
  6. When the user buys the product or removes the product from the summary of the cart, that cookie needs to be removed so that the user can buy other products without having to enter the VAT number

 

Since only this product will be valid for B2B, if you delete this product, the rest can be purchased without entering a VAT number.

 

I set the cookie like this:

$_SESSION['promotion']="promotional-product";
            
$cookie= new Cookie('promotion'); 
$cookie->setExpire(time() + 45 * 60);
$cookie->variable_name = 'promotional-product';
$cookie->write();

 

And to get the cookie on the destroy file I have the following:

<?php
include_once('./config/config.inc.php');
include_once('./config/settings.inc.php');
include_once('./classes/Cookie.php');


 
//to read
$cookie = new Cookie('promotion');
//echo $cookie->variable_name;

if (isset($_COOKIE[$cookie->variable_name])) {
    setcookie($cookie->variable_name, null, -1, '/');    
    unset($_COOKIE[$cookie->variable_name]);
}

If I access to destroy PHP file with the "echo" I see the name of the cookie correctly, so far so good, I get to read the cookie or maybe not, it's the first time that I do that.

And the AJAX that calls the destroy.php it's the following

/*...*/
var id2 = id.substr(0,2);
	$('#product_' + id).fadeOut('slow', function() {
		if(id2 == 21)
		{
			$.ajax({
			type: "GET",
			url: 'destroy.php',
			success: function(data){
			   console.log("Done");
			}
			});
		 }
      
    $(this).remove();
...

When I delete the product 21 from the cart I got the message "Done" in console correctly.

 

Now the issue is on Smarty, I can't get the cookie name, I have the following:

{if $cookie->promotion=='promotional-product'}

And below that, the HTML. I tried with: 

{$smarty.cookies.promotion}

and 

{$smarty.cookies.promotional-product}

just for curiosity and test if can get something, with the first I see nothing and with the second I see a number 0. 

 

Could you help me with that? Would I have to create the cookie differently for Smarty to read it?  Is there any other way to do this? 

Many thanks in advance!

Share this post


Link to post
Share on other sites

  • 1 year later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More