[Solved] Wishlistlisting a product through ajax call

Help for integrating the Laravel package
Forum rules
Always add your Laravel, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
kartikbhat
Posts: 40
Joined: 02 Dec 2021, 17:18

[Solved] Wishlistlisting a product through ajax call

Post by kartikbhat » 13 Apr 2023, 03:44

How can I use an ajax call to add a product to wishlist ?
again I need to show them under a profile section

Kindly let me know how to add it via ajax and fetch on page load ?

TIA :)
Last edited by kartikbhat on 24 Apr 2023, 02:47, edited 1 time in total.

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

Re: Wishlistlisting a product through ajax call

Post by aimeos » 17 Apr 2023, 08:27

Use the customer relations resource from the JSON API:
https://aimeos.org/docs/latest/frontend ... relations/
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

kartikbhat
Posts: 40
Joined: 02 Dec 2021, 17:18

Re: Wishlistlisting a product through ajax call

Post by kartikbhat » 19 Apr 2023, 15:33

How can I insert Favorite product from laravel controller ?

tried following approach

Code: Select all

$favoriteData = [
                        'token' => csrf_token(),
                        'fav_action'=>$favorite['fav_action'],
                        'fav_id'=>$favorite['fav_id'],
                        'd_name'=>$favorite['d_name'],
                        'd_prodid'=>$favorite['d_prodid']
 ];
$request = Request::create($postUrl, 'POST',$favoriteData);
$request->headers->set('X-CSRF-TOKEN', csrf_token());
$request->headers->set('Content-Type', 'application/json');
$response = Route::dispatch($request);

response returning 302 status code


help me to find correct approach

TIA :)

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

Re: Wishlistlisting a product through ajax call

Post by aimeos » 21 Apr 2023, 14:42

Don't use a network request if you can use the PHP API instead!

In a Laravel controller use:

Code: Select all

$context = app('aimeos.context')->get();
$manager = \Aimeos\MShop::create( $context, 'customer' );

// if you know that the customer doesn't have that item as favorite yet:
$item = $manager->get( '<customer ID>' );
$item->addListItem( 'product', $manager->createListItem()->setType( 'favorite' )->setRefId( '<product ID>' ) );

// else if you need to check if the product is already a favorite of the customer:
$item = $manager->get( '<customer ID>', ['product' => ['favorite']] );
if( $item->getListItem( 'product', 'favorite', '<product ID>', false ) === null ) {
	$item->addListItem( 'product', $manager->createListItem()->setType( 'favorite' )->setRefId( '<product ID>' ) );
}

$manager->save( $item );
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

kartikbhat
Posts: 40
Joined: 02 Dec 2021, 17:18

Re: Wishlistlisting a product through ajax call

Post by kartikbhat » 24 Apr 2023, 02:47

Thank you for this solution :)

Post Reply