【问题标题】:One product page has different cookie from others一个产品页面的 cookie 与其他页面不同
【发布时间】:2017-04-11 06:37:44
【问题描述】:

我想在用户打开产品页面时将产品 ID 保存在数组中,然后将该数组保存在 cookie 中。这是我的代码:

   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);
    }

那些 echo 和 print_r 用于调试目的。因此,如果它是一个产品页面,我检查是否已经设置了 cookie,如果是,那么我只想将产品 ID 添加到数组中,如果它已经存在并更新值。如果未设置 cookie,我将创建一个新数组,然后打印 $data 的内容。

问题是,除了一个产品页面之外,所有这些都可以正常工作。例如,我可以浏览产品并看到它一遍又一遍地打印出相同的数组,但是一个产品页面有自己的 ID 数组,它是一个元素的数组 - 该产品元素。该产品页面与其他产品页面没有不同的 URL。相同的模式:website.com/product/productname/

它似乎有自己的范围。即使很难我使用“/”作为域,所以我想它应该在所有网站上都一样?

这里有什么问题?

【问题讨论】:

    标签: php wordpress cookies


    【解决方案1】:

    我在本地对此进行了测试,结果相似。您在标头中收到输出错误的原因是因为您试图使用整数而不是字符串设置初始 cookie。这和 WP 标头已初始化,但查询尚未完全处理。

    我制作了一个可以在template_redirect 钩子上运行的插件,它可以让您更轻松地进行调试。还有一个 [recently_viewed] 短代码可以让您根据实际结果做一些事情。

    你可以把它放到你的主题中或者在你的插件文件夹中创建一个recently_viewed.php:

    https://gist.github.com/simplethemes/4f9b6f969bcc0a0a6668aeed75491a15

    /*
    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;
    

    【讨论】:

    • 非常感谢您的努力! :)
    【解决方案2】:

    没有看到你的输出很难说。尝试在您提到的上下文中发布您的$data 转储。 get_the_ID() 是通过get_post() 的代理,因此您可以尝试通过以下方式更可靠地设置 ID。如果你没有合适的对象,至少你会看到一个错误。

    global $product;
    $product_id = $product->id;
    

    【讨论】:

    • 问题不在于我没有得到正确的 id。身份证没问题。但问题是cookie的内容在同一个域的不同页面中是不一样的。实际上,只有一页不同。但它仍然是和其他人一样的产品页面。因为 wordpress 使用模板文件,所以在所有产品页面中执行完全相同的代码。
    • 对不起,我一定是误会了。这确实很奇怪。所以产品在同一个域上?您不会不小心导航到 www/non-www 或类似的东西吗?我的想法是,如果您在除一种产品之外的所有产品上都获得了意外的输出,那么循环会在其他地方受到影响。如果是这种情况,请尝试将wp_reset_postdata() 添加到您的循环中以确保。
    • 是的,地址没有 www 等等。我在这里写的这段代码在最上面。上面的 标记,因为如果我把它放在其他任何地方,我曾经得到“标头已发送错误”,所以它在任何其他操作之前执行。这里是一个网站:divitech.digiiital.com 在首页,有很多产品。您可以单击其中任何一个,在内部页面的最顶部,标题上方,您将看到您迄今为止访问过的产品 ID 数组,这些 ID 保存在您的 cookie 中。但是在第一行中排名第四的产品称为 Dell Inspiron... 会有一个不同的 arr
    • 我想知道您是否会因为 HTML 格式错误而出现这种意外行为。如果您将鼠标悬停在产品标题上,您会看到 href 中有错误。未定义变量:wp-content/themes/DIGIIITAL/partials/newest.php中的productt
    • 好吧,我修复了这个错误,但同样的事情发生在 cookie 上。
    猜你喜欢
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-11
    • 2014-04-10
    • 2018-02-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多