Download the PHP package darkorsa/shop without Composer
On this page you can find all versions of the php package darkorsa/shop. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download darkorsa/shop
More information about darkorsa/shop
Files in darkorsa/shop
Package shop
Short Description Flexible shop package including stock validation, discounts, price formats, etc.
License MIT
Homepage https://github.com/darkorsa/shop
Informations about the package shop
shop
This library is a convenient to use implementation of the shopping cart with the following features:
- prices calculation (gross/net)
- tax calculation
- multiple price formats
- shipping costs
- payment costs
- customized discounts
- stock validation
- increasing/decreasing items quantity
Also you do not have to worry about the correctness of operations on the amounts of money, this is carried out with the help of the Money for PHP library which is the implementation of the Money pattern by Martin Fowler.
This package is PSR-2 and PSR-4 compliant.
Install
Via Composer
Usage
A few steps are required to create a cart object.
Products
Products represent the goods sold in the shop. The required parameters are:
- id
- name
- stock (how much of that product is available on stock)
- price
- taxRate
optional params:
- weight
- imagePath
If you need to include additional product data in your shopping cart, you can extend Product functionality by using the decorator pattern.
Cart items
Cart items represent the content of the shopping cart. Items can be injected into Cart object one by one or with use of collection.
To ensure that the amount of products in the cart is not greater than the amount in stock, a validator can be used.
Validation also takes place when you add an item to the Cart. When the same item is added again (i.e. an item with the same product ID), the item is not added twice, but it's quantity is incremented.
Shipping
Shipping can be defined so the shipping data will be available within the Cart object.
Payment
Payment can be defined in order to calculate payment fee. There are two methods of fee calculation. Fixed price and percentage.
Cart
Cart is an object representing a shopping cart and provides all the necessary methods to manage it or obtain calculated data.
Creation
Currency must be in ISO standard.
Usage
Note that all prices are represented as Money object.
Discounts
Discount can be applied to the Cart object. This library comes with 2 predefined discounts, however custom discounts can be applied as well.
- TotalPriceThresholdDiscount - the discount will be applied when the total price exceeds a certain price threshold
- EverySecondItemFreeDiscount - every second cart item is free
Discount example:
Presenation
By default the prices are returned as Money object but one can easly format all the prices within Cart with use of CartPresenter.
Default formatter
Default formatter is Decimal Formatter.
Other formatters
One can use other formatters shipped with Money for PHP library or write own custom formmatter.
Cart data
To obtain all the cart data like prices, items, products, shipping details, payment, etc. toArray method can be used.
This data can then be passed on to the presentation layers or as an API response.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Credits
- Dariusz Korsak
- All Contributors
License
The MIT License (MIT). Please see License File for more information.