Skip to content

Addition of Wishlist functionality to API #4877

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions app/code/Magento/Wishlist/Api/WishlistManagementInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* Contributor company: iPragmatech solution Pvt Ltd.
* Contributor Author : Manish Kumar
* Date: 23/5/16
* Time: 11:55 AM
*/
namespace Magento\Wishlist\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);

/**
* Return Added wishlist item.
*
* @param int $customerId
* @param int $wishlistId
* @return array
*
*/
public function deleteWishlistForCustomer($customerId, $wishlistItemId);
}
227 changes: 227 additions & 0 deletions app/code/Magento/Wishlist/Model/WishlistManagement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
<?php
/**
* Contributor company: iPragmatech solution Pvt Ltd.
* Contributor Author : Manish Kumar
* Date: 23/5/16
* Time: 11:55 AM
*/

namespace Magento\Wishlist\Model;

use Exception;
use Magento\Wishlist\Api\WishlistManagementInterface;
use Magento\Wishlist\Controller\WishlistProvider;
use Magento\Wishlist\Model\ResourceModel\Item\CollectionFactory;
use Magento\Wishlist\Model\WishlistFactory;
use Magento\Catalog\Api\ProductRepositoryInterface;

/**
* Defines the implementaiton class of the \Magento\Wishlist\Api\WishlistManagementInterface
*/
class WishlistManagement implements
\Magento\Wishlist\Api\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;

/**
* @param CollectionFactory $wishlistCollectionFactory
* @param \Magento\Catalog\Model\ProductFactory $productFactory
* @param \Magento\Framework\Math\Random $mathRandom
* @param \Magento\Framework\Stdlib\DateTime $dateTime
* @param ProductRepositoryInterface $productRepository
*/
public function __construct(
\Magento\Wishlist\Model\ResourceModel\Item\CollectionFactory $wishlistCollectionFactory,
\Magento\Wishlist\Model\WishlistFactory $wishlistFactory,
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
\Magento\Wishlist\Model\ItemFactory $itemFactory
) {
$this->_wishlistCollectionFactory = $wishlistCollectionFactory;
$this->_productRepository = $productRepository;
$this->_wishlistFactory = $wishlistFactory;
$this->_itemFactory = $itemFactory;
}

/**
* Get wishlist collection
* @param int $customerId
* @return array WishlistData
*/
public function getWishlistForCustomer($customerId)
{
if (empty($customerId) || !isset($customerId) || $customerId == "") {
$message = __('Id required');
$status = false;
$response[] = [
"message" => $message,
"status" => $status
];
return $response;
} else {
$collection =
$this->_wishlistCollectionFactory->create()
->addCustomerIdFilter($customerId);

$wishlistData = [];
foreach ($collection as $item) {
$productInfo = $item->getProduct()->toArray();
$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
*
*/
public function addWishlistForCustomer($customerId, $productId)
{
if ($productId == null) {
$message = __('Invalid product, Please select a valid product');
$status = false;
$response[] = [
"message" => $message,
"status" => $status
];
return $response;
}
try {
$product = $this->_productRepository->getById($productId);
} catch (Exception $e) {
return false;
}
try {
$wishlist = $this->_wishlistFactory->create()
->loadByCustomerId($customerId, true);
$wishlist->addNewItem($product);
$wishlist->save();
} catch (Exception $e) {
return false;
}
$message = __('Item added to wishlist.');
$status = true;
$response[] = [
"message" => $message,
"status" => $status
];
return $response;
}

/**
* Delete wishlist item for customer
* @param int $customerId
* @param int $productIdId
* @return array
*
*/
public function deleteWishlistForCustomer($customerId, $wishlistItemId)
{

$message = null;
$status = null;
if ($wishlistItemId == null) {
$message = __('Invalid wishlist item, Please select a valid item');
$status = false;
$response[] = [
"message" => $message,
"status" => $status
];
return $response;
}
$item = $this->_itemFactory->create()->load($wishlistItemId);
if (!$item->getId()) {
$message = __('The requested Wish List Item doesn\'t exist .');
$status = false;

$response[] = [
"message" => $message,
"status" => $status
];
return $response;
}
$wishlistId = $item->getWishlistId();
$wishlist = $this->_wishlistFactory->create();

if ($wishlistId) {
$wishlist->load($wishlistId);
} elseif ($customerId) {
$wishlist->loadByCustomerId($customerId, true);
}
if (!$wishlist) {
$message = __('The requested Wish List Item doesn\'t exist .');
$status = false;
$response[] = [
"message" => $message,
"status" => $status
];
return $response;
}
if (!$wishlist->getId() || $wishlist->getCustomerId() != $customerId) {
$message = __('The requested Wish List Item doesn\'t exist .');
$status = false;
$response[] = [
"message" => $message,
"status" => $status
];
return $response;
}
try {
$item->delete();
$wishlist->save();
} catch (Exception $e) {
return false;
}

$message = __(' Item has been removed from wishlist .');
$status = true;
$response[] = [
"message" => $message,
"status" => $status
];
return $response;
}
}
3 changes: 3 additions & 0 deletions app/code/Magento/Wishlist/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="\Magento\Wishlist\Model\AuthenticationStateInterface" type="\Magento\Wishlist\Model\AuthenticationState" />
<preference for="\Magento\Wishlist\Controller\WishlistProviderInterface" type="\Magento\Wishlist\Controller\WishlistProvider" />
<!--Wishlist API preference-->
<preference for="\Magento\Wishlist\Api\WishlistManagementInterface" type="\Magento\Wishlist\Model\WishlistManagement" />
<!--Wishlist API preference End-->
<type name="Magento\Wishlist\Model\ResourceModel\Item\Collection\Grid">
<arguments>
<argument name="resource" xsi:type="object">Magento\Wishlist\Model\ResourceModel\Item</argument>
Expand Down
41 changes: 41 additions & 0 deletions app/code/Magento/Wishlist/etc/webapi.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
* Contributor company: iPragmatech solution Pvt Ltd.
* Contributor Author : Manish Kumar
*/
-->
<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="Magento\Wishlist\Api\WishlistManagementInterface" method="getWishlistForCustomer"/>
<resources>
<resource ref="self" />
</resources>
<data>
<parameter name="customerId" force="true">%customer_id%</parameter>
</data>
</route>

<route url="/V1/wishlist/add/:productId" method="POST">
<service class="Magento\Wishlist\Api\WishlistManagementInterface" method="addWishlistForCustomer"/>
<resources>
<resource ref="self" />
</resources>
<data>
<parameter name="customerId" force="true">%customer_id%</parameter>
</data>
</route>
<route url="/V1/wishlist/delete/:wishlistItemId" method="DELETE">
<service class="Magento\Wishlist\Api\WishlistManagementInterface" method="deleteWishlistForCustomer"/>
<resources>
<resource ref="self" />
</resources>
<data>
<parameter name="customerId" force="true">%customer_id%</parameter>
</data>
</route>
</routes>