Skip to content

Commit 0b6127d

Browse files
committed
Addition of Wishlist API
1 parent 7e9d181 commit 0b6127d

File tree

4 files changed

+316
-0
lines changed

4 files changed

+316
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* Contributor company: iPragmatech solution Pvt Ltd.
4+
* Contributor Author : Manish Kumar
5+
* Date: 23/5/16
6+
* Time: 11:55 AM
7+
*/
8+
namespace Magento\Wishlist\Api;
9+
10+
/**
11+
* Interface WishlistManagementInterface
12+
* @api
13+
*/
14+
interface WishlistManagementInterface
15+
{
16+
17+
/**
18+
* Return Wishlist items.
19+
*
20+
* @param int $customerId
21+
* @return array
22+
*/
23+
public function getWishlistForCustomer($customerId);
24+
25+
/**
26+
* Return Added wishlist item.
27+
*
28+
* @param int $customerId
29+
* @param int $productId
30+
* @return array
31+
*
32+
*/
33+
public function addWishlistForCustomer($customerId, $productId);
34+
35+
/**
36+
* Return Added wishlist item.
37+
*
38+
* @param int $customerId
39+
* @param int $wishlistId
40+
* @return array
41+
*
42+
*/
43+
public function deleteWishlistForCustomer($customerId, $wishlistItemId);
44+
}
Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
<?php
2+
/**
3+
* Contributor company: iPragmatech solution Pvt Ltd.
4+
* Contributor Author : Manish Kumar
5+
* Date: 23/5/16
6+
* Time: 11:55 AM
7+
*/
8+
9+
namespace Magento\Wishlist\Model;
10+
11+
use Exception;
12+
use Magento\Wishlist\Api\WishlistManagementInterface;
13+
use Magento\Wishlist\Controller\WishlistProvider;
14+
use Magento\Wishlist\Model\ResourceModel\Item\CollectionFactory;
15+
use Magento\Wishlist\Model\WishlistFactory;
16+
use Magento\Catalog\Api\ProductRepositoryInterface;
17+
18+
/**
19+
* Defines the implementaiton class of the \Magento\Wishlist\Api\WishlistManagementInterface
20+
*/
21+
class WishlistManagement implements
22+
\Magento\Wishlist\Api\WishlistManagementInterface
23+
{
24+
25+
/**
26+
* @var CollectionFactory
27+
*/
28+
protected $_wishlistCollectionFactory;
29+
30+
/**
31+
* Wishlist item collection
32+
* @var \Magento\Wishlist\Model\ResourceModel\Item\Collection
33+
*/
34+
protected $_itemCollection;
35+
36+
/**
37+
* @var WishlistRepository
38+
*/
39+
protected $_wishlistRepository;
40+
41+
/**
42+
* @var ProductRepository
43+
*/
44+
protected $_productRepository;
45+
46+
/**
47+
* @var WishlistFactory
48+
*/
49+
protected $_wishlistFactory;
50+
51+
/**
52+
* @var Item
53+
*/
54+
protected $_itemFactory;
55+
56+
/**
57+
* @param CollectionFactory $wishlistCollectionFactory
58+
* @param \Magento\Catalog\Model\ProductFactory $productFactory
59+
* @param \Magento\Framework\Math\Random $mathRandom
60+
* @param \Magento\Framework\Stdlib\DateTime $dateTime
61+
* @param ProductRepositoryInterface $productRepository
62+
*/
63+
public function __construct(
64+
\Magento\Wishlist\Model\ResourceModel\Item\CollectionFactory $wishlistCollectionFactory,
65+
\Magento\Wishlist\Model\WishlistFactory $wishlistFactory,
66+
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
67+
\Magento\Wishlist\Model\ItemFactory $itemFactory
68+
) {
69+
$this->_wishlistCollectionFactory = $wishlistCollectionFactory;
70+
$this->_productRepository = $productRepository;
71+
$this->_wishlistFactory = $wishlistFactory;
72+
$this->_itemFactory = $itemFactory;
73+
}
74+
75+
/**
76+
* Get wishlist collection
77+
* @param int $customerId
78+
* @return array WishlistData
79+
*/
80+
public function getWishlistForCustomer($customerId)
81+
{
82+
if (empty($customerId) || !isset($customerId) || $customerId == "") {
83+
$message = __('Id required');
84+
$status = false;
85+
$response[] = [
86+
"message" => $message,
87+
"status" => $status
88+
];
89+
return $response;
90+
} else {
91+
$collection =
92+
$this->_wishlistCollectionFactory->create()
93+
->addCustomerIdFilter($customerId);
94+
95+
$wishlistData = [];
96+
foreach ($collection as $item) {
97+
$productInfo = $item->getProduct()->toArray();
98+
$data = [
99+
"wishlist_item_id" => $item->getWishlistItemId(),
100+
"wishlist_id" => $item->getWishlistId(),
101+
"product_id" => $item->getProductId(),
102+
"store_id" => $item->getStoreId(),
103+
"added_at" => $item->getAddedAt(),
104+
"description" => $item->getDescription(),
105+
"qty" => round($item->getQty()),
106+
"product" => $productInfo
107+
];
108+
$wishlistData[] = $data;
109+
}
110+
return $wishlistData;
111+
}
112+
}
113+
114+
/**
115+
* Add wishlist item for the customer
116+
* @param int $customerId
117+
* @param int $productIdId
118+
* @return array|bool
119+
*
120+
*/
121+
public function addWishlistForCustomer($customerId, $productId)
122+
{
123+
if ($productId == null) {
124+
$message = __('Invalid product, Please select a valid product');
125+
$status = false;
126+
$response[] = [
127+
"message" => $message,
128+
"status" => $status
129+
];
130+
return $response;
131+
}
132+
try {
133+
$product = $this->_productRepository->getById($productId);
134+
} catch (Exception $e) {
135+
return false;
136+
}
137+
try {
138+
$wishlist = $this->_wishlistFactory->create()
139+
->loadByCustomerId($customerId, true);
140+
$wishlist->addNewItem($product);
141+
$wishlist->save();
142+
} catch (Exception $e) {
143+
return false;
144+
}
145+
$message = __('Item added to wishlist.');
146+
$status = true;
147+
$response[] = [
148+
"message" => $message,
149+
"status" => $status
150+
];
151+
return $response;
152+
}
153+
154+
/**
155+
* Delete wishlist item for customer
156+
* @param int $customerId
157+
* @param int $productIdId
158+
* @return array
159+
*
160+
*/
161+
public function deleteWishlistForCustomer($customerId, $wishlistItemId)
162+
{
163+
164+
$message = null;
165+
$status = null;
166+
if ($wishlistItemId == null) {
167+
$message = __('Invalid wishlist item, Please select a valid item');
168+
$status = false;
169+
$response[] = [
170+
"message" => $message,
171+
"status" => $status
172+
];
173+
return $response;
174+
}
175+
$item = $this->_itemFactory->create()->load($wishlistItemId);
176+
if (!$item->getId()) {
177+
$message = __('The requested Wish List Item doesn\'t exist .');
178+
$status = false;
179+
180+
$response[] = [
181+
"message" => $message,
182+
"status" => $status
183+
];
184+
return $response;
185+
}
186+
$wishlistId = $item->getWishlistId();
187+
$wishlist = $this->_wishlistFactory->create();
188+
189+
if ($wishlistId) {
190+
$wishlist->load($wishlistId);
191+
} elseif ($customerId) {
192+
$wishlist->loadByCustomerId($customerId, true);
193+
}
194+
if (!$wishlist) {
195+
$message = __('The requested Wish List Item doesn\'t exist .');
196+
$status = false;
197+
$response[] = [
198+
"message" => $message,
199+
"status" => $status
200+
];
201+
return $response;
202+
}
203+
if (!$wishlist->getId() || $wishlist->getCustomerId() != $customerId) {
204+
$message = __('The requested Wish List Item doesn\'t exist .');
205+
$status = false;
206+
$response[] = [
207+
"message" => $message,
208+
"status" => $status
209+
];
210+
return $response;
211+
}
212+
try {
213+
$item->delete();
214+
$wishlist->save();
215+
} catch (Exception $e) {
216+
return false;
217+
}
218+
219+
$message = __(' Item has been removed from wishlist .');
220+
$status = true;
221+
$response[] = [
222+
"message" => $message,
223+
"status" => $status
224+
];
225+
return $response;
226+
}
227+
}

app/code/Magento/Wishlist/etc/di.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
99
<preference for="\Magento\Wishlist\Model\AuthenticationStateInterface" type="\Magento\Wishlist\Model\AuthenticationState" />
1010
<preference for="\Magento\Wishlist\Controller\WishlistProviderInterface" type="\Magento\Wishlist\Controller\WishlistProvider" />
11+
<!--Wishlist API preference-->
12+
<preference for="\Magento\Wishlist\Api\WishlistManagementInterface" type="\Magento\Wishlist\Model\WishlistManagement" />
13+
<!--Wishlist API preference End-->
14+
1115
<type name="Magento\Wishlist\Model\ResourceModel\Item\Collection\Grid">
1216
<arguments>
1317
<argument name="resource" xsi:type="object">Magento\Wishlist\Model\ResourceModel\Item</argument>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © 2016 Magento. All rights reserved.
5+
* See COPYING.txt for license details.
6+
* Contributor company: iPragmatech solution Pvt Ltd.
7+
* Contributor Author : Manish Kumar
8+
*/
9+
-->
10+
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
11+
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
12+
13+
<route url="/V1/wishlist/items" method="GET">
14+
<service class="Magento\Wishlist\Api\WishlistManagementInterface" method="getWishlistForCustomer"/>
15+
<resources>
16+
<resource ref="self" />
17+
</resources>
18+
<data>
19+
<parameter name="customerId" force="true">%customer_id%</parameter>
20+
</data>
21+
</route>
22+
23+
<route url="/V1/wishlist/add/:productId" method="POST">
24+
<service class="Magento\Wishlist\Api\WishlistManagementInterface" method="addWishlistForCustomer"/>
25+
<resources>
26+
<resource ref="self" />
27+
</resources>
28+
<data>
29+
<parameter name="customerId" force="true">%customer_id%</parameter>
30+
</data>
31+
</route>
32+
<route url="/V1/wishlist/delete/:wishlistItemId" method="DELETE">
33+
<service class="Magento\Wishlist\Api\WishlistManagementInterface" method="deleteWishlistForCustomer"/>
34+
<resources>
35+
<resource ref="self" />
36+
</resources>
37+
<data>
38+
<parameter name="customerId" force="true">%customer_id%</parameter>
39+
</data>
40+
</route>
41+
</routes>

0 commit comments

Comments
 (0)