How can get the user wish list items in Magento 2 REST API
Hello @pratap_penmetsa
Create your custom API to get the customer wishlist using an API.
app/code/ExtensionName/ModuleName/etc/webapi.xml
<?xml version="1.0"?> <routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd"> <route url="/V1/wishlist/product/:customerid" method="GET"> <service class="ExtensionName\ModuleName\Api\HelloInterface" method="displayWishlist"/> <resources> <resource ref="anonymous"/> </resources> </route> </routes>
app/code/ExtensionName/ModuleName/etc/di.xml
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="ExtensionName/ModuleName\Api\HelloInterface" type="ExtensionName/ModuleName\Model\Hello" /> </config>
app/code/ExtensionName/ModuleName/Api/HelloInterface.php
<?php namespace ExtensionName\ModuleName\Api; interface HelloInterface { /** * Returns greeting message to user * * @api * @param int customer id * @return mixed */ public function displayWishlist($customer_id); }
app/code/ExtensionName/ModuleName/Model/Hello.php
<?php namespace ExtensionName\ModuleName\Model; use ExtensionName\ModuleName\Api\HelloInterface; class Hello implements HelloInterface { private $customer_id; private $wishlist; public function __construct( \Magento\Wishlist\Model\Wishlist $wishlist ) { $this->wishlist = $wishlist; } public function displayWishlist($customer_id) { $customer_id = 1; $wishlist_collection = $this->wishlist->loadByCustomerId($customer_id, true)->getItemCollection(); $wishlist_item = array(); $i=1; foreach ($wishlist_collection as $item) { $productName = $item->getProduct()->getName(); $productId = $item->getProduct()->getId(); $WishlistItemId = $item->getWishlistItemId(); $wishlist_item[$i]['productName'] = $productName; $wishlist_item[$i]['productId'] = $productId; $wishlist_item[$i]['WishlistItemId'] = $WishlistItemId; $i++; } return $wishlist_item; } }
Reference: https://magento.stackexchange.com/a/229478/48571
@Mayur Bhuva i am working on react-native and new to magento, i want REST API to get customer wish list. which end point should i use to get customer wish list
Hello @pratap_penmetsa,
Magento didn't provide wishlist Rest api for it so you have to create one module for create Rest API for wishlist data retrieve and after you will get wishlist endpoint URL for it.
--
If my answer is useful, please Accept as Solution & give Kudos