1. SQL
create table s_carts (code varchar(255) not null, user_id int(11), cart text, primary key (user_id, code)) default charset=utf8
2. api/Cart.php
2.1. At the end before the final "}" insert
$query = $this->db->placehold("DELETE FROM __carts WHERE user_id=?", $_SESSION['user_id']);
$this->db->query($query);
}
if($_SESSION['shopping_cart']){
$query = $this->db->placehold("INSERT IGNORE INTO __carts SET code=?, user_id=?, cart=?", $_COOKIE['shopping_cart_code'], 0, serialize((array)$_SESSION['shopping_cart']));
$this->db->query($query);
if($_SESSION['user_id']){
$query = $this->db->placehold("INSERT IGNORE INTO __carts SET code=?, user_id=?, cart=?", '', $_SESSION['user_id'], serialize((array)$_SESSION['shopping_cart']));
$this->db->query($query);
}
}
}
public function base_to_cart($param, $add='max')
{
if(empty($param)) return;
// $this->db->query("SELECT cart FROM __carts WHERE code=? OR user_id=? LIMIT 1", $param, $param);
$this->db->query("SELECT cart FROM __carts WHERE code=? OR (user_id>0 AND user_id=?) LIMIT 1", $param, $param);
if($c=$this->db->result('cart'))
$bcs = unserialize($c);
if ($bcs){
if($add=='max'){
if(is_array($bcs))foreach ($bcs as $variant_id => $amount) {
if(isset($_SESSION['shopping_cart'][$variant_id]))
$_SESSION['shopping_cart'][$variant_id]=max($_SESSION['shopping_cart'][$variant_id], (int)$amount);
else
$_SESSION['shopping_cart'][$variant_id]=$amount;
}
}else
$_SESSION['shopping_cart']=$bcs;
}
$this->check_cart();
}
public function check_cart()
{
$sc=(array)$_SESSION['shopping_cart'];
$variant_ids=array_keys($sc);
if(empty($variant_ids))
return;
$_SESSION['shopping_cart']=array();
$variants=$this->variants->get_variants(array('id' => $variant_ids));
if(is_array($variants))foreach ($variants as $variant) {
$vids[]=$variant->id;
}
if(is_array($variants))foreach ($variants as $variant) {
$id=$variant->id;
if(!($sc[$id]>0))continue;
if($variant->infinity)
$_SESSION['shopping_cart'][$id]=$sc[$id];
elseif($sc[$id]<=$variant->stock)
$_SESSION['shopping_cart'][$id]=$sc[$id];
elseif($variant->stock>0)
$_SESSION['shopping_cart'][$id]=$variant->stock;
}
}
2.2. in 2 places
after
// If the product exists, we will add it to the cart
if(!empty($variant) && ($variant->stock>0))
{
// We will not give more than in stock
$amount = min($amount, $variant->stock);
$_SESSION['shopping_cart'][$variant_id] = intval($amount);
paste
$this->cart_to_base();
2.3.
after
unset($_SESSION['shopping_cart'][$variant_id]);
paste
if(!isset($_COOKIE['shopping_cart_code'])){
$sid=session_id();
$_COOKIE['shopping_cart_code']=$sid;
setcookie("shopping_cart", $sid, time()+365*24*3600, '/');
$this->cart->base_to_cart($_COOKIE['shopping_cart_code']);
}