1

Get wishlist of customer using Magento 2 API.

I refereed Magento Dev but, I'm not able to find wishlist for customer.

  • Wishlist module doesn't have REST API – Evgeny Levinsky Jun 11 '18 at 15:45
3

Please find the Below Answer For Your Question for customer Wishlist.

To create webapi in Magento 2, we need to create a file in

app/code/ExtensionName/ModuleName/etc/webapi.xml

in Webapi.xml Place below code.

<?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

Then in di.xml Place below code.

<?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>

As Per the above di.xml file Create HelloInterface adn Hello

app/code/ExtensionName/ModuleName/Api/HelloInterface.php

Replace the code in 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);
}

Next Create Hello.php in below Path

app/code/ExtensionName/ModuleName/Model/Hello.php

Place below code in 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;  
        }
}

For More reference please find the below screen shot.

enter image description here

  • hello i am getting fallowing error. <message>%fieldName is a required field.</message> – Sarvesh Tiwari Mar 26 at 10:49
  • You have to mention resource reference as "anonymous" like <resource ref="anonymous"/> @SarveshTiwari – Learing_Coder Mar 29 at 15:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy