Skip to content

Commit

Permalink
Merge pull request #2 from atravkovs/configurable
Browse files Browse the repository at this point in the history
Added support for Configurable products & implemented 3 endpoints
  • Loading branch information
alfredsgenkins authored Oct 16, 2019
2 parents 0dc8e62 + a6ea3fd commit 64c592c
Show file tree
Hide file tree
Showing 11 changed files with 656 additions and 137 deletions.
42 changes: 36 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
**WishlistGraphQl** provides additional resolvers for wishlist, extending Magento_WishlistGraphQl.


### AddProductToWishlist
### SaveWishlistItem

This endpoint allows to add product to Wishlist
This endpoint allows to save Wishlist item

```graphql
mutation AddProductToWishlist($productSku: String!) {
addProductToWishlist(productSku: $productSku) {
mutation SaveWishlistItem($wishlistItem: WishlistItemInput!) {
saveWishlistItem(wishlistItem: $wishlistItem) {
id
sku
qty
description
added_at
Expand All @@ -21,15 +22,24 @@ mutation AddProductToWishlist($productSku: String!) {

```json
{
"product_sku": "n31189077-1"
"wishlistItem": {
"sku": "n31189077-1",
"quantity": 2,
"description": "Description",
"product_option": {
"extension_attributes": {}
}
}
}
```


### RemoveProductFromWishlist

This endpoint allows removing item from wishlist

```graphql
mutation RemoveProductFromWishlist($item_id: Int!) {
mutation RemoveProductFromWishlist($item_id: ID!) {
removeProductFromWishlist(item_id: $item_id)
}
```
Expand All @@ -39,3 +49,23 @@ mutation RemoveProductFromWishlist($item_id: Int!) {
"item_id": 1
}
```

### MoveWishlistToCart

This endpoint allows to move all wishlist items to cart

```graphql
mutation MoveWishlistToCart {
moveWishlistToCart()
}
```

### ClearWishlist

This endpoint allows to clear wishlist

```graphql
mutation ClearWishlist {
clearWishlist()
}
```
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
{
"name": "Alfreds Genkins",
"email": "[email protected]"
},
{
"name": "Artjoms Travkovs",
"email": "[email protected]"
}
],
"license": [
Expand Down
122 changes: 0 additions & 122 deletions src/Model/Resolver/AddProductToWishlist.php

This file was deleted.

84 changes: 84 additions & 0 deletions src/Model/Resolver/ClearWishlist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

/**
* ScandiPWA - Progressive Web App for Magento
*
* Copyright © Scandiweb, Inc. All rights reserved.
* See LICENSE for license details.
*
* @license OSL-3.0 (Open Software License ("OSL") v. 3.0)
* @package scandipwa/wishlist-graphql
* @link https://github.com/scandipwa/wishlist-graphql
*/

declare(strict_types=1);

namespace ScandiPWA\WishlistGraphQl\Model\Resolver;

use Exception;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Wishlist\Model\ResourceModel\Wishlist as WishlistResourceModel;
use Magento\Wishlist\Model\Wishlist;
use Magento\Wishlist\Model\WishlistFactory;

class ClearWishlist implements ResolverInterface
{
/**
* @var WishlistFactory
*/
protected $wishlistFactory;

/**
* @var WishlistResourceModel
*/
protected $wishlistResource;

public function __construct(
WishlistFactory $wishlistFactory,
WishlistResourceModel $wishlistResource
) {
$this->wishlistFactory = $wishlistFactory;
$this->wishlistResource = $wishlistResource;
}

/**
* @inheritDoc
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) {
$customerId = $context->getUserId();
if (!$customerId) {
throw new GraphQlAuthorizationException(__('Authorization unsuccessful'));
}

/** @var Wishlist $wishlist */
$wishlist = $this->wishlistFactory->create();
$this->wishlistResource->load($wishlist, $customerId, 'customer_id');

if (!$wishlist->getId() || $wishlist->getItemsCount() <= 0) {
return true;
}

$wishlistItems = $wishlist->getItemCollection();
foreach ($wishlistItems as $item) {
$item->delete();
}

try {
$wishlist->save();
} catch (Exception $e) {
throw new GraphQlNoSuchEntityException(__('There was an error when clearing wishlist'));
}

return true;
}
}
Loading

0 comments on commit 64c592c

Please sign in to comment.