VOOZH about

URL: https://deepwiki.com/gp247net/shop/4.1-cart-management-and-operations

⇱ Cart Management and Operations | gp247net/shop | DeepWiki


Loading...
Menu

Cart Management and Operations

Purpose and Scope

This document covers the shopping cart system in the GP247/Shop package, including cart operations (add, update, remove), multi-vendor cart handling, cart instances (default, wishlist, compare), and integration with product pages. For checkout processing and order creation, see Checkout Process. For customer authentication requirements, see Customer Authentication.


Cart Service Architecture

The cart system is built around the CartService class, which provides a unified interface for managing cart contents across multiple cart instances.

Cart Service and Instance Model


Sources: src/Controllers/ShopCartController.php12 src/Controllers/ShopCartController.php54 src/Controllers/ShopCartController.php764


Cart Data Structure

Each cart item contains the following properties:

PropertyTypeDescriptionRequired
idintegerProduct IDYes
namestringProduct nameYes
qtyintegerQuantityYes
storeIdintegerStore/vendor IDYes
optionsarrayProduct attributes/variantsNo
rowIdstringUnique row identifier (auto-generated)Auto

Cart Item Structure Diagram


Sources: src/Controllers/ShopCartController.php728-734 src/Controllers/ShopCartController.php809-816


Adding Products to Cart

The system supports two methods for adding products: standard POST form submission and AJAX requests.

Standard POST Method (Product Detail Page)

The addToCart() method handles POST requests from product detail forms.


Key validation steps:

  1. Product existence check
  2. Multi-vendor store ID resolution (lines 694-713)
  3. allowSale() validation (line 716)
  4. Attribute requirement check for products with variants (lines 718-724)

Sources: src/Controllers/ShopCartController.php684-746 src/Views/front/screen/shop_product_detail.blade.php61-63

AJAX Method (Product Listing Pages)

The addToCartAjax() method enables adding products without page reload.


Instance handling for special carts:

  • default: Main shopping cart
  • wishlist: Items saved for later (lines 828-856)
  • compare: Product comparison list (lines 828-856)

Sources: src/Controllers/ShopCartController.php754-868 src/Views/front/common/shop_js.blade.php3-37

Product Validation Rules

ValidationCheckError Handling
Product existsgetDetail() returns objectRedirect with error message
Allow sale$product->allowSale()Return error message
Has attributes$product->attributes->count()Redirect to detail page (AJAX) or error (POST)
Stock available$product->stock >= qty (if config enabled)Error message with SKU
Duplicate in wishlist/compareCheck if ID exists in instanceError: "Item already exists"

Sources: src/Controllers/ShopCartController.php716-724 src/Controllers/ShopCartController.php795-824


Cart Operations

Viewing Cart Contents

The getCartFront() method renders the cart page with all items.


Sources: src/Controllers/ShopCartController.php35-73

Updating Cart Quantities

The updateToCart() method handles AJAX requests to change item quantities.

Request parameters:

  • id: Product ID
  • rowId: Cart row identifier
  • new_qty: New quantity value
  • storeId: Store ID for product lookup

Validation logic:

  1. Verify product exists in specified store
  2. Check stock availability if product_buy_out_of_stock config is disabled
  3. If quantity is 0, remove item from cart
  4. Otherwise, update to new quantity

Sources: src/Controllers/ShopCartController.php875-911

Removing Individual Items

The _removeItem() method removes a single item from any cart instance.

Method signature: _removeItem($instance = 'cart', $id = null)

Process:

  1. Check if item ID exists in cart instance
  2. Call CartService::remove($rowId) to remove
  3. Redirect back to cart/wishlist/compare page

Sources: src/Controllers/ShopCartController.php1049-1058

Clearing Entire Cart

The _clearCart() method destroys all items in a cart instance.

Method signature: _clearCart($instance = 'cart')

Implementation:

(new Cart)->instance($instance)->destroy()

Supported instances:

  • cart (default shopping cart)
  • wishlist
  • compare

Sources: src/Controllers/ShopCartController.php1018-1022


Multi-Vendor Cart Management

The system supports multiple vendors (stores) by organizing cart items by store_id.

Store Grouping Architecture


Checkout Preparation by Store

The prepareCheckout() method handles transitioning from cart to checkout for a specific vendor.

Process flow:

  1. Receive store_id from POST data (line 91)
  2. Validate store ID exists (lines 93-96)
  3. Get items grouped by store using getItemsGroupByStore() (line 98)
  4. Validate cart for selected store is not empty (lines 101-103)
  5. Update quantities from form inputs (lines 109-115)
  6. Check minimum order quantities per product (lines 106-126)
  7. Set storeCheckout session variable (line 129)
  8. Redirect to checkout page (line 131)

Sources: src/Controllers/ShopCartController.php80-132

Multi-Vendor Store ID Resolution

When adding products to cart, store ID is determined based on configuration:


Sources: src/Controllers/ShopCartController.php694-713 src/Controllers/ShopCartController.php767-791


Cart Instances for Special Features

Wishlist

The wishlist allows customers to save products for future purchase.

Key characteristics:

  • Instance name: wishlist
  • Quantity always set to 1
  • Prevents duplicate products (line 829-830)
  • Accessible via wishlistProcessFront() route

View method:

(new Cart)->instance('wishlist')->content()

Sources: src/Controllers/ShopCartController.php919-951

Compare

The compare feature enables side-by-side product comparison.

Key characteristics:

  • Instance name: compare
  • Quantity always set to 1
  • Prevents duplicate products
  • Accessible via compareProcessFront() route

View method:

(new Cart)->instance('compare')->content()

Sources: src/Controllers/ShopCartController.php959-992

Instance Switching Pattern


Sources: src/Controllers/ShopCartController.php764 src/Controllers/ShopCartController.php934 src/Controllers/ShopCartController.php974


Integration with Product Pages

Product Detail Page Integration

The product detail form includes hidden fields and attribute selection:

Form structure:

  • Action: gp247_route_front('cart.add')
  • Method: POST
  • Hidden field: product_id
  • Quantity input: qty (default: 1, min: 1, max: 100)
  • Attribute fields: form_attr[] (if product has attributes)

Sources: src/Views/front/screen/shop_product_detail.blade.php61-92

Product Listing Integration

Product listing cards include AJAX add-to-cart buttons:

Button implementation:


Conditional display:

  • Only shown if $product->allowSale() returns true
  • Only shown if gp247_config('product_use_button_add_to_cart') is enabled

Sources: src/Views/front/common/shop_product_single.blade.php14-16

Wishlist and Compare Buttons


Sources: src/Views/front/common/shop_product_single.blade.php30-44


Stock and Availability Validation

Stock Checking During Add

When adding products to cart, stock is validated based on configuration:

Configuration key: product_buy_out_of_stock

Behavior:

  • If false: Cannot add out-of-stock products
  • If true: Can add any quantity regardless of stock

Stock Checking During Update

The updateToCart() method validates stock when increasing quantities:

Validation logic:

if ($product->stock < $new_qty && !gp247_config('product_buy_out_of_stock', $product->store_id))
{
 return error: 'Item over quantity'
}

Sources: src/Controllers/ShopCartController.php896-902

Minimum Quantity Validation

The prepareCheckout() method enforces minimum order quantities:

Process:

  1. Aggregate total quantity per product across cart items (lines 106-115)
  2. Fetch minimum quantities from ShopProduct table (line 116)
  3. Compare totals against minimums (lines 118-122)
  4. Return error if any product is below minimum (lines 123-125)

Error format:


Sources: src/Controllers/ShopCartController.php106-126


Session Management

Cart-Related Sessions

The cart system uses Laravel sessions to track state across requests:

Session KeyPurposeSet InCleared In
storeCheckoutSelected store for checkoutprepareCheckout()clearSession()
dataCheckoutCart items being checked out_getCheckout()clearSession()
shippingAddressCustomer shipping infoprocessCheckout()clearSession()
shippingMethodSelected shipping methodprocessCheckout()clearSession()
paymentMethodSelected payment methodprocessCheckout()clearSession()
address_processNew address flagprocessCheckout()After address save
dataTotalOrder totals breakdown_getCheckoutConfirm()clearSession()
orderIDCreated order IDaddOrder()clearSession()

Session Lifecycle


Sources: src/Controllers/ShopCartController.php129 src/Controllers/ShopCartController.php174 src/Controllers/ShopCartController.php362-383 src/Controllers/ShopCartController.php491 src/Controllers/ShopCartController.php645 src/Controllers/ShopCartController.php1194-1206

Session Clearing

The clearSession() method removes all checkout-related session data:

Cleared keys:

  • paymentMethod
  • shippingMethod
  • totalMethod
  • otherMethod
  • dataTotal
  • dataCheckout
  • storeCheckout
  • dataOrder
  • arrCartDetail
  • orderID

Called from:

  • _getCart() - Before displaying cart page
  • processAfterOrderSuccess() - After successful order completion
  • cancelOrder() - When payment is cancelled

Sources: src/Controllers/ShopCartController.php1194-1206


Cart to Checkout Transition

Validation Before Checkout


Sources: src/Controllers/ShopCartController.php80-132

Cart Item Removal After Order

The clearCartStore() method removes checked-out items from the cart:

Process:

  1. Retrieve dataCheckout from session
  2. Iterate through each cart item
  3. Call CartService::remove($row->rowId) for each item
  4. Items are permanently removed from shopping cart

Called from: completeOrder() after successful payment processing

Sources: src/Controllers/ShopCartController.php1181-1189


Route Definitions

Main Cart Routes

Route NameURL PatternController MethodPurpose
cart/cartgetCartFront()Display cart page
cart.add/cart/addaddToCart()Add via POST form
cart.add_ajax/cart/add_ajaxaddToCartAjax()Add via AJAX
cart.update/cart/updateupdateToCart()Update quantity (AJAX)
cart.remove/cart/remove/{instance}/{id}removeItemProcessFront()Remove single item
cart.clear/cart/clear/{instance}clearCartProcessFront()Clear cart instance
cart.prepare_checkout/cart/prepare_checkoutprepareCheckout()Prepare for checkout

Wishlist and Compare Routes

Route NameURL PatternController MethodPurpose
wishlist/wishlistwishlistProcessFront()Display wishlist
compare/comparecompareProcessFront()Display comparison

Sources: Route definitions are registered in the service provider, usage shown throughout src/Controllers/ShopCartController.php

Refresh this wiki

On this page