Php 一个产品页面具有不同于其他页面的cookie
我想在用户打开产品页面时将产品id保存在数组中,然后将该数组保存在cookie中。这是我的代码:Php 一个产品页面具有不同于其他页面的cookie,php,wordpress,cookies,Php,Wordpress,Cookies,我想在用户打开产品页面时将产品id保存在数组中,然后将该数组保存在cookie中。这是我的代码: if(is_product()) { if(isset($_COOKIE['recent'])) { $data = unserialize($_COOKIE['recent']); $product_id = get_the_ID(); if(!in_array($product_id, $data)) {
if(is_product()) {
if(isset($_COOKIE['recent'])) {
$data = unserialize($_COOKIE['recent']);
$product_id = get_the_ID();
if(!in_array($product_id, $data)) {
array_unshift($data, $product_id);
if(count($data) > 8) {
array_pop($data);
}
setcookie('recent', serialize($data), time()+(3600*24*7), "/");
}
echo "SET";
}else{
$data = array();
$product_id = get_the_ID();
array_unshift($data, $product_id);
setcookie('recent', serialize($data), time()+(3600*24*7), "/");
echo "DIDNT SET";
}
print_r($data);
}
这些回音和打印用于调试目的。因此,如果它是一个产品页面,我检查是否已经设置了cookie,如果已经设置了cookie,那么我只想将产品ID添加到数组中(如果已经设置了),并更新该值。如果没有设置cookie,我将创建一个新数组,然后打印$data的内容
问题是,除了一个产品页面外,其他所有页面都可以正常工作。例如,我可以浏览产品,看到它一次又一次地打印出同一个数组,但是一个产品页面有自己的ID数组,它是一个元素的数组——该产品元素。该产品页面没有与其他页面不同的URL。相同模式:website.com/product/productname/
它似乎有自己的范围。即使很难,我也用“/”来表示域名,所以我想在所有网站上都应该是一样的
这里出了什么问题?如果没有看到您的输出,很难说。尝试在您提到的上下文中发布您的
$data
转储get\u\u ID()
是通过get\u post()
进行的代理,因此您可以尝试使用以下选项更可靠地设置ID。如果没有合适的对象,至少会看到错误
global $product;
$product_id = $product->id;
我在当地进行了测试,得到了类似的结果。标题中出现输出错误的原因是,您试图使用整数而不是字符串设置初始cookie。这和WP标头已初始化,但查询尚未完全处理 我制作了一个插件,它将运行在
template\u redirect
hook上,这将使调试更加容易。还有一个[最近查看过的]
短代码,可以让您对实际结果做一些事情
您可以将其放入主题中,或在插件文件夹中创建最近查看的.php:
问题不是我没有得到正确的身份证。身份证没有问题。但问题是,在同一域的不同页面中,cookie的内容并不相同。实际上,只有一页不同。但和其他人一样,它仍然是一个产品页面。因为wordpress使用模板文件,所以在所有产品页面中执行完全相同的代码。对不起,我一定是误解了。这确实很奇怪。那么产品在同一领域?你不是无意中导航到www/non-www或类似的东西吗?我的想法是,如果除了一个产品外,其他所有产品都获得了意外的输出,那么循环将在其他地方受到影响。如果是这种情况,请尝试向循环中添加
wp\u reset\u postdata()
。是的,地址中没有www以及其他任何内容。我在这里写的代码位于最顶端。上面的标记,因为如果我把它放在其他任何地方,我通常会得到“headers ready sent error”,所以它会在任何其他操作之前执行。这里有一个网站:在主页上,有很多产品。您可以单击其中任何一个,在内部页面的最顶部,在标题上方,您将看到您迄今为止访问过的产品ID数组,它们保存在您的cookie中。但在第一条生产线中排名第四的戴尔Inspiron。。。将有一个不同的ARR我想知道你是否得到这种意外的行为,由于格式错误的HTML。如果将鼠标悬停在产品标题上,可以看到href中有错误。未定义变量:wp content/themes/digiital/partials/newest.phpOkey中的productt我修复了该错误,但cookies也发生了同样的情况。非常感谢您的努力!)
/*
Plugin Name: Set Shop Cookie
Description: Adds a cookie for most recently viewed products
Version: 0.1
Author: Casey Lee
Author URL: https://simplethemes.com
*/
class SetWpShopCookie {
/**
* @var string API URL
*/
public static $instance;
public $recent_cookie;
public $cookie_name = 'recent';
public $recent_products;
/** Hook WordPress
* @return void
*/
public function __construct()
{
self::$instance = $this;
add_action( 'template_redirect', array( $this, 'get_recent_cookie') );
add_action( 'template_redirect', array( $this, 'set_post_cookie') );
add_shortcode( 'recently_viewed', array( $this, 'recent_products') );
}
/**
* Gets the 'recent' cookie
* @return array or null
*/
public function get_recent_cookie()
{
$recent_cookie = null;
$cookies = array();
foreach ( $_COOKIE as $name => $value ) {
$cookies[] = new WP_Http_Cookie( array( 'name' => $name, 'value' => $value ) );
}
foreach($cookies as $cookie) {
if ($this->cookie_name == $cookie->name) {
$recent_cookie = $cookie;
break;
}
}
$this->recent_cookie = $recent_cookie;
}
/**
* Load scripts when plugin is executed
* @see https://developer.wordpress.org/reference/classes/wp_http_cookie/
*/
public function set_post_cookie()
{
global $post;
$product_id = $post->ID;
$cookie_time = time() + 60 * 60 * 24 * 7;
// nothing to do here if we're not in a product single
if (!is_product())
return;
// create the cookie
if(!$this->recent_cookie) {
$data = array();
array_unshift($data, $product_id);
setcookie($this->cookie_name, serialize($data), $cookie_time, COOKIEPATH, COOKIE_DOMAIN);
// update existing cookie
} else {
$data = unserialize($this->recent_cookie->value);
if(!in_array($product_id, $data)) {
array_unshift($data, $product_id);
if(count($data) > 8) {
array_pop($data);
}
// Use WP globals for portable cookie settings
setcookie($this->cookie_name, serialize($data), $cookie_time, COOKIEPATH, COOKIE_DOMAIN);
}
}
// Debug
// var_dump($this->recent_cookie);
}
// Recently viewed products shortcode
function recent_products( $atts ) {
// Attributes
$atts = shortcode_atts(
array(
'count' => '5',
),
$atts,
'recently_viewed'
);
$product_data = unserialize($this->recent_cookie->value);
$str = '';
if ($product_data) {
foreach ($product_data as $products_viewed) {
$str .= $products_viewed .' ,';
}
return 'You recently viewed these products: ' . rtrim($str,',');
} else {
return 'Why don\'t you visit our shop?';
}
}
}
new SetWpShopCookie;