-
-
Save mvnp/d368e5df3bcf6d1bdd59fab161aedb78 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Carrinho { | |
private $pref = 'media_'; | |
private function existe($id) { | |
if(!isset($_SESSION[$this->pref.'produto'])) { | |
$_SESSION[$this->pref.'produto']=array(); | |
} | |
if(!isset($_SESSION[$this->pref.'produto'][$id])) { | |
return false; | |
} else { | |
return true; | |
} | |
} | |
//verifica do produto e da session como um array | |
public function verificaAdiciona($id){ | |
if(!$this->existe($id)) { | |
$_SESSION[$this->pref.'produto'][$id]=1; | |
}else { | |
$_SESSION[$this->pref.'produto'][$id] +=1; | |
} | |
} | |
//verifica e adiciona mais um produto ao carrinho | |
private function prodExiste($id) { | |
if(isset($_SESSION[$this->pref.'produto'][$id])) { | |
return true; | |
}else { | |
return false; | |
} | |
} | |
//verifica se o produto existe | |
public function deletarProduto($id) { | |
if(!$this->prodExiste($id)) { | |
return false; | |
}else { | |
unset($_SESSION[$this->pref.'produto'][$id]); | |
return true; | |
} | |
} | |
//deleta produto do carrinho de compras | |
private function isArray($post) { | |
if(is_array($post)) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
//verifica se o post passsado por parâmetro é ou não um array | |
public function atualizarQuantidades($post) { | |
if($this->isArray($post)) { | |
foreach($post as $id => $qtd) { | |
$id=(int)$id; | |
$qtd=(int)$qtd; | |
if($qtd !='') { | |
$_SESSION[$this->pref.'produto'][$id]=$qtd; | |
} else { | |
unset($_SESSION[$this->pref.'produto'][$id]); | |
} | |
} | |
} | |
/*return true; | |
else { | |
return false; | |
}*/ | |
} | |
//deleta ou atualiza quantidades referente a um produto no nosso carrinho de compras | |
public function qtdProdutos() { | |
return count($_SESSION[$this->pref.'produto']); | |
} | |
} | |
$carrinho = new Carrinho; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment