[英]Session not save after redirect in WordPress
I have created a template page in WordPress.我在 WordPress 中创建了一个模板页面。 I want save the value in the session by opening the template page link, but I do not get that value after being redirected to product page.
我想通过打开模板页面链接来保存 session 中的值,但是在重定向到产品页面后我没有得到该值。 So what am I doing wrong?
那么我做错了什么?
I'm trying in the code below.我正在尝试下面的代码。
This is template file where i set session!这是我设置会话的模板文件!
<?php
/**
* Template Name: HDFC Coupon
*/
$coupon_code = 'HDFC10EGMKKRYD';
$coupon = new WC_Coupon($coupon_code);
$product_ids = $coupon->get_product_ids();
$_SESSION['HDFC_COUPON_VALID'] = true;
if(count($product_ids) == 1){
$url = get_permalink(reset($product_ids));
}else{
$url = wc_get_cart_url();
}
wp_redirect($url);
exit();
This is function.php
code where i want to get session value, but i got black array of session!这是
function.php
代码,我想在其中获得 session 值,但我得到了黑色的会话数组!
<?php
session_start();
add_filter('woocommerce_get_price', 'woocommerce_get_price_fun', 10, 2);
function woocommerce_get_price_fun($price, $product){
// here it's blank when i print this : print_r($_SESSION)
if(is_product() && isset($_SESSION['HDFC_COUPON_VALID']) && $_SESSION['HDFC_COUPON_VALID']){
// sonthing which i want to do after session value get.
}
return $price;
}
it's working fine when i comment this line wp_redirect($url);
当我评论这行
wp_redirect($url);
, but when i use redirect then it not save data in session. ,但是当我使用重定向时,它不会将数据保存在 session 中。
WooCommerce has it's own session handler, which you can use by calling WC()->session
. WooCommerce 有自己的 session 处理程序,您可以通过调用
WC()->session
来使用它。 https://docs.woocommerce.com/wc-apidocs/class-WC_Session_Handler.html https://docs.woocommerce.com/wc-apidocs/class-WC_Session_Handler.html
It's possible that this is conflicting with session writes if you're using the $_SESSION
superglobal, particularly if you're calling exit
before the session has been written.如果您使用
$_SESSION
超全局变量,这可能与 session 写入冲突,特别是如果您在 session 写入之前调用exit
。
Instead, try using相反,请尝试使用
WC()->session->set('HDFC_COUPON_VALID', true);
and和
WC()->session->get('HDFC_COUPON_VALID');
followed by其次是
WC()->session->save_data();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.