1. Go to this page and download the library: Download abo3adel/shoppingcart 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/ */
abo3adel / shoppingcart example snippets
composer
php artisan vendor:publish --tag=shoppingcart-all
update config/shoppingcart.php with your configration
and then
php artisan migrate
namespace Abo3adel\ShoppingCart\Tests\Model;
use Abo3adel\ShoppingCart\Contracts\CanBeBought;
use Abo3adel\ShoppingCart\Traits\Buyable;
use Illuminate\Database\Eloquent\Model;
class SpaceCraft extends Model implements CanBeBought
{
use Buyable;
public function getPrice(): float
{
return $this->price;
}
public function getDiscount(): float
{
return $this->discount;
}
//
}
Cart::add()
cart()->add()
// all options
Cart::add(
CanBeBought $buyable,
int $qty,
mixed $opt1, // see config/shoppingcart.php to change this
mixed $opt2, // see config/shoppingcart.php to change this
array $options
)
// only price and qty
Cart::add($buyable, $qty)
// only price && qty && options array
// use this if you do not use any of (opt1 or opt2)
Cart::add($buyable, $qty, ['weight' => 250])
// if you do not use opt2
Cart::add($buyable, $qty, $opt1, ['weight' => 250])
// by item ID
Cart::find($itemId)
// by buyable model id
Cart::find(int $buyable_id, string $buyable_type)
Cart::find(25, App\Book::class)
// all args
Cart::add(
int $itemId,
int $qty,
mixed $opt1, // see config/shoppingcart.php to change this
mixed $opt2, // see config/shoppingcart.php to change this
array $options
)
// only qty
Cart::update($itemId, $qty)
// only options
Cart::update($itemId, $options)
// if no (opt1 || opt2)
Cart::update($itemId, $qty, $options)
// if no opt2
Cart::update($itemId, $qty, $opt1, $options)
Cart::delete(int $itemId)
// by item id
Cart::has($itemId)
// by buyable id
Cart::has($buyable_id, $buyable_type)
Cart::has(5, 'App\Product')
Cart::content()
Cart::content()->count() // get count
Cart::content()->search() // search
Cart::content()->each() // loop
// or any collection method
// add to cart in the default instance
$buyable->addToCart($qty, $opt1, $opt2, $options)
// remove from cart in the default instance
$buyable->removeFromCart()
// get list of all cart items associated with this model
$buyable->items()
// get the subTotal price after discount substract
$buyable->getSubPrice()
// $admin is logged in
// user here is admin
Cart::forUser($user)
// user here is $user
Cart::add($buyable, $qty)
// reset user and return to logged in admin
Cart::resetUser()
// user here is $admin again