1, installation
To install the expansion pack through composer:
Composer require Syscover/shoppingcart Dev-master
Then go to the providers in config/app.php to register the service provider:
Syscover\shoppingcart\shoppingcartserviceprovider::class,
At the same time in the aliases registration façade:
' Cartprovider ' => syscover\shoppingcart\facades\cartprovider::class,
To publish the configuration file for the expansion pack to the Config directory:
PHP artisan vendor:publish--provider= "Syscover\shoppingcart\shoppingcartserviceprovider"
You can then run the PHPUnit unit test:
PHPUnit Tests/cartprovidertest
To test the expansion pack for problems.
2. Use
The ShoppingCart expansion pack provides us with a wealth of ways to operate our shopping carts.
Add to Shopping Cart
Add by using the Add method
Cartprovider::instance ()->add (' 293ad ', ' Product 1 ', 1, 9.99, array (' Size ' => ' large '));
Add by array
Cartprovider::instance ()-> ([' id ' => ' 293ad ', ' name ' => ' Product 1 ', ' qty ' => 1, ' Price ' => 9.99, ' Options ') => Array (' size ' => ' large ')]);
Bulk Add
Cartprovider::instance ()-> ([
Array (' ID ' => ' 293ad ', ' name ' => ' Product 1 ', ' qty ' => 1, ' Price ' => 10.00),
Array (' ID ' => ' 4832k ', ' name ' => ' Product 2 ', ' qty ' => 1, ' Price ' => 10.00, ' Options ' => array (' Size ' => ' Large '))
]);
Update Shopping Cart
$rowId = ' da39a3ee5e6b4b0d3255bfef95601890afd80709 ';
ROWID is the ID of the shopping Cart entry that represents the update, and the second argument can be a number that represents the number of updates, or an array that represents the updated property
Cartprovider::instance ()->update ($rowId, 2);
Cartprovider::instance ()->update ($rowId, Array (' name ' => ' Product 1 '));
Remove from Shopping cart
$rowId = ' da39a3ee5e6b4b0d3255bfef95601890afd80709 ';
Cartprovider::instance ()->remove ($rowId);
Get Shopping cart Data
Get the specified shopping cart entry data
$rowId = ' da39a3ee5e6b4b0d3255bfef95601890afd80709 ';
Cartprovider::instance ()->get ($rowId);
Get all the data for the shopping cart
Cartprovider::instance ()->content ();
Get the total price (not including freight)
Cartprovider::instance ()->subtotal ();
Get Total price (including freight)
Cartprovider::instance ()->total ();
Get total number of shopping cart entries
Cartprovider::instance ()->count ();
Search CART contains specified entry
Cartprovider::instance ()->search (array (' ID ' => 1, ' Options ' => array (' Size ' => ' L '));
Empty shopping Cart
Cartprovider::instance ()->destroy ();
For more use, refer to the Syscover\shoppingcart\libraries\cart class.
Multiple Shopping cart Instances
In addition, this expansion pack supports multiple shopping cart instances, and you can specify the instance name by Cart::instance (' newinstance '):
Cartprovider::instance (' shopping ')->add (' 192ao12 ', ' Product 1 ', 1, 9.99);
If you want to switch, specify the appropriate instance name:
Cart::instance (' wishlist ')->add (' sdjk922 ', ' Product 2 ', 1, 19.95, array (' Size ' => ' Medium '));
The default shopping Cart instance name is main, which is equivalent to using the main instance without specifying the instance name.
In this section we use a ready-made expansion pack to implement the shopping cart, and in the next section we will demonstrate how to create the shopping cart function in Laravel.