Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
<pre lang="PHP"><pre>  $cart_id = null;
  $cart_version = 1;
  if(isset($_COOKIE["cartid"])){
    $cart_id = $_COOKIE["cartid"];
      $cart_version = $_COOKIE["cart_version"];
  } else {
      $curl = curl_init();
    
    curl_setopt_array($curl, array(
      CURLOPT_URL => 'https://api.australia-southeast1.gcp.commercetools.com/commerce-tool-project/carts',
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => '',
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 0,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => 'POST',
      CURLOPT_POSTFIELDS =>'{
      "currency" : "EUR",
      "customerId" : "94fdb639-4f68-41b1-a886-cd75b5c3ab8b"
    }',
      CURLOPT_HTTPHEADER => array(
        'Content-Type: application/json',
        'Authorization: Bearer q2fSUOooANKEYuuHURXW82R0xs88CX4c'
      ),
    ));
    
    $response = curl_exec($curl);
    
    curl_close($curl);
    //echo $response;
    $datas = json_decode($response , true);
//echo '<pre>';
//print_r ($datas);
    echo $cart_id = $datas['id'];
    echo $cart_version = $datas['version'];
    setcookie("cartid", $cart_id, time() + 3600, '/');
    setcookie("cartversion", $cart_version, time() + 3600, '/');
    $cart_id = $_COOKIE["cartid"];
    $cart_version = $_COOKIE["cartversion"];
    echo "qwowe";
  }



Error: Undefined index: cartid and
Undefined index: cartversion


What I have tried:

Tried adding the expire time and '/' in setcookie but not able to do it.
Posted
Updated 14-Sep-21 2:00am
Comments
Member 15329613 14-Sep-21 7:45am    
That means that cartid and cartversion do not have the values you think they have.
Priyanshu Sharma 2021 15-Sep-21 3:25am    
i have print both and they has the value

1 solution

You cannot refer to cookies immediately after setting them like that; see PHP: setcookie - Manual[^].
 
Share this answer
 
Comments
Priyanshu Sharma 2021 15-Sep-21 0:59am    
so any alternative way I can do this
Richard MacCutchan 15-Sep-21 3:47am    
Study the documentation and do it correctly, as explained. If you want your product to work then you must follow the rules.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900