File: //home/armgroup/libs/app/Http/Livewire/Frontend/Search/AddToCart.php
<?php
namespace App\Http\Livewire\Frontend\Search;
use App\Http\Livewire\ConvertEmptyStringsToNull;
use App\Http\Livewire\General;
use App\Models\Product;
use Gloudemans\Shoppingcart\Facades\Cart;
use Livewire\Component;
class AddToCart extends Component
{
use ConvertEmptyStringsToNull, General;
public Product $product;
public $optionPrice = null;
public $selectedValueId = null;
public $selectedOptionId = null;
public $optionName;
public $quantity;
public $rowId;
public $options;
public function mount()
{
$this->quantity = 0;
$this->product = $this->product->with('options')->first();
if (!empty($this->product->options) && count($this->product->options) && count($this->product->activeOptionValues)) {
$option = $this->product->options->first();
$this->selectedOptionId = $option->id;
$value = $this->product->activeOptionValues->where('option_id', $this->selectedOptionId)->first();
$this->selectedValueId = $value->id;
$this->optionPrice = $value->pivot->price;
$this->optionName = $option->name.': '.$value->name;
}
$this->setOptions();
if (Cart::count()) {
$cart = Cart::content()->where('id', $this->product->id);
if (count($this->options)) {
$cart = $cart->where('options.value_id', $this->selectedValueId);
}
$cart = $cart->first();
$this->quantity = $cart ? $cart->qty : 0;
$this->rowId = $cart?->rowId;
}
}
public function render()
{
return view('livewire.frontend.search.add-to-cart');
}
public function increment()
{
$this->quantity++;
$this->addToCart(1);
}
public function decrement()
{
if ($this->quantity > 1) {
$this->quantity--;
$this->addToCart(0);
} else {
$this->remove();
}
}
public function addToCart($is_increment)
{
if (! checkSaleStatus()) {
$this->doneMessage('در حال حاضر فروش محصولات غیرفعال میباشد، لطفا بعدا امتحان کنید.', 'warning');
return redirect()->route('index');
}
$price = ($this->product->special ?: $this->product->price) + $this->optionPrice;
$canBuyStatus = $this->product->canBuy($this->quantity, $this->options, $price);
if ($canBuyStatus == 'OK') {
if ($this->rowId && Cart::content()->search(fn ($cartItem) => $cartItem->rowId === $this->rowId)) {
Cart::update($this->rowId, ['qty' => $this->quantity]);
} else {
$without_special = $this->product->special ? ($this->product->price + $this->optionPrice) : 'none';
$cart = Cart::add([
'id' => $this->product->id,
'name' => $without_special, // Store without special price
'qty' => $this->quantity,
'price' => $price,
'options' => $this->options,
]);
$this->rowId = $cart->rowId;
}
$this->ajaxDoneMessage('با موفقیت به سبد خرید اضافه شد.');
$this->emit('cartChanged');
$this->emit('updateTotalCartPrice', Cart::subtotal(0), Cart::count());
} else {
$this->quantity = $is_increment ? $this->quantity - 1 : $this->quantity + 1;
$this->ajaxDoneMessage($canBuyStatus, 'error');
}
}
public function remove()
{
$this->quantity = 0;
if ($this->rowId && Cart::content()->search(fn ($cartItem) => $cartItem->rowId === $this->rowId)) {
Cart::remove($this->rowId);
$this->rowId = null;
}
$this->emit('cartChanged');
$this->emit('updateTotalCartPrice', Cart::subtotal(0), Cart::count());
}
private function setOptions()
{
$this->options = [];
if ($this->selectedValueId) {
$this->options = [
'option_id' => $this->selectedOptionId,
'value_id' => $this->selectedValueId,
'name' => $this->optionName,
];
}
}
}