Skip to content

Instantly share code, notes, and snippets.

@subhanahmed047
Created June 18, 2016 14:46
Show Gist options
  • Save subhanahmed047/47a1e6f214947c6c6209125385e8f4b5 to your computer and use it in GitHub Desktop.
Save subhanahmed047/47a1e6f214947c6c6209125385e8f4b5 to your computer and use it in GitHub Desktop.
<?php
namespace IncCart\Controller\Component;
use Cake\Controller\Component;
/**
* Created by PhpStorm.
* User: Subhan Ahmed
* Date: 5/29/2016
* Time: 7:21 PM
*/
class CartComponent extends Component
{
public function addToCart($product)
{
$products = $this->getCart();
array_push($products, $product);
$this->getControllerCookie()->write("IncCart", $products);
}
public function removeFromCart($product)
{
$resultant_product = [];
$products = $this->getCart();
foreach ($products as $cart_product) {
if ($cart_product['id'] == $product['id']) {
}
}
return $resultant_product;
}
public function truncateCart()
{
return $this->getControllerCookie()->delete('IncCart');
}
public function getCart()
{
return $this->isExistCartCookie() ? $this->getControllerCookie()->read('IncCart') : [];
}
public function isExistCartCookie()
{
return $this->getControllerCookie()->check("IncCart");
}
private function getControllerCookie()
{
return $this->_registry->getController()->Cookie;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment