Adding Basket Info to Login API Response
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!
Always add your Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
- PaoloLegaspi
- Posts: 35
- Joined: 07 Nov 2024, 15:02
Adding Basket Info to Login API Response
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
userService
Aimeos version: aimeos headless
PHP version: 8.2
Environment: Mac
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(),
]);
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?
}
PHP version: 8.2
Environment: Mac
- PaoloLegaspi
- Posts: 35
- Joined: 07 Nov 2024, 15:02
Re: Adding Basket Info to Login API Response
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!
Thanks!
- PaoloLegaspi
- Posts: 35
- Joined: 07 Nov 2024, 15:02
Re: Adding Basket Info to Login API Response
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
Re: Adding Basket Info to Login API Response
Use the Basket frontend manager, get the basket and count the products and their quantity: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.
- 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,
give us a star
If you like Aimeos,

Re: Adding Basket Info to Login API Response
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: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.
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,
give us a star
If you like Aimeos,
