How to add shopping cart contents in cookie in codeigniter? - php

I have situation where i want to store my whole cart into cookie so that if accidently user closed browser, cart contains the same contents using that cookie.
Right now, i am using codeigniter cart class to contain all the products user added to cart.
//array for product details...
$insert_data = array(
'id' => $this->input->post('id'),
'name' => $this->input->post('name'),
'price' => $this->input->post('price'),
'image' => $this->input->post('image'),
'qty' => 1
);
//add previously defined data to cart....
$this->cart->insert($insert_data);
//assigning whole cart to cookie
$this->input->set_cookie('cart_cookie', $this->cart->contents(),36000);
But here, set_cookie method is giving error as it is expecting second parameter to be string...But i want whole cart to be stored..
Any solution with this problem?
Or is there any better solution than cookie?

Related

How to get additional product information in cart.php in opencart?

I am using opencart for a web project and I add a custom field to all products called unit. I created a column named unit in the oc_product table. I can access a product's unit on the product.php page by adding data['unit'] = product_info['unit'] and fetching it via a query, but I want to access it on the cart.php page. I am not sure where the cart get's its products from thought. So I don't know where exactly to add the code to get the unit. In controller/common/cart.php there is this block of code:
$data['products'][] = array(
'product_id'=> $product['product_id'],
'key' => $product['key'],
'thumb' => $image,
//THIS
'unit' => $product['unit'],
'name' => $product['name'],
'model' => $product['model'],
'option' => $option_data,
'recurring' => ($product['recurring'] ? $product['recurring']['name'] : ''),
'quantity' => $product['quantity'],
'price' => $price,
'total' => $total,
'href' => $this->url->link('product/product', 'product_id=' . $product['product_id'])
);
If you look at the code above, right blow the comment THIS, I added a line to get the unit. Adding that code doesn't give me the unit and I know it doesn't because I am not querying it but I don't know where to add the query. If I replace $product['unit'] with a simple string like "12", I can access the 12 on the cart page. So the question is which query do I edit to get the unit on the cart page?
I was able to figure it out. Go to line 242 int /system/library/cart.php and in the array add the following line
'unit' => $product_query->row['unit']

Show product on main page by id opencart

I need to show one certain product on front page. I didn't find a module for this. What do I need to do? I need to show product with countdown timer but I can do this, I only do not know how to get product from database by ID or SKU. I guess the code should be like this:
echo $product_option_data[] = array(
'product_option_id' => $product_option['product_option_id'],
'option_id' => $product_option['option_id'],
'name' => $product_option['name'],
'type' => $product_option['type'],
'option_value' => $product_option['option_value'],
'required' => $product_option['required']
);
In OpenCart controller you can call
$this->load->model('catalog/product'); // only if not yet 'loaded' within the controller
$product = $this->model_catalog_product->getProduct($productId);
print_r($product);
which will return you the product by it's ID. Do with it whatever you need afterwards.

Opencart adding a new field to the customer view of the admin section

I have added a new field to my database. I also have managed to add necessary codes and functions in catalog section. This new field is related to the customer. The data related to this new field gets added successfully to the database.
This new field belongs to Customer table.
Now, I want to know, when viewing customer's details in the admin section, how this new field should be retrieved from database? I mean which file should be edited for this purpose?
getCustomer($customer_id) and getCustomers($data = array()) are the functions used to get customer data.
Since they are SELECT * querys your field is being processed automatically.
Afterwards you need to go in the Controller section in the controller\sale folder and there you have customer.php, custom_field.php and and edit the ones that you need. For example:
$this->data['customers'][] = array(
'customer_id' => $result['customer_id'],
'name' => $result['name'],
'email' => $result['email'],
'customer_group' => $result['customer_group'],
'status' => ($result['status'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled')),
'approved' => ($result['approved'] ? $this->language->get('text_yes') : $this->language->get('text_no')),
'ip' => $result['ip'],
'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added'])),
'selected' => isset($this->request->post['selected']) && in_array($result['customer_id'], $this->request->post['selected']),
'action' => $action
);
add your field in this array (this is from customer.php).
And finally edit the .tpl files that are called from the view\template\sale folder so they appear where they want them to.
Hope i was clear enough.

How to keep a session alive after logging in (Codeigniter)

We are building a webshop based on Codeigniter. Products are saved in a shopping cart with help of Codeigniters' Cart Class.
When a visitor logs in with it's account, all products which are already in their cart are removed because a new session started when the user logged in.
How can we keep the products in the cart at this point?
if($query->num_rows() == 1)
{
$user = $query->row_array();
$data = array(
'userid' => $user['id'],
'email' => $user['email'],
'validated' => true
);
$this->session->set_userdata($data);
return true;
}
You have to create new table in base, like shoppingcarts, and pass all items from that Chart class in that table, and user ID, then when user log in just query that table with his ID and ID of row now you got all products.But when user bye just delete that rows in base and unset cart class
$data = array(
'id' => 'sku_123ABC',
'qty' => 1,
'price' => 39.95,
'name' => 'T-Shirt',
'options' => array('Size' => 'L', 'Color' => 'Red'
'user_id => '45')
);
$this->cart->insert($data);
$chart = $this->cart->contents();
$this->db->insert('shoppingcarts',$chart);
This is just simple example, how to do that :)
If users have account, you need to save data in DB.
If users don't have an account and you want to keep his chart (based on browser), then you don't need to destroy session when user close the window. You need to keep it on session until he clear his chart. You can do it on config.php file.

MongoDB does not seem to want to push to array

I am making an online shopping cart, and I having huge issues pushing add to cart to items
$collection->update(
array('session' => $_SESSION["redi-Shop"],
array('$push'=>
array('items'=> $_POST["item"])
)));
When the customer selects their first item to add to the cart it works fine
$collection->insert(
array('session' => $_SESSION["redi-Shop"],
'status' => "cart",
'items' =>$_POST['item']));
but after the first item is added it does not allow me to add any more.
Please any advice would be helpful.
When you insert it the first time, the items field is not array (probably a string).
According to the mongodb $push doc :
The operation will fail if the field specified in the $push statement
is not an array.
Change your insert operation to :
$collection->insert(
array(
'session' => $_SESSION["redi-Shop"],
'status' => "cart",
'items' => array($_POST['item'])
));
Then run your update query.

Categories