0

I've found another questions regarding on how to set cookies on wp/wc but i cannot find how to properly get those cookies, i created a hook on the init function like this;

add_action('init', function () {
    $utm_source = get_request_parameter('utm_source');
    if (!empty($utm_source)) {
        wc_setcookie(DEFAULT_COOKIE_KEY, $utm_source, DEFAULT_COOKIE_EXPIRATION);
    }
});

And the cookie was set correctly (i saw it on the cookie options on my browser) but when i try to get this cookie value on my other filter the key is not present.

function my_custom_price($price, $product) {
    $cookie = isset($_COOKIE[DEFAULT_COOKIE_KEY]) ? $_COOKIE[DEFAULT_COOKIE_KEY] : "";
    // $cookie is always empty string
}
add_filter('woocommerce_product_get_price', 'my_custom_price', 10, 2);

Is this related to the order that filter and hooks are called? how can i set a cookie and get the value from another filter action?

2
  • According to definition, Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE or $HTTP_COOKIE_VARS arrays. Normally, it is not related to any hooks except that the cookie you are going to use is being tampered by other hooks before or after. You may var_dump($_COOKIE) to see what is inside at specific point for debugging. Apr 25, 2020 at 14:20
  • even realoding the page my key doesn't show on with var_dump, what i need is to get the utm_source from the query string and save it temporarily for the user, is there a better option than cookie? (transient doesn't do the job, i need to get the utm_source by "session")
    – RFL
    Apr 25, 2020 at 14:41

2 Answers 2

0

Because my reply is too long to be a comment, so I write it as suggested methods instead.

Method 1

In my project, I use ajax to set cookie with php built-in setcookie() function and cookie variable $_COOKIE, I did not add them in any hook and residing in its own class, any actions calling is calling the class and/or methods from other place. So the ajax call can fetch the cookie value any time because each call is a new page loading to ajax.

For ajax, you may refer to [this post] (How to get dynamically custom post type that are under a certain category) which I have created a sample of ajax. And you may refer to Ajax handbook

Method 2

You may consider to create 1 custom meta_key for user to store the temporary value of your choice, you may set a simple array or variable. Please note that the array need to serialize first before storing to MySQL database. The array structure is as simple as possible because it is easier to handle even it is serialized.

So you could use get_user_meta function to fetch the value

Method 3

If method 1 is working but not enough for page to page transition, you may combine method 1 and method 2 together. eg. You can fetch the value to the cookie from user meta if available.(In case any accidental events such as browser closing.) So that the user may still use a previous value without start over again.

Pros

  • can save the meta any time when you can get it
  • can make use of cookie as transition between pages
  • user meta can be a temporary storage provided for future use or other events which might be able to improve the user experience
1
  • Thank you for your suggestions
    – RFL
    Apr 28, 2020 at 0:58
0

The problem:

I need to get the values defined on the $_COOKIE when the page starts (init action), but the $_COOKIE is only available after the client refresh his page.

To do so i created a singleton class to hold the value between the init action and my custom filter action.

class GenericCookieHandler
{
    const DEFAULT_COOKIE_KEY = 'KEY';
    private static $instance;
    private $value;

    public static function getInstance()
    {
        if (self::$instance == null) {
            self::$instance = new self;
        }

        return self::$instance;
    }

    public function set($value, $expiration)
    {
        wc_setcookie(self::DEFAULT_COOKIE_KEY, $value, $expiration);
        $this->value = $value;
    }

    public function get()
    {
        if (!isset($_COOKIE[self::DEFAULT_COOKIE_KEY])) {
            return $this->value;
        }

        return $_COOKIE[self::DEFAULT_COOKIE_KEY];
    }
}

With this i can start the class on my init action then instantiate the class on my filter to retrieve the already defined values.

The init action:

add_action('init', function () {
    $cookieHandler = GenericCookieHandler::getInstance();
    $cookieHandler->set(
        'my_value',
        strtotime('+20 minutes')
    );
});

The filter action:

function my_custom_price_filter($price, $product){
    $handler = GenericCookieHandler::getInstance();
    echo $handler->get(); // my_value
}
add_filter('woocommerce_product_get_price', 'my_custom_price_filter', 10, 2);

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.