Maginium
Docs
Docs
  • Welcome
  • Getting Started
    • Quickstart
    • Installation
    • Structure
  • The Basics
    • Concurrency
    • 🔤Strings
    • ⚙️Actions
    • 🗃️Cache
    • 🗂️Collections
    • 🔲Component
    • ⚙️Config
    • 💬Console
    • 🗃️Database
      • Overview
      • Eloquent
      • Seeder
      • Migration
      • Models
      • Factories
      • Schemas
      • Traits
      • Concerns
    • 🛅Container
    • 🔠Enum
    • 📦Event
    • 📝Log
    • 🔢Pagination
    • 💬Prompts
    • 🌐Request
    • 📡Response
    • ⚡Redis
    • 📏Resize
    • 🔌Serializer
      • Json
      • Serializer
      • Closure Serializer
    • 🔑Uuid
    • 👤Avatar
    • 🔐Hashing
    • 📤Dto
      • Overview
      • Attributes
    • 🌍Locale
    • 🔗Token
      • Api Keys
      • Admin Token
      • Customer Token
    • 🕘Message Queue
    • 🖼️Media
    • Helpers
    • 📜Crud
      • Filters and Sorts
        • introduction
        • Basic Usage
        • JS Examples
          • Available Methods
          • Sorts
          • Filters
        • Rename Fields
        • Relation Fields
        • Changing Params Source
        • Allowed Fields
        • Queries and javascript examples
        • Sort null values last
        • Restrict
        • Custom Filters
  • Commerce Modules
    • API Key Module
    • Auth Module
    • Cart Module
    • Customer Module
    • Inventory Module
    • Fulfillment Module
    • Order Module
    • Payment Module
    • Pricing Module
    • Product Module
    • Promotion Module
    • User Module
  • SDKs and Tools
    • create-maginium-app
    • Maginium CLI
    • JS SDK
    • Storefront Starter
Powered by GitBook
On this page
  • Key Features
  • Getting Started
  • Configuration
  • Using the Cart Module
  • Handling Checkout
  • Advanced Features
  • Customisation
  • Best Practices
  • Conclusion
  1. Commerce Modules

Cart Module

The Cart Module in Maginium provides a flexible, efficient, and secure way to manage shopping carts in your application. Designed for scalability, it handles everything from simple cart operations to complex e-commerce workflows.

Whether you are building a small store or a large-scale platform, the Cart Module seamlessly integrates into your project, ensuring a smooth user experience.


Key Features

  • Add and Remove Items: Dynamically add or remove items to the cart with ease.

  • Item Quantity Management: Adjust the quantity of items directly in the cart.

  • Session-Based Persistence: Cart data is stored across user sessions.

  • Database Support: Option to store cart information in your database for persistent carts.

  • Customisable: Fully customisable to suit your application’s unique requirements.

  • Discounts and Coupons: Apply discounts and coupons for promotions.

  • Tax and Shipping Calculations: Automatically calculate applicable taxes and shipping costs.

  • Guest and Authenticated User Support: Works seamlessly for both guest users and logged-in users.


Getting Started

Installation

To enable the Cart Module in your Maginium application, ensure it is included in your configuration. Run the following command to install the required package:

composer require maginium/cart

Then, publish the configuration file:

php artisan vendor:publish --tag=cart-config

This command will publish a cart.php configuration file to your application’s config directory.


Configuration

The cart.php file includes various options for customisation:

return [
    'storage' => 'session', // Options: session, database
    'tax_rate' => 0.1, // Default tax rate (10%)
    'currency' => 'USD', // Default currency
];
  • Storage: Defines where cart data will be stored. By default, it uses the session.

  • Tax Rate: The default tax percentage applied to the cart.

  • Currency: Sets the default currency for cart transactions.


Using the Cart Module

Adding Items to the Cart

use Maginium\Cart\Cart;

Cart::add([
    'id' => 101,
    'name' => 'Wireless Headphones',
    'quantity' => 1,
    'price' => 199.99,
    'attributes' => [
        'color' => 'black',
    ],
]);

Retrieving Cart Contents

$cartContents = Cart::content();
foreach ($cartContents as $item) {
    echo $item->name . ' - ' . $item->quantity;
}

Updating Item Quantity

Cart::update(101, [
    'quantity' => 2,
]);

Removing Items from the Cart

Cart::remove(101);

Handling Checkout

The Cart Module integrates with Maginium’s payment and order modules to facilitate a smooth checkout process. Use the following example to convert cart contents into an order:

$order = Cart::checkout([
    'user_id' => Auth::id(),
    'payment_method' => 'credit_card',
    'shipping_address' => $address,
]);

Clearing the Cart

After a successful checkout, clear the cart:

Cart::clear();

Advanced Features

Persistent Cart for Authenticated Users

For authenticated users, you can enable persistent carts that save to the database:

'middleware' => ['auth'],

Coupons and Discounts

Cart::applyCoupon('SUMMER2025');

The applyCoupon method applies a discount to the cart, reducing the total price.


Customisation

The Cart Module is fully extensible. Override core methods to add custom logic to your cart operations. For example, you can add a custom tax calculation:

Cart::setTaxCalculation(function($subtotal) {
    return $subtotal * 0.15; // Custom 15% tax
});

Best Practices

  1. Validate Inputs: Always validate item data (ID, quantity, etc.) before adding it to the cart.

  2. Secure Data: Use secure methods to store cart information, especially for guest users.

  3. Optimise Database Queries: If using the database for storage, ensure queries are optimised for large-scale data.


Conclusion

The Cart Module in Maginium is designed to simplify e-commerce development, providing robust features and flexibility. Start building powerful shopping experiences today with Maginium’s Cart Module.

PreviousAuth ModuleNextCustomer Module

Last updated 3 months ago