0

I have a basket cookie, before adding a product to the basket the function checks to see if a cookie exists or if a new cookie is required. This works fine when adding one item at a time but there are occasions when more than one item is added at the same time. Again if an old basket cookie exists this works fine. The problem is when no basket cookie exists and the function had to create one.

The first time it goes through, the cookie is created a database record is added ect.

The second time round the function we check for the cookie no cookie is found and another cookie is created etc.

  $this->db->select('basket_id');
  $this->db->from($this->basket_table);
  $this->db->where('basket_id', get_cookie('basket_id'));
  $check_basket = $this->db->get();

 if($check_basket->num_rows > 0){
   $basket_exists = 1;
 }else{
   $basket_exists = 0;
 }

 if($basket_exists == 0){
  delete_cookie('basket_id');

  $basket = array(
   'lang_id' => $lang_id,
   'currency_id'  => $currency_id,
   'customer_id'  => $customer_id,
  );

  $this->db->insert($this->basket_table, $basket);
  $basket_id =  $this->db->insert_id();;

  $cookie  = array(
   'name' => 'basket_id',
   'value' => $basket_id,
   'expire' => 60*60*24*30,
   'domain' => 'REMOVED'
   'path' => '/',
   'prefix' => '',
  );

  set_cookie($cookie);
}else{
  $basket_id = get_cookie('basket_id');
}
4

1 に答える 1

1

Cookieを設定するには、Cookieをブラウザに送信する必要がありますが、ビューを作成する前に関数が複数回ループした場合、これは発生しません。

したがって、バスケットを使用してCookieを事前に設定するか、次のようにCookieを一度だけ設定する必要があるかどうかを確認します。

  $this->db->select('basket_id');
  $this->db->from($this->basket_table);
  $this->db->where('basket_id', get_cookie('basket_id'));
  $check_basket = $this->db->get();

 if($check_basket->num_rows > 0) { 

  $basket = array(
   'lang_id' => $lang_id,
   'currency_id'  => $currency_id,
   'customer_id'  => $customer_id,
  );

  $this->db->insert($this->basket_table, $basket);
  $basket_id =  $this->db->insert_id();

  $cookie  = array(
   'name' => 'basket_id',
   'value' => $basket_id,
   'expire' => 60*60*24*30,
   'domain' => 'REMOVED'
   'path' => '/',
   'prefix' => '',
  );

  set_cookie($cookie);
}

// Now run your basket logic here - knowing the cookie is setup

}

于 2012-05-24T04:28:23.753 に答える