Skip to content

Instantly share code, notes, and snippets.

@ogabrielsantos
Created June 16, 2015 16:35
Show Gist options
  • Save ogabrielsantos/ff3a4d23aab7a393653d to your computer and use it in GitHub Desktop.
Save ogabrielsantos/ff3a4d23aab7a393653d to your computer and use it in GitHub Desktop.
<?php
// Controller
public function add_cart($id = null)
{
$product = $this->Product->find(
'first',
array(
'recursive' => -1,
'fields' => array(
'Product.id',
'Product.description',
'Product.price',
'Product.quantity',
'Product.image'
),
'conditions' => array(
'Product.id' => $id
)
)
);
if (!$product) {
throw new NotFoundException('Compra Inválida');
}
$this->saveProduct($product);
$this->Session->setFlash('Produto adicionado ao carrinho.', 'flash_success');
return $this->redirect(
array(
'action' => 'cart',
$this->makeCart()
)
);
}
private function saveProduct(array $product)
{
$cart = CakeSession::read('cart');
if (!$cart) {
$cart = array();
}
$cart[] = $product;
return CakeSession::write('cart', $cart);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment