What's new

How do you manage a cart? PHP help required

MRSN

T20I Star
Joined
Oct 20, 2010
Runs
30,597
friends who have good skills with PHP help get through this problem..

so I have an item collections, user adds them to his cart like this:

User: XYZ
productname: price: company
... .... ...
===================
total:

I use database to process the cart system, I have a table where all the users selections are stored,the cart just fetches that collections through php. Problem I have is how to clear the cart? suppose the user have made just doesn't order and he closes the window ,those selections would still appear since they were added to the table and I'm just using PHP to fetch it.

Also I'm not sure if I am processing the cart right way?i.e through database the specific table I have created which stores the collections...what if there are multiple people making the purchases at the same time..wouldn't this become messy?

hopefully someone could help me out..
 
Last edited:
can you help me understand this statement? [MENTION=133397]WebGuru[/MENTION] [MENTION=139108]Sachin136[/MENTION]

public function get_item($row_id){
return (in_array($row_id, array('total_items', 'cart_total'), TRUE) OR ! isset($this->cart_contents[$row_id]))
? FALSE
: $this->cart_contents[$row_id];
}

This basically returns the details of a given item but I can't understand the working of it..
 
can you help me understand this statement? [MENTION=133397]WebGuru[/MENTION] [MENTION=139108]Sachin136[/MENTION]



This basically returns the details of a given item but I can't understand the working of it..

It uses the ternary operator. [condition] ? [value if true] : [value if false]

If $row_id is in that array or it doesn't exist in cart_contents, then it returns false. Otherwise, it returns the details of the item from cart_contents.
 
Last edited:
It uses the ternary operator. [condition] ? [value if true] : [value if false]

If $row_id is in that array or it doesn't exist in cart_contents, then it returns false. Otherwise, it returns the details of the item from cart_contents.

thanks! what output would this statement generate? would it return an integer..and is it legal?
return(! isset($this->cart_contents[$row_id]));
 
thanks! what output would this statement generate? would it return an integer..and is it legal?
return(! isset($this->cart_contents[$row_id]));

It would return false if $this->cart_contents[$row_id] is set and not null (so isset($this->cart_contents[$row_id]) is true).

It will return true if $this->cart_contents[$row_id] isn't set or is NULL (so isset($this->cart_contents[$row_id]) is false).
 
Last edited:
There are two scenarios

Case 1: User is a guest
Case 2: User is authenticated

For Case 1:

I would store it in the $_SESSION and i would save it in the database as soon as the user logs into his account.


For case 2

It should be saved in the database, you don't need to clear cart in this case. Just check an item for inventory when the user submits the order and let him know about the inventory.
 
It would return false if $this->cart_contents[$row_id] is set and not null (so isset($this->cart_contents[$row_id]) is true).

It will return true if $this->cart_contents[$row_id] isn't set or is NULL (so isset($this->cart_contents[$row_id]) is false).

got it, thanks.:)
 
There are two scenarios

Case 1: User is a guest
Case 2: User is authenticated

For Case 1:

I would store it in the $_SESSION and i would save it in the database as soon as the user logs into his account.


For case 2

It should be saved in the database, you don't need to clear cart in this case. Just check an item for inventory when the user submits the order and let him know about the inventory.

what if the authenticated user just ads them to cart and but doesn't make an order?
 
what if the authenticated user just ads them to cart and but doesn't make an order?

Be smart and make it opportunity.

You can send an email to that user. "Did you forget to confirm your order x,y,z. There is a discount on these items." & after a certain number of days remove them if they are out of stock by creating a script which will run after every x amount of time (cron job)
 
what if the authenticated user just ads them to cart and but doesn't make an order?

You don't need to clear it so that user can still checkout next time he login. (That's how carts at most shopping sites work).

If you still want to clear his cart you can setup a function to clear his cart next time he login or you can setup a cron to clear all pending items in all users carts after 24 hours or any specific time.

Most sites like Godaddy keep reminding their users that there are pending items in their shopping cart and give them a catchy discount coupon. You can try this tactic to catch more orders.
 
Last edited:
Be smart and make it opportunity.

You can send an email to that user. "Did you forget to confirm your order x,y,z. There is a discount on these items." & after a certain number of days remove them if they are out of stock by creating a script which will run after every x amount of time (cron job)

You don't need to clear it so that user can still checkout next time he login. (That's how carts at most shopping sites work).

If you still want to clear his cart you can setup a function to clear his cart next time he login or you can setup a cron to clear all pending items in all users carts after 24 hours or any specific time.

Most sites like Godaddy keep reminding their users that there are pending items in their shopping cart and give them a catchy discount coupon. You can try this tactic to catch more orders.

true. love the idea.
 
[MENTION=133397]WebGuru[/MENTION] [MENTION=139108]Sachin136[/MENTION]
how do you write MySQL query which has variables as table name?

for example, I am fetching records from customers table and I have a '$cus' variable to use instead in the sql query for table_name how will I write this query?

I tried this but this doesn't work

"SELECT * FROM $cus "

I also tried
"SELECT * FROM '$cus' "
 
[MENTION=133397]WebGuru[/MENTION] [MENTION=139108]Sachin136[/MENTION]
how do you write MySQL query which has variables as table name?

for example, I am fetching records from customers table and I have a '$cus' variable to use instead in the sql query for table_name how will I write this query?

I tried this but this doesn't work

First one should work are you using an old version of PHP? Try using the code i pasted here https://kobra.io/#/e/-Kee861ZomBa5t50zU-O

Pakpassion is running behind sucuri firewall so it's not allowing to paste the code directly here.
 
Back
Top