BOB Docs
APIApplicationStore

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:

  1. 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.
  2. 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.
  3. Cart Summary:

    • get_cart_summary(cart_id): Calculates the total cost and details of the cart, including currency and line items.
  4. 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.
  5. Order Association:

    • add_order(cart_id, order_id): Links a cart to an order, preventing further modifications.

Example Workflow:

  1. Add an Item to the Cart:

    await cart_service.add(cart_id=123, pricing_id=456, quantity=2)
  2. Retrieve a Cart Summary:

    summary = await cart_service.get_cart_summary(cart_id=123)
  3. Merge Carts:

    merged_cart = await cart_service.merge_carts(user_master_id="user123", user_slave_id="anon456")
  4. Associate a Cart with an Order:

    await cart_service.add_order(cart_id=123, order_id=789)

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.

On this page