CartService
Key Features:
- Cart Management: Creates, retrieves, and manages the user's cart, supporting both logged-in and anonymous users.
- Cart Line Handling: Adds products to the cart, updates quantities, and removes items. Automatically manages the cart lifecycle based on its content.
- Cart Summary: Provides a detailed summary of cart contents, including the total cost and currency.
- Cart Merging: Combines the contents of two carts, typically when an anonymous user logs in, transferring items seamlessly.
- Order Association: Links a cart with an order to lock it for further modifications post-checkout.
Methods:
-
Adding and Removing Items:
add(cart_id, pricing_id, quantity)
: Adds a product to the cart or updates its quantity. Creates a new cart line if none exists.remove_line(cart_id, line_id)
: Removes a specific line from the cart. Deletes the cart if it becomes empty.
-
Cart Retrieval and Management:
get_current_cart(user_id, is_anonymous)
: Retrieves the current cart for a user.get_or_create_cart(user_id, is_anonymous)
: Retrieves an existing cart or creates a new one if none exists.get_cart_lines(cart_id)
: Retrieves all lines in the cart with associated product and pricing data.
-
Cart Summary:
get_cart_summary(cart_id)
: Calculates the total cost and details of the cart, including currency and line items.
-
Cart Merging:
merge_carts(user_master_id, user_slave_id)
: Merges two carts, transferring items from the slave cart to the master cart and deleting the slave cart.
-
Order Association:
add_order(cart_id, order_id)
: Links a cart to an order, preventing further modifications.
Example Workflow:
-
Add an Item to the Cart:
-
Retrieve a Cart Summary:
-
Merge Carts:
-
Associate a Cart with an Order:
Dependencies:
- Repositories Used:
CartRepository
: Handles database operations for carts.CartLineRepository
: Manages cart line items, including adding and removing products.
Benefits:
- Seamless User Experience: Supports smooth transitions between anonymous and logged-in user states.
- Data Integrity: Prevents cart modifications once linked to an order.
- Flexibility: Handles complex scenarios like cart merging and dynamic cart updates.
- Integration: Works harmoniously with order processing and payment workflows.
The CartService
ensures robust and scalable cart operations, crucial for modern e-commerce platforms.