What is the logic of a shopping cart?

Asked

Viewed 2,160 times

0

I am learning programming and want to try to make a shopping cart system in PHP that adds the products in the cart and finalizes the order. From what I’ve been seeing, it seems that this type of system uses muito of the resource of array multidimensional to connect the buyer to the products.

What kind of data do you use to say que um pedido tem x produtos e pertence a fulano de tal? Would anyone be willing to explain to me a little the logic of this type of system?

  • 1

    You can use Session to keep products in the user’s shopping cart. Session in PHP usually contains your session data like Name and login, but it can also contain data of other things. It is not convenient to put too much pressure on Session. But for a shopping cart it is perfect.

3 answers

2

In a virtual store, for example, the focus is to sell. A user places things in the cart and for some reason closed the browser. When it return on this site will have to redo the entire purchase, fetch the products and put them in the cart. In this process the user may get impatient and give up buying. If the site had feature save cart and identify the customer even if not logged in, would have higher chance to realize the sale without boring you.

I just copied the excerpt from another reply. The original can see here: /a/119365/4793

Frizando again, the decision depends on each case. Regardless of the focus being for sales, sometimes saving the cart in database may not be ideal.

I particularly recommend saving the data even if the user is not authenticated because this data is useful to generate statistics and better understand the behavior of users. So you can study ways to improve sales. Because as I mentioned several times, the focus is to sell.

In the technical part, speaking of codes, think about how to assemble the structure.

Avoid weak or messy things like

$_SESSION = array(
    'id do produto' => array('nome do produto', 'valor', 'quantidade')
)

Because in the global variable $_SESSION can have other data not referring to the cart as the id of the authenticated user.

So at least build something more organized like

$_SESSION['id da loja']['cart'] = array(
    'id do produto' => array('nome do produto', 'valor', 'quantidade')
)

if you use Session to authenticate, then mount something like this

$_SESSION['user']['id usuario'] = 'um token'

This will avoid conflict and allow flexibility.

But the ideal is to save only one token also to the Cart.

$_SESSION['id da loja']['cart'] = 'token do cart';

This token would then be related to a table in the database

tabela cart
   id_loja
   id_usuario
   cart_token

tabela cart_items
   id_item
   item_valor
   item_quantidade
   id_qualquer_outra_coisa_que_precisar

But instead of using Session to save the token, use cookie

$_COOKIE['id da loja']['cart'] = 'token do cart';

as mentioned above, using the cookie, you may allow the user to restore the shopping cart if for some reason you close your browser.

0

I think the best option would be to save the products to the database for example in a table cart ( pk code, product ... etc ) so you could associate this cart with a user for example Joaquim has the caro1 and when you make a request you will fetch the data of the 2 and fill in, and you have to be careful with various aspects, how to store a cart in the product for 5 months and it suffer a change in value ! You need to think a little bit first about what you want to keep in the cart. I hope I helped xD I’m also new but I’ve made a cart +- this way. anything I’m around ... hug ;)

-2

Good afternoon!

I know this post is old, but I see that there are a lot of people coming in, and maybe, this is a question of people nowadays.

To create a shopping cart, you can use several ways to create the application in PHP.

Basically, you will need to have a buy button that when clicked it stores in a table of the database the product id or can also, if desired, add other relevant information to the product, be saved the session, date/time, and other desired information.

When user click on the button a session should be created, which I normally call from Cart, and every time you click on the buy button, the system should check whether that "Cart" session exists or not, whether it exists remains with the session, otherwise it should be created.

In the shopping cart, you will pull the order, listing the products related to session, that is, when making the query you will use in Where the column where you stored the session and then just make available to the user if you want to delete the product from that order or change the quantity, among others.

To make the total value, usually you will create a variable that we will name here as total and will create it with zero value before the for or foreach, and within the repetition structure you mention it this way: $total += $value-do-foreach['column-with-product-value'];

In this way, the value will be added to the extent that the repeat loop will be executed by the application.

You leave a playlist on Youtube of how to create a shopping cart, think it will help people in building their own shopping cart: https://www.youtube.com/watch?v=TTMLMIfHud8&list=PLVR6F4yZo5Lk-5Qo_6E-dp6gmKIwWCSjn

Browser other questions tagged

You are not signed in. Login or sign up in order to post.