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?
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.var_dump
, what i need is to get theutm_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")