|
| 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 | +} |
0 commit comments