Jump to content

Shopping Cart & Product Classes, etc


Go to solution Solved by nodirtyrockstar,

Recommended Posts

I'm writing some classes right now for a website with a shopping cart. There are all kinds of classes, Blog, Cart, Product, Database, etc. I am trying to make a design choice about where to store the requested quantity for a particular product. I feel like it should NOT go into the Product class, because the requested quantity does not semantically relate to any of the standard product detail. I was considering maybe setting the quantity property of the product instance, to the requested quantity, once it is put into the cart. For some reason my instinct says no, that it should be abstracted out, perhaps into the Cart class. If that is the case, how do you couple the requested quantity with the requested object? As array key/value pair? Is there another class it could go into? Does anyone have practical experience to shed some light on this?

Link to comment
https://forums.phpfreaks.com/topic/284538-shopping-cart-product-classes-etc/
Share on other sites

When you have something like this, a bunch of classes and some functionality, and you don't know where to put it, you need to analyse your model and try to find missing components.

 

In your case, you are missing Order. Your Customer Orders a Product by placing it into his Cart.

 

$order = new Order($someProduct, 12);
$cart->add($order);

Every class should have a single responsibility, forcing the responsibilities of Order onto Input means it is doing work it shouldn't have to. It also makes it harder for future developers working on your project to continue development due to classes having responsibilities that they are not aware of.

 

Which is why it's best to make your model as explicit as possible. And Order is a better name (since the customer orders a product by placing it into his basket) then Input. Just like you would have a Product class instead of naming it Input as well.

 

Input is better suited for all incoming request data. So that you can do:

 

$input->get('product_id');
$input->post('product_id');
// etc
Not to represent an Order or Product.

I think you are making a lot of assumptions about what classes I have and what they are doing. My input class only does one thing. It handles input. The way I handle the user's order is not something I really went into on here, but suffice it to say, it is separate from the Input class.

Edited by nodirtyrockstar
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.