Commit 673f2b43 authored by Artem's avatar Artem

fix bugs in update qty in cart method

parent 2b18faab
......@@ -13,6 +13,26 @@ class Cart /*extends Model*/
$this->total_price = 0;
}
public function updateTotalQty()
{
$result = 0;
foreach ($this->items as $item)
{
$result += $item['qty'];
}
$this->total_qty = $result;
}
public function updateTotalPrice()
{
$result = 0;
foreach ($this->items as $item)
{
$result += $item['qty'] * $item['item']->price;
}
$this->total_price = $result;
}
public function addItem($product, $id)
{
if (array_key_exists($id, $this->items)) {
......@@ -31,7 +51,20 @@ class Cart /*extends Model*/
public function updateQty($products_qty)
{
foreach ($products_qty as $id => $qty) {
$this->items[$id]['qty'] = $qty;
if (!$qty) {
$this->removeItem($id);
} else {
$this->items[$id]['qty'] = (int)$qty;
$this->updateTotalQty();
$this->updateTotalPrice();
}
}
}
public function removeItem($id)
{
unset($this->items[$id]);
$this->updateTotalQty();
$this->updateTotalPrice();
}
}
......@@ -12,6 +12,9 @@ class CartController extends Controller
{
public function index()
{
// testing
// Session::get('cart')->updateTotalPrice();
// endtest
if (!Session::has('cart')) {
return view('cart');
......
......@@ -16,7 +16,7 @@ function setUpdateCartQtyEventListener() {
productsQty: JSON.stringify(productsQty)
})
.then(function (response) {
document.location.reload(true);;
document.location.reload(true);
})
.catch(function (error) {
console.log(error);
......@@ -24,4 +24,9 @@ function setUpdateCartQtyEventListener() {
});
}
function setRemoveItemEventListener() {
}
setUpdateCartQtyEventListener();
setRemoveItemEventListener();
......@@ -16,7 +16,7 @@
</tr>
</thead>
<tbody>
<tbody class="">
@foreach($products as $product)
<tr>
<td>
......
......@@ -20,7 +20,7 @@
<p class="product__short-text">{{$product->description_short}}</p>
<p class="product__price">{{$product->price}} EUR</p>
<button class="js-btn-add-to-basket btn btn-info"
<button class="js-btn-add-to-basket btn btn-info text-white"
data-action="{{ route('cart.addToCart', $product) }}"
>Add to basket</button>
</div>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment