WishlistItemsResolver.php 3.0 KB

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