Adding Basket Info to Login API Response

How to configure and adapt Aimeos based shops as developer
Forum rules
Always add your Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
User avatar
PaoloLegaspi
Posts: 35
Joined: 07 Nov 2024, 15:02

Adding Basket Info to Login API Response

Post by PaoloLegaspi » 18 Nov 2024, 10:37

Hi,
I’ve set up a login API for customers, and I’d like to include additional data in the JSON response. Specifically, I want to include the basket items and the basket item count. I noticed that basket items are tied to the user session. Is there a way to include these in the response after the user successfully logs in? The main reason behind this is we want to reduce the amounts of requests, and we would want to have the counts and data saved in the local storage in the frontend when user a successfully logs in.

Thanks for the help!

Login response

Code: Select all

return $this->sendResponse('Login successful', [
            'access_token' => $token,
            'token_type' => 'bearer',
            'expires_in' => auth('api')->factory()->getTTL() * 60,
            'user' => $user,
            'favorites' => $this->userService->getFavorites($user->id),
            'cart_no' => $this->userService->getBasket(),
        ]);
userService

Code: Select all

    public function __construct(AimeosContext $context)
    {
        $this->userManager = \Aimeos\MShop::create( $context->get(), 'customer' );
        $this->basketManager = \Aimeos\MShop::create( $context->get(), 'order/basket' );
    }
    
     public function getFavorites(int $userId)
    {
        $ref = ['product'];
        $items = $this->userManager->get($userId, $ref)->getListItems($ref);

        return $items ?? [];
    }

    public function getBasket()
    {
        // how can I get it?
    }
    
Aimeos version: aimeos headless
PHP version: 8.2
Environment: Mac

User avatar
PaoloLegaspi
Posts: 35
Joined: 07 Nov 2024, 15:02

Re: Adding Basket Info to Login API Response

Post by PaoloLegaspi » 18 Nov 2024, 12:16

Update: I only need the cart item count in the login response now. What’s the best way to include that? This will be used in the cart count in our react frontend.

Thanks!

User avatar
PaoloLegaspi
Posts: 35
Joined: 07 Nov 2024, 15:02

Re: Adding Basket Info to Login API Response

Post by PaoloLegaspi » 18 Nov 2024, 14:14

Is there an endpoint just for getting the basket count? I noticed there’s a query to fetch only the basket/products, but it still gets in address and service info as well.

Code: Select all

http://localhost:8000/jsonapi/basket?included=basket/product&fields[basket]=order.base.price&fields[basket/product]=order.base.product.name,order.base.product.mediaurl,order.base.product.price

User avatar
aimeos
Administrator
Posts: 8633
Joined: 01 Jan 1970, 00:00

Re: Adding Basket Info to Login API Response

Post by aimeos » 19 Nov 2024, 09:21

PaoloLegaspi wrote: 18 Nov 2024, 12:16 Update: I only need the cart item count in the login response now. What’s the best way to include that? This will be used in the cart count in our react frontend.
Use the Basket frontend manager, get the basket and count the products and their quantity:
- https://github.com/aimeos/ai-controller ... ce.php#L43
- https://github.com/aimeos/aimeos-core/b ... e.php#L361
- https://github.com/aimeos/aimeos-core/b ... /Iface.php
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

User avatar
aimeos
Administrator
Posts: 8633
Joined: 01 Jan 1970, 00:00

Re: Adding Basket Info to Login API Response

Post by aimeos » 19 Nov 2024, 16:00

PaoloLegaspi wrote: 18 Nov 2024, 14:14 Is there an endpoint just for getting the basket count? I noticed there’s a query to fetch only the basket/products, but it still gets in address and service info as well.
There's no endpoint for the product count in the basket only but this is the minimal response which you can use to sum up the product count yourself:

Code: Select all

/jsonapi/basket?include=basket.product&fields[basket]=order.price&fields[basket.product]=order.product.quantity&pretty=true
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply