1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\WishlistGraphQl\Model\Resolver;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Wishlist\Model\ResourceModel\Item\Collection as WishlistItemCollection;
use Magento\Wishlist\Model\ResourceModel\Item\CollectionFactory as WishlistItemCollectionFactory;
use Magento\Wishlist\Model\Item;
use Magento\Wishlist\Model\Wishlist;
/**
* Fetches the Wishlist Items data according to the GraphQL schema
*/
class WishlistItemsResolver implements ResolverInterface
{
/**
* @var WishlistItemCollectionFactory
*/
private $wishlistItemCollectionFactory;
/**
* @var StoreManagerInterface
*/
private $storeManager;
/**
* @param WishlistItemCollectionFactory $wishlistItemCollectionFactory
* @param StoreManagerInterface $storeManager
*/
public function __construct(
WishlistItemCollectionFactory $wishlistItemCollectionFactory,
StoreManagerInterface $storeManager
) {
$this->wishlistItemCollectionFactory = $wishlistItemCollectionFactory;
$this->storeManager = $storeManager;
}
/**
* @inheritdoc
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) {
if (!isset($value['model'])) {
throw new LocalizedException(__('Missing key "model" in Wishlist value data'));
}
/** @var Wishlist $wishlist */
$wishlist = $value['model'];
$wishlistItems = $this->getWishListItems($wishlist);
$data = [];
foreach ($wishlistItems as $wishlistItem) {
$data[] = [
'id' => $wishlistItem->getId(),
'qty' => $wishlistItem->getData('qty'),
'description' => $wishlistItem->getDescription(),
'added_at' => $wishlistItem->getAddedAt(),
'model' => $wishlistItem,
];
}
return $data;
}
/**
* Get wishlist items
*
* @param Wishlist $wishlist
* @return Item[]
*/
private function getWishListItems(Wishlist $wishlist): array
{
/** @var WishlistItemCollection $wishlistItemCollection */
$wishlistItemCollection = $this->wishlistItemCollectionFactory->create();
$wishlistItemCollection
->addWishlistFilter($wishlist)
->addStoreFilter(array_map(function (StoreInterface $store) {
return $store->getId();
}, $this->storeManager->getStores()))
->setVisibilityFilter();
return $wishlistItemCollection->getItems();
}
}