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:
Then, publish the configuration file:
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:
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
Retrieving Cart Contents
Updating Item Quantity
Removing Items from the Cart
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:
Clearing the Cart
After a successful checkout, clear the cart:
Advanced Features
Persistent Cart for Authenticated Users
For authenticated users, you can enable persistent carts that save to the database:
Coupons and Discounts
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:
Best Practices
Validate Inputs: Always validate item data (ID, quantity, etc.) before adding it to the cart.
Secure Data: Use secure methods to store cart information, especially for guest users.
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.
Last updated