Jump to content

Recommended Posts

Вариант 1

setcookie(session_name(),session_id(),strtotime('+1 year'),"/");

В документе index.php который лежит в корне сайта.

После строчки

session_start();

Соответственно, после этого корзина и все плюшки будут жить там год, даже после перезагрузки компа =)

 

Вариант 2 

Запись корзины в базу

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. В конце перед заключительным "}" вставить

public function cart_to_base()
{
$query = $this->db->placehold("DELETE FROM __carts WHERE code=?", $_COOKIE['shopping_cart_code']);
$this->db->query($query);
if($_SESSION['user_id']){
$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. в 2-х местах
после

// Если товар существует, добавим его в корзину
if(!empty($variant) && ($variant->stock>0) )
{
// Не дадим больше чем на складе
$amount = min($amount, $variant->stock);

$_SESSION['shopping_cart'][$variant_id] = intval($amount);

вставить

$this->cart_to_base();

2.3.
после

unset($_SESSION['shopping_cart'][$variant_id]);

вставить

$this->cart_to_base();

2.4.
после

unset($_SESSION['shopping_cart']);
unset($_SESSION['coupon_code']);

вставить

$this->cart_to_base();

2.5.
после

public function get_cart()
{
$cart = new stdClass();
$cart->purchases = array();
$cart->total_price = 0;
$cart->total_products = 0;
$cart->coupon = null;
$cart->discount = 0;
$cart->coupon_discount = 0;

вставить

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

3. view/LoginView.php - в 2-х местах

после

$_SESSION['user_id'] = $user_id;

вставить

$this->cart->base_to_cart($user_id);
$this->cart->cart_to_base();

4. view/RegisterView.php


после

$_SESSION['user_id'] = $user_id;

вставить

$this->cart->base_to_cart($user_id);
$this->cart->cart_to_base();

 

Автор: https://www.nulled.cc/members/451859/

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...