1. Go to this page and download the library: Download rariteth/laravel-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/ */
rariteth / laravel-cart example snippets
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
// Instance 'whishlist' with guard 'web'
$this->app
->when(
[
Whishlist\ManageController::class,
Whishlist\CheckoutController::class
]
)
->needs(CartRepositoryInterface::class)
->give(function () {
return new CartRepository(new CartInstance('whishlist', 'web'));
});
// Instance 'other-cart' with guard 'frontend'
$this->app
->when(OtherCartController::class)
->needs(CartInstanceInterface::class)
->give(function () {
return new CartInstance('other-cart', 'frontend');
});
...
class CartController extends Controller
{
/**
* @var CartRepositoryInterface
*/
private $cartRepository;
/**
* CartController constructor.
*
* @param CartRepositoryInterface $cartRepository
*/
public function __construct(CartRepositoryInterface $cartRepository)
{
$this->cartRepository = $cartRepository;
}
/**
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function index()
{
$items = $this->cartRepository->getItems();
return view('cart.index', compact('items'));
}
/**
* @param Product $product
*
* @return \Illuminate\Http\RedirectResponse
*/
public function add(Product $product)
{
$qty = 1;
$this->cartRepository->add($product, $qty);
return redirect()->route('cart.index');
}
/**
* @return \Illuminate\Http\RedirectResponse
*/
public function clear()
{
$this->cartRepository->clear();
return redirect()->route('cart.index');
}
/**
* @param string $rowId
*
* @return \Illuminate\Http\RedirectResponse
*/
public function remove(string $rowId)
{
if ($cartItem = $this->cartRepository->get($rowId)) {
$this->cartRepository->remove($cartItem);
return redirect()->route('cart.index');
}
return abort(404);
}
/**
* @param string $rowId
* @param int $qty
*
* @return \Illuminate\Http\RedirectResponse
*/
public function updateQty(string $rowId, int $qty)
{
if ($cartItem = $this->cartRepository->get($rowId)) {
$cartItem->setQty($qty);
$this->cartRepository->update($cartItem);
return redirect()->route('cart.index');
}
return abort(404);
}
/**
* @param CartRepositoryInterface $cartRepository
*/
public function refreshCartItems(CartRepositoryInterface $cartRepository): void
{
$shouldRefreshItems = $cartRepository->search(function (CartItem $cartItem) {
return $this->shouldRefreshCartItem($cartItem);
});
$cartRepository->refresh($shouldRefreshItems);
}
/**
* @param CartRepositoryInterface $cartRepository
*/
public function refreshCartItems(CartRepositoryInterface $cartRepository): void
{
$shouldRefreshItems = $cartRepository->getGuestItems();
$cartRepository->refresh($shouldRefreshItems);
}
/**
* @param CartRepositoryInterface $cartRepository
*/
public function removeOldCartItems(CartRepositoryInterface $cartRepository): void
{
$itemsForRemove = $cartRepository->search(function (CartItem $cartItem) {
return $this->shouldRemoveCartItem($cartItem);
});
$cartRepository->removeBatch($itemsForRemove);
}
/**
* @param CartItem $cartItem
*
* @return bool
*/
private function shouldRefreshCartItem(CartItem $cartItem): bool
{
return $cartItem->updatedAt < $this->expireAt()->subDay();
}
/**
* @param CartItem $cartItem
*
* @return bool
*/
private function shouldRemoveCartItem(CartItem $cartItem): bool
{
return $cartItem->addedAt < now()->subDays(5);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.