0

I'm trying to conditionally set a cookie IF a certain query string parameter exists. My Class is an observer. The config.xml code:

<frontend>
    <events>
        <controller_action_predispatch>
            <observers>
                <my_module>
                    <class>my_module/observer</class>
                    <method>setCookie</method>
                </my_module>
            </observers>
        </controller_action_predispatch>
    </events>
</frontend>

The observer class (app/code/local/My/Module/Model/Observer.php):

class My_Module_Model_Observer
{
    public $_defaultParam = null;

    public function setCookie(Varien_Event_Observer $observer = null, $setCookie = true)
    {
        $myCookie = 'source';
        $myCookieVal = $this->getQueryString($myCookie);
        $myCookiePeriod = (int) 86400 * 30;
        $myCookieEnabled = Mage::getStoreConfig('my_module/my_group/source_cookie', Mage::app()->getStore());
        if ($setCookie) {
            if ($myCookieEnabled == 1 && $myCookieVal) {
                $doCookie = $this->setCookie($myCookie, $myCookieVal, $myCookiePeriod, false);
            } else {
                if ($this->getCookie($myCookie)) {
                    $doCookie = $this->delCookie($myCookie);
                }
            }
        } else {
            $doCookie = $this->getCookie($myCookie, true);
        }
        return $doCookie;
    }

    public function getQueryString($param = null)
    {
        $queryParam = Mage::app()->getRequest()->getParam($param, 'notset');
        $paramValue = $queryParam ? (string) $queryParam : $this->_defaultParam;
        var_dump('ParamValue: ' . $paramValue);
        return $paramValue;
    }

    public function setCookie($cookieName = null, $cookieVal = null, $cookiePeriod = 3600, $setEncode = true)
    {
        $model = Mage::getModel('core/cookie');
        if ($setEncode) {
            $cookieVal = base64_encode($cookieVal);
        }
        $curStore = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
        $urlPath = parse_url($curStore);
        $cookiePath = rtrim(str_replace('index.php', '', $urlPath['path']), '/');
        var_dump('CookieVal: ' . $cookieVal);
        $theCookie = $model->set($cookieName, $cookieVal, $cookiePeriod, $cookiePath);
        return $theCookie;
    }
}

The query string looks like this:

mydomain.com?some=rubbish&then=what&we=really&want=this&source=my

You will notice in the method getQueryString, I'm var_dump'ing the variable $paramValue. It' spits out string(14) "ParamValue: my".

Also, in the setCookie method, the var_dump spits out string(13) "CookieVal: my".

HOWEVER, the cookie is set with the value notset! That means the $param is null, yes? But the var_dumps tell me otherwise. Why is the correct value not set in the cookie?

I guess I've been staring at this too long and need some eyes; could be my own stupidity! Thanks for taking a look.

~ Edit ~

I also tried to loop through all the query string params by changing the getQueryString method to the following:

public function getQueryString($param = null)
{
    $queryParams = Mage::app()->getRequest()->getParams();
    foreach ($queryParams as $pid => $p) {
        $paramValue = ($pid == 'source') ? $p : $this->_defaultParam;
    }
    var_dump('ParamValue: ' . $paramValue);
    return $paramValue;
}

But the result is the same.

7
  • By chance is this a source click attribution for MyAffiliateProgram?
    – philwinkle
    Commented Jul 16, 2013 at 13:33
  • @philwinkle Well spotted, this is an affiliate program, but for another affiliate.
    – Jongosi
    Commented Jul 16, 2013 at 14:10
  • Have you looked into ecommerce.aheadworks.com/magento-extensions/… ?
    – philwinkle
    Commented Jul 16, 2013 at 14:11
  • 2
    I hope you are not expecting the cookie to be set at the same time as you are using var_dump() to output some data? The reason is that var_dump() triggers the headers to be sent, so no cookie header could be sent after that.
    – Vinai
    Commented Jul 16, 2013 at 14:21
  • 1
    I'm pretty sure your previous question I pointed this out, though not as concisely as Vinai, who is such a beauty :)
    – philwinkle
    Commented Jul 16, 2013 at 19:04

1 Answer 1

1

Solved; thanks to @vinai for pointing out the following:

Using var_dump will trigger the headers to be sent, so trying to set a cookie after that point will obviously fail.

This is not explicitly defined in the PHP manual, but if you think about it, it makes sense! They do say ...as with anything that outputs its result directly to the browser...

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.