1. Go to this page and download the library: Download soap/laravel-shopping-cart library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
soap / laravel-shopping-cart example snippets
return [
/*
|--------------------------------------------------------------------------
| Gross price as base price
|--------------------------------------------------------------------------
|
| This default value is used to select the method to calculate prices and taxes
| If true the item price is managed as a gross price, so taxes will be calculated by separation/exclusion
|
*/
'calculator' => \Soap\ShoppingCart\Calculation\DefaultCalculator::class,
/*
|--------------------------------------------------------------------------
| Default tax rate
|--------------------------------------------------------------------------
|
| This default tax rate will be used when you make a class implement the
| Taxable interface and use the HasTax trait.
|
*/
'tax' => 7,
/*
|--------------------------------------------------------------------------
| Shoppingcart database settings
|--------------------------------------------------------------------------
|
| Here you can set the connection that the shoppingcart should use when
| storing and restoring a cart.
|
*/
'database' => [
'connection' => null,
'table' => 'shopping_carts',
],
/*
|--------------------------------------------------------------------------
| Destroy the cart on user logout
|--------------------------------------------------------------------------
|
| When this option is set to 'true' the cart will automatically
| destroy all cart instances when the user logs out.
|
*/
'destroy_on_logout' => false,
/*
|--------------------------------------------------------------------------
| Default number format
|--------------------------------------------------------------------------
|
| This defaults will be used for the formatted numbers if you don't
| set them in the method call.
|
*/
'format' => [
'decimals' => 2,
'decimal_point' => '.',
'thousand_separator' => ',',
],
];
$rowId = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';
ShoppingCart::update($rowId, 2); // Will update the quantity
$rowId = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';
ShoppingCart::update($rowId, ['options' => ['size' => 'small']]); // Will update the size option with new value
ShoppingCart::update($rowId, ['name' => 'Product 1']); // Will update the name
ShoppingCart::update($rowId, $product); // Will update the id, name and price
namespace App\Models;
use Soap\Shoppingcart\Contracts\Buyable;
use Illuminate\Database\Eloquent\Model;
class Product extends Model implements Buyable {
use Soap\Shoppingcart\CanBeBought;
}
public function getBuyableIdentifier($options = null): int|string
{
return $this->id;
}
public function getBuyableDescription($options = null): string
{
return $this->name;
}
public function getBuyablePrice($options = null): float
{
return $this->price;
}
public function getBuyableWeight($options = null): float
{
return $this->weight;
}
namespace App\Models;
use Soap\Shoppingcart\Contracts\BuyableInterface;
use Illuminate\Database\Eloquent\Model;
class Product extends Model implements BuyableInterface {
public function getBuyableIdentifier($options = null): int|string
{
return $this->id;
}
public function getBuyableDescription($options = null): string
{
return $this->name;
}
public function getBuyablePrice($options = null): float
{
return $this->price;
}
public function getBuyableWeight($options = null): float
{
return $this->weight;
}
}
ShoppingCart::content()->count();
ShoppingCart::content()->groupBy('id');
ShoppingCart::instance('shopping')->add('192ao12', 'Product 1', 1, 9.99, 550);
// Get the content of the 'shopping' cart
ShoppingCart::content();
ShoppingCart::instance('wishlist')->add('sdjk922', 'Product 2', 1, 19.95, 550, ['size' => 'medium']);
// Get the content of the 'wishlist' cart
ShoppingCart::content();
// If you want to get the content of the 'shopping' cart again
ShoppingCart::instance('shopping')->content();
// And the count of the 'wishlist' cart again
ShoppingCart::instance('wishlist')->count();
// First we'll add the item to the cart.
$cartItem = ShoppingCart::add('293ad', 'Product 1', 1, 9.99, 550, ['size' => 'large']);
// Next we associate a model with the item.
ShoppingCart::associate($cartItem->rowId, 'Product');
// Or even easier, call the associate method on the CartItem!
$cartItem->associate('Product');
// You can even make it a one-liner
ShoppingCart::add('293ad', 'Product 1', 1, 9.99, 550, ['size' => 'large'])->associate('Product');
// Now, when iterating over the content of the cart, you can access the model.
foreach(ShoppingCart::content() as $row) {
echo 'You have ' . $row->qty . ' items of ' . $row->model->name . ' with description: "' . $row->model->description . '" in your cart.';
}
ShoppingCart::store('username');
// To store a cart instance named 'wishlist'
ShoppingCart::instance('wishlist')->store('username');
ShoppingCart::restore('username');
// To restore a cart instance named 'wishlist'
ShoppingCart::instance('wishlist')->restore('username');
// Merge the contents of 'savedcart' into 'username'.
ShoppingCart::instance('username')->merge('savedcart', $keepDiscount, $keepTaxrate, $dispatchAdd, 'savedcartinstance');
ShoppingCart::erase('username');
// To erase a cart switching to an instance named 'wishlist'
ShoppingCart::instance('wishlist')->erase('username');
namespace Soap\ShoppingCart\Calculation;
use Soap\ShoppingCart\CartItem;
use Soap\ShoppingCart\Contracts\CalculatorInterface;
class DefaultCalculator implements CalculatorInterface
{
public static function getAttribute(string $attribute, CartItem $cartItem)
{
$decimals = config('cart.format.decimals', 2);
switch ($attribute) {
case 'discount':
return $cartItem->price * ($cartItem->getDiscountRate() / 100);
case 'tax':
return round($cartItem->priceTarget * ($cartItem->taxRate / 100), $decimals);
case 'priceTax':
return round($cartItem->priceTarget + $cartItem->tax, $decimals);
case 'discountTotal':
return round($cartItem->discount * $cartItem->qty, $decimals);
case 'priceTotal':
return round($cartItem->price * $cartItem->qty, $decimals);
case 'subtotal':
return max(round($cartItem->priceTotal - $cartItem->discountTotal, $decimals), 0);
case 'priceTarget':
return round(($cartItem->priceTotal - $cartItem->discountTotal) / $cartItem->qty, $decimals);
case 'taxTotal':
return round($cartItem->subtotal * ($cartItem->taxRate / 100), $decimals);
case 'total':
return round($cartItem->subtotal + $cartItem->taxTotal, $decimals);
default:
return;
}
}
}
// Add some items in your Controller.
ShoppingCart::add('192ao12', 'Product 1', 1, 9.99);
ShoppingCart::add('1239ad0', 'Product 2', 2, 5.95, ['size' => 'large']);
// Display the content in a View.
<table>
<thead>
<tr>
<th>Product</th>
<th>Qty</th>
<th>Price</th>
<th>Subtotal</th>
</tr>
</thead>
<tbody>
foreach(ShoppingCart::content() as $row) :
namespace App;
...
use Illuminate\Foundation\Auth\User as Authenticatable;
use Soap\Shoppingcart\Contracts\InstanceIdentifier;
class User extends Authenticatable implements InstanceIdentifier
{
...
/**
* Get the unique identifier to load the Cart from
*
* @return int|string
*/
public function getInstanceIdentifier($options = null): int|string
{
return $this->email;
}
/**
* Get the unique identifier to load the Cart from
*
* @return int|string
*/
public function getInstanceGlobalDiscount($options = null): int|string
{
return $this->discountRate ?: 0;
}
}
// Inside Controller
$user = \Auth::user();
$cart = ShoppingCart::instance($user);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.