Reputation: 131
I'm working in CakePHP 3.2
I have a table Carts
to store products in cart when user is logged in with user_id
.
I want addToCart accessible to user without login too. But in this case I want to use temporary table to store the cart data and when user is logged in, transfer all data from temporary table to carts
table and delete temporary table
.
How to work with temporary table in CakePHP 3 ? Is it a good practice to use temporary data for the same or is there any better alternative to this ?
Edit 2
Values to store in cookie/temp table
product_id
seller_id
seller_product_id
quantity
Associations
product_id
is foreign key to products
table
seller_id
is foreign key to sellers
table
seller_product_id
is foreign key to seller_products
table
product table is further associated with product_attributes
Currently using carts
table with following columns
+-----------+-------------+----------+---------------------+--------+
| user_id | product_id | seller_id | seller_product_id |quantity |
+-----------+-------------+----------+---------------------+--------+
and this is what I'm doing to retrieve associated data
if (!empty($this->Auth->user('id'))) {
$user_id = $this->Auth->user('id');
$g_cart = $this->Carts->find('all', [
'conditions' => [
'user_id' => $user_id,
],
'contain' => [
'Products', 'SellerProducts', 'Sellers', 'CartAttributes'
]
]);
$g_count = $g_cart->count();
if (!empty($g_cart)) {
// foreach($g_cart as $g_c){debug($g_c);}
foreach($g_cart as $g_c) {
$total_cost += $g_c->quantity * $g_c->seller_product->selling_price;
}
}
}
Hope I'm clear to you.
Upvotes: 0
Views: 423
Reputation: 859
You could store it in cookies for guests then transfer the cookie data into table when they login.
I have not tested this but it should get the ball rolling
On the method where cart items are added:
$cart = $this->Cookie->read('Cart.items');
if(!$this->Auth->user()) {
if(!empty($cart)) {
$newItem = [
'Products' => $product->id,
'Sellers' => $seller->id,
'SellerProducts' => $sellerProduct->id,
'quantity' => $quantity
];
$newCart = array_merge($cookie, [$newItem]);
$this->Cookie->write('Cart', ['items' => $newCart]);
}else{
$this->Cookie->write('Cart', [
'items' => [
'products' => $product->id,
'sellers' => $seller->id,
'seller_products' => $sellerProduct->id,
'quantity' => $quantity
]
]);
}
}else{
$item = $this->Cart->newEntity();
if(!empty($cart)) {
foreach($cart as $items) {
$data = [
'user_id' => $this->Auth->user('id'),
'product_id' => $items['Products'],
'seller_id' => $items['Sellers'],
'seller_product_id' => $items['SellerProducts'],
'quantity' => $items['quantity']
];
$item = $this->Cart->patchEntity($item, $data);
$this->Cart->save($item);
}
$this->Cookie->delete('Cart.items');
}
}
on CartController view method
$cart = $this->Cookie->read('Cart');
$entities = [];
if(!$this->Auth->user() && !empty($cart) {
$i = 0;
foreach($cart['items'] as $items) {
foreach($items as $model => $id) {
if($model == 'quantity') {
$quantity = $id;
continue;
}
$this->loadModel($model);
$entity = $this->$model->get($id, ['contain' => []]);
$entities[$i][] = $entity;
$entities[$i]['quantity'] = $quantity;
}
$i++;
}
$this->set('items', $entities);
}
On the view.ctp for CartController
if(!empty($items)) {
foreach($items as $item) {
foreach($item as $entity) {
echo '<div><?=$entity->name</div>';
echo '<div><?=$entity->quantity</div>';
}
}
}
Upvotes: 0