Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set cookie in Magento

Tags:

magento

i am trying to set a cookie for the users who signup with the newsletter pop in magento home page

i have a pop in magento homepage with a newsletter subscription option when user subscribes to the newsletter a cookie is set to that the newsletter will not show him on next visit

here is the code how am setting the cookie

<?php
$value=$_POST['newslettertext'];
setcookie("EmailCookie", $value);
setcookie("EmailCookie", $value , time()+86400,"/");  
function gotopage($url)
{
    echo "<script language=\"javascript\">";
    echo "window.location = '".$url."'; \n";
    echo "</script>";
}

$url="http://abc.com";
gotopage($url);

?>

the above code sets a coookie

after subscription the user redirects to the same page there i have check if cookie is set then the popup code executes otherwise there will be non popup

but its still showing the popup after subsscription

am using this code to check cookie

<?php 
if(!isset($_COOKIE['EmailCookie'] ) )
{
//popup code goes here
}
?>

where am doing wrong ?

like image 707
Manoj Kumar Avatar asked May 29 '12 05:05

Manoj Kumar


People also ask

How do I set cookies in Magento 2?

In this article we learn how to Set, get and delete data from cookie in Magento2. So first, Create a folder “Cookie” under app/code/Namespace/Module/ and create a file “Custom. php” under app/code/Namespace/Module/Cookie/. In the above code, I have created a function get() that is used to get data stored in cookie.

How are cookies used in Magento?

The Cookie Notice module allows selecting cookie notice display type and modifying the layout of the bar or the popup making them match the Magento theme. The extension ensures users that the Magento 2 store abides by the EU's cookie law and hence win their trust.

Does Magento 2 use cookies out of the box?

Magento 2 Default Cookies. The following cookies are used by Magento Commerce “out of the box” for on-premise and cloud installations. These cookies may be required by functionality that is explicitly requested by the customer. To learn about the lifetime of session cookies, see Session Lifetime.


1 Answers

require_once 'Mage.php';
Mage::app();
$cookie = Mage::getSingleton('core/cookie');
$cookie->set('cookiename', 'cookievalue' ,time()+86400,'/');

here is the answer

like image 166
Manoj Kumar Avatar answered Sep 30 '22 08:09

Manoj Kumar