Last active
July 28, 2017 19:08
-
-
Save tadeubdev/d182c9336fd4271a0427 to your computer and use it in GitHub Desktop.
Carrinho de compras em PHP para responder em: http://forum.imasters.com.br/topic/525247-carrinho-de-compras-php/
This file contains 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 Cart | |
{ | |
public $CookieName = 'carrinho'; | |
public function __construct() { | |
if(!$this->getCookie()) { | |
return; | |
} | |
$Products = array(); | |
foreach($this->getCookie() as $Key => $Value) { | |
if($Value['product_amount']!==0) { | |
$Products[$Key] = $Value; | |
} | |
} | |
$this->setCookie($Products); | |
} | |
/** | |
* | |
* string $Value: Valor do cookie | |
* int $Days: Dias em que o cookie estará válido | |
* | |
**/ | |
public function setCookie(string $Value, int $Days=7) { | |
setcookie($this->CookieName, serialize($Value), ($Days*24*60*60), '/', "", "", true); | |
} | |
public function getCookie():string { | |
return isset($_COOKIE[$this->CookieName]) ? unserialize($_COOKIE[$this->CookieName]): null; | |
} | |
public function getProductById(int $ID):array { | |
return $this->getCookie()[$ID] ?? null; | |
} | |
public function removeProductById($ID) { | |
$Products = $this->getCookie(); | |
unset($Products["products_{$ID}"]); | |
$this->setCookie($Products); | |
} | |
/** | |
* | |
* Remove um item de um produto | |
* int $ID: Id do produto | |
* | |
**/ | |
public function removeItem(int $ID) { | |
$Product = $this->getProductById($ID); | |
if(!$Product) { | |
return; | |
} | |
if(1==$Product['product_amount']) { | |
return $this->removeProductById($ID); | |
} | |
$Products = $this->getCookie(); | |
$Products["products_{$ID}"]["product_amount"]--; | |
$this->setCookie($Products); | |
} | |
public function addItem(int $ID, int $Amount=1) { | |
$Products = $this->getCookie(); | |
if(!isset($Products["products_{$ID}"])) { | |
return; | |
} | |
$Products["products_{$ID}"]['product_amount'] += (int) $Amount; | |
$this->setCookie($Products); | |
} | |
public function toHTML() | |
{ | |
if(!count($this->getCookie())) | |
{ | |
return array('<b>Carrinho vazio.</b>'); | |
} | |
$Products = $this->getCookie(); | |
$Table = array(); | |
$Total = 0; | |
foreach($Products as $Key => $Value) { | |
$Total += $SubTotal = number_format($Value['product_price'] * $Value['product_amount'] , 2, ',', '.'); | |
$Table[] = array( | |
sprintf("Código %s - %s R$ %s - (Qtd %s) <br/>", $Value['product_code'], $Value['product_name'], number_format($Value['product_price'], $Value['product_amount']), | |
sprintf("%s - %s <br/>", $Value['product_options']['tamanho'], $Value['product_options']['cor']), | |
sprintf("R$ %s", $SubTotal) | |
); | |
} | |
$Table[] = sprintf("Itens: %s, Total: R$ %s", count($Products), number_format($Total, 2, ',', '.')); | |
return $Table; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment