Skip to content

Commit

Permalink
fix: bug where you couldn't delete product from cart
Browse files Browse the repository at this point in the history
  • Loading branch information
CorwinDev committed May 25, 2024
1 parent 8c3efa9 commit 32d2ad7
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
7 changes: 3 additions & 4 deletions app/Livewire/Checkout/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function products()
$discount = 0;
$products = [];
if ($cart) {
foreach ($cart as $product2) {
foreach ($cart as $key => $product2) {
$product = Product::where('id', $product2['product_id'])->first();
$product->config = $product2['config'] ?? [];
$product->configurableOptions = $product2['configurableOptions'] ?? [];
Expand Down Expand Up @@ -132,7 +132,7 @@ public function products()
$this->tax->amount ?? $this->tax->amount += $price + $setupFee;
$discount += ($product->discount + $product->discount_fee) * $product->quantity;

$products[] = $product;
$products[$key] = $product;
}
}

Expand Down Expand Up @@ -193,10 +193,9 @@ public function updateQuantity($product, $value)
$this->updateCart();
}

public function removeProduct($product)
public function removeProduct($key)
{
$cart = session()->get('cart');
$key = array_search($product, array_column($cart, 'product_id'));
unset($cart[$key]);
session()->put('cart', $cart);
$this->updateCart();
Expand Down
4 changes: 2 additions & 2 deletions themes/default/views/livewire/checkout/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</tr>
</thead>
<tbody>
@foreach ($this->products as $product)
@foreach ($this->products as $key => $product)
<tr
class="{{ $loop->last ? '' : 'border-b-2 border-secondary-200 dark:border-secondary-50' }}">
<td class="pl-6 py-3">
Expand Down Expand Up @@ -60,7 +60,7 @@ class="w-16 border border-secondary-200 dark:border-secondary-50 dark:bg-seconda
{{ __('each') }}
@endif
</td>
<td class="py-3 pr-6" wire:click="removeProduct({{ $product->id }})">
<td class="py-3 pr-6" wire:click="removeProduct({{ $key }})">
<button class="button button-danger-outline">
<i class="ri-delete-bin-2-line"></i>
</button>
Expand Down

0 comments on commit 32d2ad7

Please sign in to comment.