I want REST api for "Add to Wishlist" in Magento 2, and also need to know in which folder to place and the endpoint
4 Answers
- Create a module Test_Mobileshop
Please follow the below steps with the file paths
2.app/code/Test/Mobileshop/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/items" method="GET">
<service class="Test\Mobileshop\Api\WishlistManagementInterface" method="getWishlistForCustomer"/>
<resources>
<resource ref="anonymous" />
</resources>
<data>
<parameter name="customerId" force="true">%customer_id%</parameter>
</data>
</route>
<route url="/V1/wishlist/add/:productId" method="POST">
<service class="Test\Mobileshop\Api\WishlistManagementInterface" method="addWishlistForCustomer"/>
<resources>
<resource ref="anonymous" />
</resources>
<data>
<parameter name="customerId" force="true">%customer_id%</parameter>
</data>
</route>
</routes>
- app/code/Test/Mobileshop/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="Test\Mobileshop\Api\WishlistManagementInterface" type="Test\Mobileshop\Model\WishlistManagement" />
</config>
- app/code/Test/Mobileshop/Api/WishlistManagementInterface.php
<?php
/**
* A Magento 2 module named Test/Mobileshop
*
*/
namespace Test\Mobileshop\Api;
/**
* Interface WishlistManagementInterface
* @api
*/
interface WishlistManagementInterface
{
/**
* Return Wishlist items.
*
* @param int $customerId
* @return array
*/
public function getWishlistForCustomer($customerId);
/**
* Return Added wishlist item.
*
* @param int $customerId
* @param int $productId
* @return array
*
*/
public function addWishlistForCustomer($customerId,$productId);
}
- app/code/Test/Mobileshop/Model/WishlistManagement.php
<?php
/**
* A Magento 2 module named Test/Mobileshop
*
*/
namespace Test\Mobileshop\Model;
use Test\Mobileshop\Api\WishlistManagementInterface;
use Magento\Wishlist\Controller\WishlistProvider;
use Magento\Wishlist\Model\ResourceModel\Item\CollectionFactory;
use Magento\Wishlist\Model\WishlistFactory;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Catalog\Helper\ImageFactory as ProductImageHelper;
use Magento\Store\Model\App\Emulation as AppEmulation;
/**
* Defines the implementaiton class of the WishlistManagementInterface
*/
class WishlistManagement implements WishlistManagementInterface
{
/**
* @var CollectionFactory
*/
protected $_wishlistCollectionFactory;
/**
* Wishlist item collection
*
* @var \Magento\Wishlist\Model\ResourceModel\Item\Collection
*/
protected $_itemCollection;
/**
* @var WishlistRepository
*/
protected $_wishlistRepository;
/**
* @var ProductRepository
*/
protected $_productRepository;
/**
* @var WishlistFactory
*/
protected $_wishlistFactory;
/**
* @var Item
*/
protected $_itemFactory;
/**
* @var \Magento\Customer\Model\Customer
*/
protected $_customer;
/**
*@var \Magento\Catalog\Helper\ImageFactory
*/
protected $productImageHelper;
/**
*
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $storemanagerinterface;
/**
*@var \Magento\Store\Model\App\Emulation
*/
protected $appEmulation;
/**
*@var \Magento\Catalog\Model\Product
*/
protected $_productload;
/**
*@var \Magento\Directory\Model\CountryFactory
*/
protected $countryfactory;
/**
* @param CollectionFactory $wishlistCollectionFactory
* @param \Magento\Catalog\Model\ProductFactory $productFactory
* @param \Magento\Framework\Math\Random $mathRandom
* @param \Magento\Customer\Model\Customer $customer
* @param \Magento\Framework\Stdlib\DateTime $dateTime
* @param ProductRepositoryInterface $productRepository
*/
public function __construct(
CollectionFactory $wishlistCollectionFactory,
WishlistFactory $wishlistFactory,
\Magento\Customer\Model\Customer $customer,
AppEmulation $appEmulation,
\Magento\Directory\Model\CountryFactory $countryfactory,
\Magento\Store\Model\StoreManagerInterface $storemanagerinterface,
ProductImageHelper $productImageHelper,
\Magento\Catalog\Model\Product $productload,
\Magento\Wishlist\Model\WishlistFactory $wishlistRepository,
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
\Magento\Wishlist\Model\ItemFactory $itemFactory
) {
$this->_wishlistCollectionFactory = $wishlistCollectionFactory;
$this->_wishlistRepository = $wishlistRepository;
$this->_productRepository = $productRepository;
$this->_wishlistFactory = $wishlistFactory;
$this->countryfactory = $countryfactory;
$this->storemanagerinterface = $storemanagerinterface;
$this->_itemFactory = $itemFactory;
$this->_customer = $customer;
$this->_productload = $productload;
$this->appEmulation = $appEmulation;
$this->productImageHelper = $productImageHelper;
$this->_customer = $customer;
}
/**
* Get wishlist collection
* @deprecated
* @param $customerId
* @return WishlistData
*/
public function getWishlistForCustomer($customerId)
{
if (empty($customerId) || !isset($customerId) || $customerId == "") {
throw new InputException(__('Id required'));
} else {
$collection =
$this->_wishlistCollectionFactory->create()
->addCustomerIdFilter($customerId);
$baseurl = $this->storemanagerinterface->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA).'catalog/product';
$wishlistData = [];
foreach ($collection as $item) {
$productInfo = $item->getProduct()->toArray();
if ($productInfo['small_image'] == 'no_selection') {
$currentproduct = $this->_productload->load($productInfo['entity_id']);
$imageURL = $this->getImageUrl($currentproduct, 'product_base_image');
$productInfo['small_image'] = $imageURL;
$productInfo['thumbnail'] = $imageURL;
}else{
$imageURL = $baseurl.$productInfo['small_image'];
$productInfo['small_image'] = $imageURL;
$productInfo['thumbnail'] = $imageURL;
}
$data = [
"wishlist_item_id" => $item->getWishlistItemId(),
"wishlist_id" => $item->getWishlistId(),
"product_id" => $item->getProductId(),
"store_id" => $item->getStoreId(),
"added_at" => $item->getAddedAt(),
"description" => $item->getDescription(),
"qty" => round($item->getQty()),
"product" => $productInfo
];
$wishlistData[] = $data;
}
return $wishlistData;
}
}
/**
* Add wishlist item for the customer
* @param int $customerId
* @param int $productIdId
* @return array|bool
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function addWishlistForCustomer($customerId, $productId)
{
if ($productId == null) {
throw new LocalizedException(__
('Invalid product, Please select a valid product'));
}
try {
$product = $this->_productRepository->getById($productId);
} catch (NoSuchEntityException $e) {
$product = null;
}
try {
$wishlist = $this->_wishlistRepository->create()->loadByCustomerId
($customerId, true);
$wishlist->addNewItem($product);
$returnData = $wishlist->save();
} catch (NoSuchEntityException $e) {
}
return true;
}
/**
* Helper function that provides full cache image url
* @param \Magento\Catalog\Model\Product
* @return string
*/
public function getImageUrl($product, string $imageType = ''){
$storeId = $this->storemanagerinterface->getStore()->getId();
$this->appEmulation->startEnvironmentEmulation($storeId, \Magento\Framework\App\Area::AREA_FRONTEND, true);
$imageUrl = $this->productImageHelper->create()->init($product, $imageType)->getUrl();
$this->appEmulation->stopEnvironmentEmulation();
return $imageUrl;
}
}
Please let me know whether you have any questions?
-
how to pass dynamic customer id ?customer_id=416 can i pass any variable instead 416 Mar 27, 2019 at 4:30
-
-
1[2020-06-20 05:32:24] main.CRITICAL: Report ID: webapi-5eed9f686690b; Message: The "array" class doesn't exist and the namespace must be specified. Verify and try again. {"exception":"[object] (Exception(code: 0): Report ID: webapi-5eed9f686690b; Message: The \"array\" class doesn't exist and the namespace must be specified. Verify and try again. at /opt/lampp/htdocs/m233/lib/internal/Magento/Framework/Webapi/ErrorProcessor.php:208, LogicException(code: 0): The \"array\" class doesn't exist and the namespace must be specified.– devmageJun 20, 2020 at 5:33
-
You can read guide about REST API.
http://alankent.me/2015/07/24/creating-a-new-rest-web-service-in-magento-2/
I read & practice and now I can do anything about REST API.
-
-
-
-
MagentoOdoo.com i tried that but ended up with : { "message": "Request does not match any route.", "trace": null } i tried the 1st example from : alankent.me/2015/07/24/… Apr 21, 2016 at 6:07
-
[2020-06-20 05:32:24] main.CRITICAL: Report ID: webapi-5eed9f686690b; Message: The "array" class doesn't exist and the namespace must be specified. Verify and try again. {"exception":"[object] (Exception(code: 0): Report ID: webapi-5eed9f686690b; Message: The \"array\" class doesn't exist and the namespace must be specified. Verify and try again. at /opt/lampp/htdocs/m233/lib/internal/Magento/Framework/Webapi/ErrorProcessor.php:208, LogicException(code: 0): The \"array\" class doesn't exist and the namespace must be specified.– devmageJun 20, 2020 at 5:33
Wishlist is integral part for any eCommerce platform and We wish Magento guys would have included them in the core features. So we have taken the initiative and added these features by taking out the fork from the github. We are waiting for Magento guys to Merge our pull request. In the meanwhile you can take use the following forks for your online store:
WishList: https://github.com/manish-ip/magento2
Or you can download our free plugin from the following link :
WishList API Plugin: http://www.ipragmatech.com/products/wishlist-rest-api-magento/
-
I started to process your Pull Request github.com/magento/magento2/pull/5233 and made a Code Review of your proposed changes for Wishlist API. Here you can read main considerations you should apply to make us merge current PR - github.com/magento/magento2/pull/… Feb 23, 2017 at 13:22
Try this it will help to you.
/**
* Add wishlist item for the customer
* @param int $customerId
* @param int $productIdId
* @return array|bool
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function addWishlistForCustomer($customerId, $productId)
{
if ($productId == null) {
throw new LocalizedException(__
('Invalid product, Please select a valid product'));
}
try {
$product = $this->_productRepository->getById($productId);
} catch (NoSuchEntityException $e) {
$product = null;
}
try {
$wishlist = $this->_wishlistRepository->create()->loadByCustomerId
($customerId, true);
$wishlist->addNewItem($product);
$returnData = $wishlist->save();
} catch (NoSuchEntityException $e) {
}
return true;
}