0

I'm looking to set a cookie for one of my WordPress applications. Despite my research, I can't find why WordPress ignores the creation of the cookie. Here is my code place in my app. What do you think of my code? do they have errors? wordpress 6.0.1 php 8.0 https://lysto.fr/tools/?categorie=461 (ajouter un article pour declancher la creation du cookie)

Bonjour Je cherche à définir un cookie pour une de mes applications WordPress. Malgré mes recherches, je ne trouve pas pourquoi WordPress ignore la création du cookie. Voici mon code placer dans mon application. Que pensez-vous de mon code ? comporte t'ils des erreurs ? wordpress 6.0.1 php 8.0 https://lysto.fr/tools/?categorie=461 (add an article to trigger the creation of the cookie)

if (!isset ($_COOKIE['panier'])){
        function cookie_panier() {  
            $cookie_user_id = uniqid();
            setcookie( 'panier', $cookie_user_id, time()+60*60*24*12  );
        }
        add_action('init', 'cookie_panier');
    }

The site generates errors that are linked in the absence of this cookie Line 27 retrieves the value of the 'cart' cookie. The cookie does not exist, the browser displays me;

Warning: Undefined variable $panier_value in /homepages/44/d875690649/htdocs/tools/wp-content/plugins/catalogue/inc_data_panier.php on line 27

Do you think the following error can prevent the cookie from being created?

Warning: Cannot modify header information - headers already sent by (output started at /homepages/44/d875690649/htdocs/tools/wp-content/plugins/catalogue/panierBddCookie.php:17) in /homepages/44/d875690649/htdocs/tools/wp-content/plugins/catalogue/panierBddCookie.php on line 7

Le site génère des erreurs qui sont lier en l’absence de ce cookie La ligne 27 récupère la valeur du cookie ‘panier’. Le cookie n’existant pas, le navigateur m’affiche ; Warning: Undefined variable $panier_value in /homepages/44/d875690649/htdocs/tools/wp-content/plugins/catalogue/inc_data_panier.php on line 27

Pensez-vous que l’erreur suivante peut empêcher le cookie de ce créé ?

Warning: Cannot modify header information - headers already sent by (output started at /homepages/44/d875690649/htdocs/tools/wp-content/plugins/catalogue/panierBddCookie.php:17) in /homepages/44/d875690649/htdocs/tools/wp-content/plugins/catalogue/panierBddCookie.php on line 7

Here is the code of this file 'inc_data_panier.php'

<?php
function data_panier(){
    global $wpdb ;  
    $result=$wpdb->get_results("SELECT user_item_id, position, item_id, item_q, item_name, item_price FROM {$wpdb->prefix}gc_panier  where user_id = '".$_COOKIE['panier']."'ORDER BY position" ) ;
    foreach ($result as $row) {
        $user_item_id=$row->user_item_id;
        $position=$row->position;
        $panier_item_id=$row->item_id;
        $panier_item_q=$row->item_q;
        $panier_item_name=$row->item_name;
        $panier_item_price=$row->item_price;
        
        $panier_value[$panier_item_id]['user_item_id']=$user_item_id;
        $panier_value[$panier_item_id]['position']=$position;
        $panier_value[$panier_item_id]['id']=$panier_item_id;
        $panier_value[$panier_item_id]['q']=$panier_item_q;
        $panier_value[$panier_item_id]['name']=$panier_item_name;
        $panier_value[$panier_item_id]['price']=$panier_item_price;
    
        $result_item_ref_builder=$wpdb->get_results("SELECT item_ref_builder FROM {$wpdb->prefix}gc_articles where item_id LIKE '".$panier_item_id."%'") ;
        foreach ($result_item_ref_builder as $row) {
            $item_ref_builder=$row->item_ref_builder;
            $panier_value[$panier_item_id]['item_ref_builder']=$item_ref_builder;
            // $art[$item_id]['feature_sub_cathegorie_name'] =$item_ref_builder;
        }
    }
    return $panier_value;
}
    $panier_value=data_panier();    
        
            
?>

Here is the code of this file 'panierBddCookie.php'

<?php
        
    if (!isset ($_COOKIE['panier'])){
        function cookie_panier() {  
            $cookie_user_id = uniqid();
            setcookie( 'panier', $cookie_user_id, time() + DAY_IN_SECONDS, '/');
            echo "</br>Le cookie panier n'esiste pas</br>la valeur du cookie devrait être ".$cookie_user_id."</br>";
        }
        add_action('init', 'cookie_panier');
    }
    
if (isset($_POST['valid'])){
    $q=$_POST['quantite'];
    $item_id_panier=$_POST['item_id_panier'];
    $item_name_panier=$_POST['item_name_panier'];
    $item_price_panier=$_POST['item_price_panier'];
    $cookie_user_id=$_COOKIE['panier'];
    $user_item_id_panier = $cookie_user_id.$item_id_panier; 
        require('inc_data_panier.php');
            
        
        // si l'utlisateur est present dans la table panier
        if(isset($panier_value)){
            foreach($panier_value as $id => $value){
                
                // $position=$row->item_q;
                $position[]=$panier_value[$id]['position'];
            }
            $der_position=max($position)+1;
            foreach($panier_value as $id => $value){
                // echo $panier_value[$id]['name'];
                if($user_item_id_panier == $panier_value[$id]['user_item_id'])
                {
                    $new_quantite = $q+$panier_value[$id]['q'];
                    $wpdb -> update ( $wpdb->prefix.'gc_panier',
                                    array ( 'item_q' => $new_quantite ),
                                    array ( 'user_item_id' => $panier_value[$id]['user_item_id'] ),
                                    array ( '% d' )
                                    );
                    
                    
                }
                else{
                    // entrée dans la base de donne du panier si l'article n'a pas été ajouter au panier lors de la creation du cookie
                    $wpdb->insert($wpdb->prefix.'gc_panier',
                                    array(
                                        'user_item_id' => $user_item_id_panier,
                                        'position' => $der_position,
                                        'user_id' => $cookie_user_id,
                                        'item_id' => $item_id_panier,
                                        'item_q' => $q,
                                        'item_name' => $item_name_panier,
                                        'item_price' => $item_price_panier
                                    ),
                                    array(
                                        '%s',
                                        '%s',
                                        '%s',
                                        '%s',
                                        '%s',
                                        '%s',
                                        '%s'
                                    )
                                );
                    }
            }
        }
        // le panier est vide , 1er entre dans la table avec cette utilisateur
        else{
            $wpdb->insert($wpdb->prefix.'gc_panier',
                                    array(
                                        'user_item_id' => $user_item_id_panier,
                                        'position' => 1,
                                        'user_id' => $cookie_user_id,
                                        'item_id' => $item_id_panier,
                                        'item_q' => $q,
                                        'item_name' => $item_name_panier,
                                        'item_price' => $item_price_panier
                                    ),
                                    array(
                                        '%s',
                                        '%s',
                                        '%s',
                                        '%s',
                                        '%s',
                                        '%s'
                                    )
                                );
                }   
    }
        
            



// ?>
3
  • Undefined variable $panier_value has nothing to do with the code in the question, or the cookie. It says you're using a variable $panier_value that's undefined. This has nothing to do with the cookie. What is the code for inc_data_panier.php around line 27? Jul 21, 2022 at 10:40
  • Thanks for your help. I put the code at the bottom of the post. this function will search the database for the values ​​of the cookie. I also added the complete code of the first file.
    – Geo
    Jul 21, 2022 at 13:09
  • The headers already sent message is telling you that you're trying to create a header (probably your cookie) after output has already been sent to the browser. This could be any output -- it could be as small as a single space after a closing ?> tag in a PHP file. Once output begins, you can't send any more header data; cookies are headers, and so it's possible that this early output is causing your cookie creation to fail.
    – Pat J
    Jul 21, 2022 at 19:51

2 Answers 2

0

You are missing the path parameter. Try setcookie( 'panier', $cookie_user_id, time() + DAY_IN_SECONDS, '/');

Also, don't put the callback function in a conditional, put the conditional statement inside the function.

1
  • Thanks for your help. After testing, the cookie is still not created. I added a line to test the cookie value and the condition. lysto.fr/tools/?categorie=461 (add an article to trigger the creation of the cookie) Do you have an example for the condition in the function? I want to create the cookie only if it does not exist.
    – Geo
    Jul 21, 2022 at 12:48
0

I removed all the code except the cookie. The error still persist. To my surprise, the final php tags (?>) cause problems. My error was resolved by deleting these tags from the catalogue.php and panierBddCookie.php file. Not finding this logical, I deactivated the other extensions of the site. After deleting the "Orbit Fox by ThemeIsle" extension, the error reappeared. So I put an end tag back on the catalogue.php file to solve the problem. Thanks to Jacob Peattie, Pat and Paul for helping me find this complicated problem.

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.