/    Sign up×
Community /Pin to ProfileBookmark

PHP/OOP – Collection for cart

As hopefully the title has given a clue to my question.

I have a shopping cart script which currently uses an array. Product ID and count for how many are in the cart of this item.

I want to make this into a OOP Collection instead.

My question is,

my collection of products is fine, but do I just add the same product twice to my collection if someone wants to buy multiple numbers of the same products?

Or should my product class have a integer in there?

what would be the best approach?

Also thinking is need to update the number or delete all.

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@NogDogSep 27.2020 — My initial instinct is to have a quantity attribute for each product in the cart, but I don't know if I have a "technical" reason -- it just feels better to me for some reason. It may make things a bit cleaner later if you ever want to do something like "5 for the price of 4" on a given product?
Copy linkTweet thisAlerts:
@kiwisauthorSep 28.2020 — Thanks, that’s probably how I’ll go about it.

out of interest. How do you pass your collection of from one page to another?

Say if in view-cart.php I show my collection.

how can I store this and pull it back up on payment.php?

Can I store in a session for example?
Copy linkTweet thisAlerts:
@NogDogSep 28.2020 — I think a session makes sense, unless you think you might want things to stay in the cart for an extended period of time (for some undefined value of "extended period of time"), in which case a database solution might be better, if just for the more dependable long-term storage. (Maybe just one "cart" table with columns user_id, product_id, quantity, and created_at and modified_at timestamps?)
Copy linkTweet thisAlerts:
@kiwisauthorSep 28.2020 — @NogDog#1623743

so you can place a collection (an object) of objects into a session?

how does the second page know all the properties of each object inside the collection?

do you need to do something with the session when you retrieve it on page load?
Copy linkTweet thisAlerts:
@NogDogSep 28.2020 — Hmm...in theory you can use [serialize()](https://www.php.net/serialize) and [unserialize()](https://www.php.net/unserialize) to do that, but IIRC it gets a bit cumbersome and fiddly to do so with objects. It might be easier to add a method to your collection class that will build an array with just the necessary info to rebuild it, store that in $_SESSION, and add another method that could accept that array and rebuild the collection?

Sorry for the vague answers -- haven't tried to deal with that sort of thing in quite awhile. :(
Copy linkTweet thisAlerts:
@kiwisauthorSep 30.2020 — @NogDog#1623751

oh right so have a method called something like makeIntoArray and have each ProductID and a count of how many are in the cart for each ID. Basically what I do today and save the session recall it in the next page where I call something like makeFromArray method?
Copy linkTweet thisAlerts:
@NogDogSep 30.2020 — @kiwis80#1623783 Yep, pretty much what I was thinking. Possibly even just have a private method that stores that info directly to $_SESSION called from any method that would change it? Then you'd just need to have something you could include in each page's start-up that did something like (at the most basic):
<i>
</i>$cart = new Cart();
if(!empty($_SESSION['cart_data'])) {
$cart-&gt;populate_from_array($_SESSION['cart_data']);
}
Copy linkTweet thisAlerts:
@kiwisauthorOct 02.2020 — So further to this, I'll need to connect to my database within my class to retrieve the details of my object wont i?
``<i>
</i> public function populateFromArray($inputArray){

foreach($inputArray as $key =&gt; $value){

$product1 = new Product($key, "Test Product", 124, 'image', 'desc', 99);
$this-&gt;addProduct($product1, $value);
}
}<i>
</i>
``
×

Success!

Help @kiwis spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...