Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't use "View::Share" to display items cart count #259

Closed
christophe-44-27 opened this issue Oct 7, 2019 · 8 comments
Closed

Can't use "View::Share" to display items cart count #259

christophe-44-27 opened this issue Oct 7, 2019 · 8 comments

Comments

@christophe-44-27
Copy link

Hi,
I would like to display the number of items from my cart, into my navigation bar. I want to use AppServiceProvider to facilitate my work but something weird appeared.

I don't know why, but when i use "view::share" into AppServiceProvider, my cart is empty (but my cart session is always filled).

Maybe developping an helper to count the items number could be a nice thing to have..

Regards,
Christophe

@lukepolo
Copy link
Owner

lukepolo commented Oct 7, 2019

You can use LaraCart::count() , if you want the QTY's to be added to the count you can LaraCart::count(true)

@christophe-44-27
Copy link
Author

Well same thing, it clear my cart :/

@lukepolo
Copy link
Owner

lukepolo commented Oct 7, 2019

I just tested this :

image

{{ dump(LaraCart::get()->cart) }}

Total
{{ LaraCart::total() }}

Total Items
{{ LaraCart::count() }}
<br><br>
<form action="/cart/add">
    <button>Add Random Item</button>
</form>

<form action="/cart/fee">
    <button>Add $5.00 Delivery Fee</button>
</form>

<form action="/cart/destroy">
    <button>Destroy</button>
</form>

@christophe-44-27
Copy link
Author

christophe-44-27 commented Oct 7, 2019

Ok, so it's nearly work, in fact, it's "LaraCart::getItems()" that doesn't work in my way.

This works (but the counter return 0)

//In AppServiceProvider
View::share('cart_items', LaraCart::count());

$items = LaraCart::get()->cart->items;
return view('customer.cart.cart', compact('items'));

This doesn't works

//In AppServiceProvider
View::share('cart_items', LaraCart::count());

$items = LaraCart::getItems();
return view('customer.cart.cart', compact('items'));

@lukepolo
Copy link
Owner

lukepolo commented Oct 7, 2019

If you try to use share in the boot method its too early, and the cart has not been loaded yet. You can manually load the cart though to make it work in a middleware :

<?php

namespace App\Http\Middleware;

use Closure;
use LukePOLO\LaraCart\Facades\LaraCart;

class LoadCart
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        view()->share('totalItems', $LaraCart::count());
        return $next($request);
    }
}

@christophe-44-27
Copy link
Author

Oh ok ! Perfect, you're solution works :D thank you for your fast support ^^

@lukepolo
Copy link
Owner

lukepolo commented Oct 7, 2019

No problem, i don't think that is the best solution, you should be able to do it after the cart has loaded. You may be able todo it in the register method rather than the boot method.

@lukepolo
Copy link
Owner

lukepolo commented Oct 7, 2019

Or mabye use a view composer instead

https://laravel.com/docs/master/views#view-composers

@lukepolo lukepolo closed this as completed Oct 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants