diff --git a/app/code/Magento/Wishlist/Api/WishlistManagementInterface.php b/app/code/Magento/Wishlist/Api/WishlistManagementInterface.php new file mode 100644 index 0000000000000..ae9abad79f123 --- /dev/null +++ b/app/code/Magento/Wishlist/Api/WishlistManagementInterface.php @@ -0,0 +1,44 @@ +_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; + } +} diff --git a/app/code/Magento/Wishlist/etc/di.xml b/app/code/Magento/Wishlist/etc/di.xml index a102672216bc8..f47094e5aac0d 100644 --- a/app/code/Magento/Wishlist/etc/di.xml +++ b/app/code/Magento/Wishlist/etc/di.xml @@ -8,6 +8,9 @@ + + + Magento\Wishlist\Model\ResourceModel\Item diff --git a/app/code/Magento/Wishlist/etc/webapi.xml b/app/code/Magento/Wishlist/etc/webapi.xml new file mode 100755 index 0000000000000..f71fad84c5292 --- /dev/null +++ b/app/code/Magento/Wishlist/etc/webapi.xml @@ -0,0 +1,41 @@ + + + + + + + + + + + %customer_id% + + + + + + + + + + %customer_id% + + + + + + + + + %customer_id% + + + \ No newline at end of file