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.