Download the PHP package mrnewport/laravel-stow without Composer
On this page you can find all versions of the php package mrnewport/laravel-stow. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mrnewport/laravel-stow
More information about mrnewport/laravel-stow
Files in mrnewport/laravel-stow
Package laravel-stow
Short Description This unofficial laravel package allows for the addition of any model to an instancable "Cart", "Quote" or other "Basket".
License MIT
Informations about the package laravel-stow
Laravel Stow Package
Overview
Laravel Stow is an unofficial laravel package designed to handle the management of baskets and basket items within a Laravel application. It provides a flexible and extensible solution for handling shopping carts, wishlists, and other similar use cases. This package intentionally avoids pricing and checkout functionality for granularity purposes, focusing solely on the management of items within baskets. If you are looking for a qty based pricelist functionality, keep your eye out for mrnewport/price, my upcoming package.
Installation
To integrate the Laravel Stow package into your Laravel project, execute:
Then, publish and migrate the database tables:
Configuration
Define basket instances and restrictions in config/stow.php
to ensure type safety and integrity:
Instances not listed will remain unrestricted, accepting any Stowable
models.
Creating Baskets
Instantiate a basket, specifying an optional type (instance name):
Adding Items to Baskets
Add Stowable interface to any models to allow them to be added to baskets:
Add items respecting instance restrictions and options:
Items that are already in the basket will have their quantities incremented only when the item exists with identical options.
Updating Basket Items
Update item details within the basket, this requires the BasketItem or its ID:
Removing Items from Baskets
Remove items from the basket by BasketItem or ID:
Retrieving Basket Items
Retrieve items from the basket:
Merging Baskets
Merge the contents of one basket into another:
Clone Baskets
Clone a basket and its associated items:
Deleting Baskets
Delete a basket and its associated items:
Detailed Events Overview
Here's a look at the events, the objects they provide, and how they can be used:
1. BasketCreatedEvent
- Object Included:
$basket
(The newly created basket instance) - Usage: You can use this event to perform actions after a new basket has been created, such as logging, custom notifications, or integrating with other systems.
2. BasketUpdatedEvent
- Object Included:
$basket
(The updated basket instance) - Usage: This event allows you to respond to updates on a basket, enabling actions like auditing, triggering business workflows, or updating related data.
3. BasketDeletedEvent
- Object Included:
$basket
(The deleted basket instance) - Usage: Important for cleaning up related resources or performing actions post-deletion. The built-in listener uses this to remove associated basket items, but you can extend functionality to suit other needs.
4. BasketItemCreatedEvent
- Object Included:
$basketItem
(The newly added basket item instance) - Usage: Useful for tracking additions to the basket, updating inventory, or triggering recommendations based on new basket contents.
5. BasketItemUpdatedEvent
- Object Included:
$basketItem
(The updated basket item instance) - Usage: Leverage this event to handle changes in basket items, such as adjusting stock levels, recalculating basket totals, or applying additional business rules.
6. BasketItemDeletedEvent
- Object Included:
$basketItem
(The removed basket item instance) - Usage: This event can be used for actions similar to BasketItemUpdatedEvent, but specifically in response to item deletions.
Creating and Registering Custom Listeners
To create a custom listener for any of these events, generate a listener using the Artisan command:
In your custom listener, you can access the event object and its properties:
Register your listener in the EventServiceProvider
:
Best Practices and Possible Pitfalls
- Ensure correct instance configuration to prevent unintended item types.
- Utilize events for syncing basket states across the application.
- Monitor performance when using large baskets, and consider caching strategies.