Shiva's avatar
Level 5

Getting the total quantity to be displayed

I'm trying to create an e-commerce site and the problem I'm having at the moment is that at the bottom of my cart the sub total price isn't displaying correctly.

Here is my product_details.blade.php

@extends('layouts.public')
@section('content')
    <div class="content_wrapper">
        @foreach($single_product as $product)
            <div class="row single_product_wrapper">
                <div class="col-lg-4 col-md-12 col-sm-12">
                    <?php
                        $product_image = getImagesArray($product->image);
                        $product_image2 = getImagesArray($product->image2);
                    ?>
                    
                    <div class="white-block">
                        <img src="{{ asset('img/white-block.jpg') }}">
                    </div>

                    <div class="slider-for main_product_slider">
                        @if(!empty($product_image))
                            @foreach($product_image as $image)
                                <div>
                                    <img src="{{ asset('product_images/products/'.$image) }}" alt="{{ $image }}">
                                </div>
                            @endforeach
                        @endif
                    </div>

                    <div class="slider-nav main_product_nav">
                        @if(!empty($product_image2))
                            @foreach($product_image2 as $image2)
                                <div>
                                    <img src="{{ asset('product_images/thumbs/'.$image2) }}" alt="{{ $image2 }}">
                                </div>
                            @endforeach
                        @endif
                    </div>
                </div>

                <div class="col-lg-8 col-md-12-col-sm-12">
                    <h1>
                        {{ $product->title }}
                    </h1>

                    {!! $product->description !!}


                    <!-- BEGIN ADD TO CART FORM -->
                    <form action="{{ route('product.sendToCart', ['id' => $product->id]) }}" method="POST">
                        {{ csrf_field() }}
                        <div>
                            <div class="quantity_box">
                                <div class="qty_number">
                                    <input type="text" value="1" name="product_qty">                            
                                </div>
                            </div>
                        </div>

                        {{ Form::submit('Save', array('class' => "btn btn-dark btn-lg btn-block")) }}
                    </form>
                    <!-- END ADD TO CART FORM -->
                </div>
            </div>

        @endforeach
    </div>
@stop

and here is my controller

public function sendToCart(Request $request, $id)
    {
        $product = Product::find($id);

        $oldCart = Session::has('cart') ? Session::get('cart') : null;

        $cart = new Cart($oldCart);
        $cart->addQty($product, $request->product_qty, $product->id);
        $request->session()->put('cart', $cart);

        return redirect()->back();
    }

My Cart.php

<?php

namespace App;


class Cart
{
    public $items = null;
    public $totalQty = 0;
    public $totalPrice = 0;

    public function __construct($oldCart)
    {
        if($oldCart)
        {
            $this->items = $oldCart->items;
            $this->totalQty = $oldCart->totalQty;
            $this->totalPrice = $oldCart->totalPrice;
        }
    }

    public function addQty($item, $qty, $id)
    {
        $storedItem = ['qty' => $qty, 'price' => $item->price, 'item' => $item];

        $storedItem['price'] = $item->price * $storedItem['qty'];
        $this->items[$id] = $storedItem;
        $this->totalQty++;
        $this->totalPrice += $storedItem['price'];
    }
}

and my cart.blade.php

@extends('layouts.public')
@section('content')
    <div class="content_wrapper">
        @if(Session::has('cart'))
            <div class="container">
                <div class="cart_table">
                    <div class="table">
                        <div class="thead">
                            <div class="tr">
                                <div class="th border-bottom">Image</div>
                                <div class="th border-bottom">&nbsp</div>
                                <div class="th border-bottom">Qty</div>
                                <div class="th border-bottom">Unit Price</div>
                                <div class="th border-bottom">Total Price</div>
                                <div class="th border-bottom"></div>
                            </div>
                        </div>

                        <div class="tbody">
                            @foreach($products as $product)
                                <?php
                                    $image = getImagesArray($product['item']['image']);
                                ?>

                                <div class="tr">
                                    <div class="td border-bottom" data-title="Image">
                                        @if(!empty($image))
                                            <img src={!! asset("product_images/thumbs/$image[0]") !!}>
                                        @endif
                                    </div>

                                    <div class="td border-bottom" data-title="Title">
                                        <strong>{{ $product['item']['title'] }}</strong>
                                    </div>

                                    <div class="td border-bottom" data-title="Qty">
                                        <div class="quantity_box">
                                            <div class="qty_number">
                                                <input type="text" value="{{ $product['qty'] }}">
                                                
                                                <div class="inc button">
                                                    <a href="{{ route('product.addToCart', ['id' => $product['item']['id']]) }}" class="">
                                                        +
                                                    </a>
                                                </div>

                                                <div class="dec button">
                                                    <a href="{{ route('product.removeFromCart', ['id' => $product['item']['id']]) }}" class="">
                                                        -
                                                    </a>
                                                </div>
                                            </div>
                                        </div>
                                    </div>

                                    <div class="td border-bottom" data-title="Unit Price">
                                        <span class="label label-success">R {{ $product['item']['price'] }}</span>
                                    </div>

                                    <div class="td border-bottom" data-title="Total Price">
                                        <span class="label label-success">R {{ $product['price'] }}</span>
                                    </div>
                                </div>
                            @endforeach
                        </div>
                    </div>
                </div>

                <div class="cart_subtotal text-right">
                    <div class="subtotal_text">
                        Subtotal
                    </div>

                    <div class="subtotal_price text-right">
                        {{ $totalPrice }}
                    </div>
                </div>
            </div>
        @else
            <div class="row">
                <div class="col-sm-6 col-md-6 col-md-offset-3 col-sm-offset-3">
                    <h2>No items in cart!</h2>
                </div>
            </div>
        @endif
    </div>
@stop
0 likes
3 replies
Vilfago's avatar

And what "not displaying correctly" mean ?

Not the right value ? The format is not what you expect (in this case : what did you expect) ? Something else ?

Shiva's avatar
Level 5

@VILFAGO - Sorry about that. The right value isn't showing. So for example: I have a t-shirt that's 20 and I bought 5, the sub total needs to show 100. But in this case it's showing 20

Shiva's avatar
Level 5

The issue I had was solved. I had to clear my cache

Please or to participate in this conversation.