Download the PHP package dlds/yii2-ecom without Composer
On this page you can find all versions of the php package dlds/yii2-ecom. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package yii2-ecom
E-commerce extension for Yii2
Provides support for basic e-commerce operations as a wrapper for dlds/yii2-payment. For usage examples see dlds/yii2-app-ecom - a sample application using this extension. This guide assumes that you are familiar with implementations of Estonian bank payments (see pangalink.net)
Main features:
- Extensible code, almost everything can be customized
- Integration with ActiveRecord objects (orders, products)
- Shopping basket functionality (support for multiple storage methods)
- Support for payment adapters (currently Estonian banks are implemented)
- Support for discounts
- Basic widgets for displaying shopping basket contents, payment forms, lists
Installation
You can install this package using composer. In your composer.json
add the following required package:
Technical overview
The package lives under \dlds\ecom
namespace and consists of 3 sub-components (these can be overridden in configuration and are directly accessible as properties of the main component).
- basket - provides shopping basket functionality
- payment - provides functionality for receiving payments
- formatter - helper class used in grid views
Most classes and subclasses can be overridden in the configuration.
Configuration
After the installation you will probably want to:
- Override and extend the main class to provide some custom functionality
- Register this class as application component
- Configure the bank payment parameters and the return URL
You can use something similar in the application configuration to achieve this:
And define the class MyEcomComponent
like this:
You can use banks-default.php
to store general bank configuration (e.g. which banks are used) and define the more
specific access parameters in banks-local.php
, which is environment-specific and is not held in CVS. You can download
the example files from here.
Usage
The following examples assume that you have registered dlds\ecom\Component
(or its child class) as an application component under Yii::$app->ecom
.
Using ActiveRecord integration
ActiveRecord integration makes it very easy to add items to shopping basket and to use the basket to create orders and payment forms. Currently there are three kinds of AR integration available.
- Implement
dlds\ecom\models\BasketProductInterface
in your AR model class to add support for products and services - Implement
dlds\ecom\models\BasketDiscountInterface
to add support for discounts - Implement
dlds\ecom\models\OrderInterface
to add support for orders
Using the shopping basket
Operations with the shopping basket are very straightforward when using a models that implement one of the two basket interfaces.
The basket object can be accessed under \Yii::$app->ecom->basket
and can be overridden in configuration if you need to customize it.
Items in the basket
Products/items that are added to the basket are serialized/unserialized when saving and loading data from basket storage. If you are using Active Record models as products/discounts, make sure that you are omitting any unnecessary references from the serialized data to keep it compact.
Creating orders
The basket object can easily be converted into orders (AR objects that implement the OrderInterface
).
This calls the saveFromBasket
method from your Order class, which could look something like this.
Using payments
Generating payment forms
If you have saved your Order objects, you can use the included widget dlds\ecom\widgets\PaymentButtons
to render all the bank forms included in your configuration. You can provide your own widget class in the configuration if you need customization (override widgetClass
under payment
sub-component). There is a shorthand method for generating the widget with correct parameters:
Receiving requests from banks
After payment the user is directed back to the application to a URL specified in your configuration by bankReturnRoute
under payment
sub-component. In your controller action you can pass the request to the component like this:
The above code loads the right Order from the database and calls the bankReturn
method from your Order class. You are responsible for saving the order and logging necessary data.
Advanced examples
Storing basket data in database
There are two built-in adapters for storing basket data: session storage (default) and database storage. You can also write your own storage classes and define them in configuration under basket
component:
In addition to table name, you can override both field names (one for user identifier and another for session data). If Database::$userComponent
is set (for example to user
), then the adapter will try to ask the user identifier via \Yii::$app->user->getId()
method. If this fails or the component name is not specified, session ID will be used to identify the user. The session table should have at least two fields and look similar to this:
Using discounts
To add discounts to the basket/order, simply add an object implementing BasketDiscountInterface
to the basket and specify
the discount logic in applyToBasket
method.