Ecommerce
Examples
Ecommerce
Examples
Full component examples
Here’s a set of Livewire components.
Cart component:
<?php
namespace App\Livewire;
use Filament\Actions\Action;
use Filament\Actions\Concerns\InteractsWithActions;
use Filament\Actions\Contracts\HasActions;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Notifications\Notification;
use Filament\Support\Enums\Alignment;
use Firepit\Cms\Models\ShopDiscountCode;
use Livewire\Component;
use LukePOLO\LaraCart\Coupons\Fixed as Coupon;
use LukePOLO\LaraCart\Facades\LaraCart;
class Cart extends Component implements HasActions, HasForms
{
use InteractsWithActions, InteractsWithForms;
public $discount = '';
public function applyDiscount(): void
{
if (! $this->discount) {
return;
}
$this->validate([
'discount' => [
'required',
'string',
function ($attribute, $value, $fail) {
if (! ShopDiscountCode::whereCode($value)->exists()) {
$fail('Deze kortingscode is niet geldig.');
}
},
],
]);
$code = ShopDiscountCode::whereCode($this->discount)->firstOrFail();
$this->removeDiscount();
if($code->percentage){
$discount = round(($code->percentage / 100) * LaraCart::total(false, false), 3);
$coupon = new Coupon($code->percentage, $discount);
}else{
$discount = $code->price;
$coupon = new Coupon($code->price, $discount);
}
LaraCart::addCoupon($coupon);
session()->put('discount', [
'discount' => $code->code,
'percentage' => $code->percentage,
'total_discount' => $discount,
]);
$this->discount = '';
}
public function removeDiscount(): void
{
LaraCart::removeCoupons();
session()->forget('discount');
$this->discount = '';
}
public function removeDiscountAction(): Action
{
return Action::make('removeDiscount')
->requiresConfirmation()
->color('danger')
->link()
->label('Verwijder')
->modalHeading('Korting verwijderen')
->modalIcon('heroicon-o-banknotes')
->modalAlignment(Alignment::Left)
->action(function () {
$this->removeDiscount();
});
}
public function removeItemAction(): Action
{
return Action::make('removeItem')
->requiresConfirmation()
->color('danger')
->hiddenLabel()
->tooltip('Verwijder dit artikel uit de winkelwagen')
->icon('heroicon-o-trash')
->modalHeading('Item verwijderen uit winkelwagen')
->modalIcon('heroicon-o-shopping-cart')
->modalAlignment(Alignment::Left)
->action(function (array $arguments) {
LaraCart::removeItem($arguments['item']);
LaraCart::removeCoupons();
session()->forget('discount');
Notification::make('removed')
->title('Winkelwagen')
->body('Item is verwijderd uit je winkelmand')
->success()
->send();
});
}
public function render()
{
return view('livewire.cart');
}
}
On this page