File: /home/armgroup/libs/app/Http/Livewire/Frontend/Product/Info.php
<?php
namespace App\Http\Livewire\Frontend\Product;
use App\Http\Livewire\ConvertEmptyStringsToNull;
use App\Http\Livewire\General;
use App\Models\Option;
use App\Models\OptionProduct;
use App\Models\OptionValue;
use App\Models\Product;
use App\Models\ProductDemand;
use Gloudemans\Shoppingcart\Facades\Cart;
use Livewire\Component;
class Info extends Component
{
use ConvertEmptyStringsToNull, General;
public Product $product;
public $price = null;
public $special = null;
public $stock = null;
public $selectedValueId = null;
public $selectedOptionId = null;
public $optionName;
public $quantity;
public function mount()
{
$this->quantity = 1;
$optionProduct = OptionProduct::query()
->where('product_id', $this->product->id)
->first();
if ($optionProduct) {
$this->selectedOptionId = $optionProduct->option_id;
$this->selectedValueId = $optionProduct->value_id;
// $this->price = $optionProduct->price;
// $this->special = $optionProduct->special_mask;
$this->price = ($this->product->price) + $optionProduct->price;
$this->special = $this->product->special + $optionProduct->price;
$this->stock = $optionProduct->stock;
$option = Option::find($this->selectedOptionId);
$value = OptionValue::find($this->selectedValueId);
$this->optionName = $option->name.': '.$value->name;
} else {
$this->price = $this->product->price;
$this->special = $this->product->special_mask;
$this->stock = $this->product->stock;
}
}
public function increment()
{
$this->quantity++;
}
public function decrement()
{
if ($this->quantity > 1) {
$this->quantity--;
}
}
public function render()
{
return view('livewire.frontend.product.info');
}
protected $listeners = ['optionChanged'];
public function optionChanged($option_id, $value_id)
{
$this->selectedOptionId = $option_id;
$this->selectedValueId = $value_id;
$optionProduct = OptionProduct::query()
->where('product_id', $this->product->id)
->where('value_id', $value_id)
->where('option_id', $option_id)
->first();
$option = Option::find($this->selectedOptionId);
$value = OptionValue::find($this->selectedValueId);
$this->optionName = $option->name.': '.$value->name;
// $this->price = $optionProduct->price;
// $this->special = $optionProduct->special_mask;
$this->price = ($this->product->special ?: $this->product->price) + $optionProduct->price;
$this->special = $this->product->special + $optionProduct->price;
$this->stock = $optionProduct->stock;
}
public function addToCart()
{
if (! checkSaleStatus()) {
$this->doneMessage('در حال حاضر فروش محصولات غیرفعال میباشد، لطفا بعدا امتحان کنید.', 'warning');
return redirect()->route('index');
}
$this->quantity = (((int) $this->quantity) > 0) ? (int) $this->quantity : 1;
$options = $this->createOptions();
$cart = $this->checkCart($options);
$canBuyStatus = $this->product->canBuy($this->quantity, $options, $this->special ?: $this->price);
if ($canBuyStatus == 'OK') {
if ($cart) {
Cart::update($cart->rowId, ['qty' => $this->quantity]);
} else {
Cart::add([
'id' => $this->product->id,
'name' => $this->special ? $this->price : 'none', // Store without special price
'qty' => $this->quantity,
'price' => $this->special ?: $this->price,
'options' => $options,
]);
}
$this->ajaxDoneMessage('با موفقیت به سبد خرید اضافه شد.');
$this->emit('cartChanged');
$this->emit('updateTotalCartPrice', Cart::subtotal(0), Cart::count());
} else {
$this->ajaxDoneMessage($canBuyStatus, 'error');
}
}
private function checkCart(array $options)
{
if (Cart::count()) {
$cart = Cart::content()->where('id', $this->product->id);
if (count($options)) {
$cart = $cart->where('options.value_id', $this->selectedValueId);
}
return $cart->first();
}
return null;
}
private function createOptions(): array
{
$options = [];
if ($this->selectedValueId) {
$options = [
'option_id' => $this->selectedOptionId,
'value_id' => $this->selectedValueId,
'name' => $this->optionName,
];
}
return $options;
}
public function demand()
{
if (! $this->canDemand()) {
$this->ajaxDoneMessage('برای اطلاع از موجود شدن این محصول ابتدا باید وارد حساب کاربری خود شوید', 'error');
return;
}
if (auth()->user()->hasDemanded($this->product->id, $this->selectedValueId)) {
$this->ajaxDoneMessage('اطلاعرسانی موجود شدن این محصول برای شما غیرفعال شد', 'info');
ProductDemand::where('user_id', auth()->id())
->where('product_id', $this->product->id)
->when($this->selectedValueId, function ($query) {
return $query->where('value_id', $this->selectedValueId);
})->delete();
} else {
$this->ajaxDoneMessage('اطلاعرسانی موجود شدن این محصول برای شما فعال شد');
ProductDemand::create(['user_id' => auth()->id(), 'product_id' => $this->product->id, 'value_id' => $this->selectedValueId]);
}
}
private function canDemand(): bool
{
return $this->stock == 0 && auth()->check();
}
}